Untitled

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

Reply to "Untitled"

Here you can reply to the paste above