Untitled

From Subtle Marten, 7 Years ago, written in C, viewed 2 times.
URL https://paste.blessuren.de/view/e95a0c7f Embed
Download Paste or View Raw
  1. #ifdef ESP32
  2. #include <DNSServer.h>
  3. #include <WebServer.h>
  4. #include <WiFi.h>
  5. #else
  6. #include <DNSServer.h>
  7. #include <ESP8266WebServer.h>
  8. #include <ESP8266WiFi.h>
  9. #endif
  10. #include <WiFiManager.h>
  11. #include <Adafruit_GFX.h>
  12. #include <Max72xxPanel.h>
  13. #include <ESP8266HTTPClient.h>
  14. #include <ArduinoJson.h>
  15.  
  16. int pinCS = D4; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
  17. int numberOfHorizontalDisplays = 4;
  18. int numberOfVerticalDisplays   = 1;
  19. DynamicJsonBuffer jsonBuffer;
  20.  
  21. // LED Matrix Pin -> ESP8266 Pin
  22. // Vcc            -> 3v  (3V on NodeMCU 3V3 on WEMOS)
  23. // Gnd            -> Gnd (G on NodeMCU)
  24. // DIN            -> D7  (Same Pin for WEMOS)
  25. // CS             -> D4  (Same Pin for WEMOS)
  26. // CLK            -> D5  (Same Pin for WEMOS)
  27.  
  28. Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
  29.  
  30. int wait = 100; // In milliseconds
  31.  
  32. int zaehler = -1;
  33. char* coins[] = {"bitcoin", "ethereum", "dash", "pivx"};
  34.  
  35. String text = "CONNECTING...";
  36. String coinText = "";
  37. String coinSymbol = "";
  38.  
  39. int arraySize = 0;
  40. int spacer = 1;
  41. int width  = 5 + spacer; // The font width is 5 pixels
  42.  
  43. void setup() {
  44.   Serial.begin(115200);
  45.   WiFi_Setup();
  46.  
  47.   matrix.setIntensity(0); // Use a value between 0 and 15 for brightness
  48.   matrix.setRotation(0, 1);    // The first display is position upside down
  49.   matrix.setRotation(1, 1);    // The first display is position upside down
  50.   matrix.setRotation(2, 1);    // The first display is position upside down
  51.   matrix.setRotation(3, 1);    // The first display is position upside down
  52. }
  53.  
  54. void loop() {
  55.   matrix.fillScreen(LOW);
  56.  
  57.   Serial.println(zaehler);
  58.  
  59.   if (zaehler == -1) {
  60.     display_message(text);
  61.     delay(2000);
  62.     zaehler = 1;
  63.   }
  64.   else if (zaehler == 0) {
  65.     zaehler = 10;
  66.     if (WiFi.status() == WL_CONNECTED) {
  67.       HTTPClient http;
  68.       text = "";
  69.       arraySize = sizeof(coins) / sizeof(char *);
  70.       for (int i = 0; i < arraySize; i++) {
  71.         coinText = "";
  72.         coinSymbol = "";
  73.         Serial.println(i);
  74.         http.begin("http://coin.blessuren.de/get.php?coin=" + String(coins[i]));
  75.         int httpCode = http.GET();
  76.         if (httpCode > 0) {
  77.           coinText = http.getString();
  78.           JsonArray& array = jsonBuffer.parseArray(coinText);
  79.           JsonObject& root = jsonBuffer.parseObject(array.get<String>(0));
  80.           coinText = root["price_usd"].as<String>(); // or price_eur
  81.           coinSymbol = root["symbol"].as<String>();
  82.           coinText = coinSymbol + ":$" + coinText.substring(0, coinText.indexOf('.') + 3); // 4 = 3 digits after . 3 = 2 digits after .
  83.         }
  84.         else {
  85.           text = "HTTPS Error";
  86.         }
  87.         http.end();
  88.         text = text + ' ' + coinText;
  89.       }
  90.     }
  91.  
  92.  
  93.   }
  94.  
  95.   zaehler--;
  96.   display_message(text);
  97. }
  98.  
  99. void display_message(String message) {
  100.   for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
  101.     //matrix.fillScreen(LOW);
  102.     int letter = i / width;
  103.     int x = (matrix.width() - 1) - i % width;
  104.     int y = (matrix.height() - 8) / 2; // center the text vertically
  105.     while ( x + width - spacer >= 0 && letter >= 0 ) {
  106.       if ( letter < message.length() ) {
  107.         matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background off, reverse to invert the image
  108.       }
  109.       letter--;
  110.       x -= width;
  111.     }
  112.     matrix.write(); // Send bitmap to display
  113.     delay(wait / 2);
  114.   }
  115. }
  116.  
  117. void WiFi_Setup() {
  118.   WiFiManager wifiManager; // Connect to Wi-Fi
  119.   // A new OOB ESP has no Wi-Fi credentials so will connect and not need the next command to be uncommented and compiled in, a used one with incorrect credentials will
  120.   // so restart the ESP and connect your PC to the wireless access point called 'ESP_AP' or whatever you call it below in ""
  121.   // wifiManager.resetSettings(); // Command to be included if needed, then connect to http://192.168.4.1/ and follow instructions to make the WiFi connection
  122.   // Set a timeout until configuration is turned off, useful to retry or go to sleep in n-seconds
  123.   wifiManager.setTimeout(180);
  124.   //fetches ssid and password and tries to connect, if connections succeeds it starts an access point with the name called "ESP8266_AP" and waits in a blocking loop for configuration
  125.   if (!wifiManager.autoConnect("ESP_AP")) {
  126.     Serial.println(F("failed to connect and timeout occurred"));
  127.     delay(6000);
  128.     ESP.restart(); //reset and try again
  129.   }
  130.   // At this stage the WiFi manager will have successfully connected to a network, or if not will try again in 180-seconds
  131.   Serial.println(F("WiFi connected..."));
  132.   //----------------------------------------------------------------------
  133.   Serial.println(F("Use this URL to connect: http://")); Serial.println(WiFi.localIP().toString() + "/"); // Print the IP address
  134. }
  135.  

Reply to "Untitled"

Here you can reply to the paste above