Untitled

From Emerald Gibbon, 7 Years ago, written in Plain Text, viewed 2 times.
URL https://paste.blessuren.de/view/1f8cf814 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. // https://www.finanzen.net/aktien/Tesla-Aktie
  34. char* coins[] = {"share:Tesla-Aktie", "share:BVB-Aktie", "bitcoin", "ethereum", "dash", "pivx", "verge"};
  35.  
  36. String text = "CONNECTING...";
  37. String coin = "";
  38. String coinText = "";
  39. String coinSymbol = "";
  40.  
  41. int arraySize = 0;
  42. int spacer = 1;
  43. int width  = 5 + spacer; // The font width is 5 pixels
  44.  
  45. void setup() {
  46.   Serial.begin(115200);
  47.   WiFi_Setup();
  48.  
  49.   matrix.setIntensity(0); // Use a value between 0 and 15 for brightness
  50.   matrix.setRotation(0, 1);    // The first display is position upside down
  51.   matrix.setRotation(1, 1);    // The first display is position upside down
  52.   matrix.setRotation(2, 1);    // The first display is position upside down
  53.   matrix.setRotation(3, 1);    // The first display is position upside down
  54. }
  55.  
  56. void loop() {
  57.   matrix.fillScreen(LOW);
  58.  
  59.   Serial.println(zaehler);
  60.  
  61.   if (zaehler == -1) {
  62.     display_message(text);
  63.     delay(2000);
  64.     zaehler = 1;
  65.   }
  66.   else if (zaehler == 0) {
  67.     zaehler = 10;
  68.     if (WiFi.status() == WL_CONNECTED) {
  69.       HTTPClient http;
  70.       text = "";
  71.       arraySize = sizeof(coins) / sizeof(char *);
  72.       for (int i = 0; i < arraySize; i++) {
  73.         coin = String(coins[i]);
  74.         coinText = "";
  75.         coinSymbol = "";
  76.         Serial.println(i);
  77.         http.begin("http://coin.blessuren.de/get.php?coin=" + coin);
  78.         int httpCode = http.GET();
  79.         if (httpCode > 0) {
  80.           coinText = http.getString();
  81.           if(coin.indexOf(':') < 0) {
  82.             JsonArray& array = jsonBuffer.parseArray(coinText);
  83.             JsonObject& root = jsonBuffer.parseObject(array.get<String>(0));
  84.             coinText = root["price_usd"].as<String>(); // or price_eur
  85.             coinSymbol = root["symbol"].as<String>();
  86.             coinText = coinSymbol + ":$" + coinText.substring(0, coinText.indexOf('.') + 3); // 4 = 3 digits after . 3 = 2 digits after .
  87.           }
  88.         }
  89.         else {
  90.           text = "HTTPS Error";
  91.         }
  92.         http.end();
  93.         text = text + ' ' + coinText;
  94.       }
  95.     }
  96.  
  97.  
  98.   }
  99.  
  100.   zaehler--;
  101.   display_message(text);
  102. }
  103.  
  104. void display_message(String message) {
  105.   for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
  106.     //matrix.fillScreen(LOW);
  107.     int letter = i / width;
  108.     int x = (matrix.width() - 1) - i % width;
  109.     int y = (matrix.height() - 8) / 2; // center the text vertically
  110.     while ( x + width - spacer >= 0 && letter >= 0 ) {
  111.       if ( letter < message.length() ) {
  112.         matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background off, reverse to invert the image
  113.       }
  114.       letter--;
  115.       x -= width;
  116.     }
  117.     matrix.write(); // Send bitmap to display
  118.     delay(wait / 2);
  119.   }
  120. }
  121.  
  122. void WiFi_Setup() {
  123.   WiFiManager wifiManager; // Connect to Wi-Fi
  124.   // 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
  125.   // so restart the ESP and connect your PC to the wireless access point called 'ESP_AP' or whatever you call it below in ""
  126.   // wifiManager.resetSettings(); // Command to be included if needed, then connect to http://192.168.4.1/ and follow instructions to make the WiFi connection
  127.   // Set a timeout until configuration is turned off, useful to retry or go to sleep in n-seconds
  128.   wifiManager.setTimeout(180);
  129.   //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
  130.   if (!wifiManager.autoConnect("ESP_AP")) {
  131.     Serial.println(F("failed to connect and timeout occurred"));
  132.     delay(6000);
  133.     ESP.restart(); //reset and try again
  134.   }
  135.   // At this stage the WiFi manager will have successfully connected to a network, or if not will try again in 180-seconds
  136.   Serial.println(F("WiFi connected..."));
  137.   //----------------------------------------------------------------------
  138.   Serial.println(F("Use this URL to connect: http://")); Serial.println(WiFi.localIP().toString() + "/"); // Print the IP address
  139. }
  140.  

Reply to "Untitled"

Here you can reply to the paste above