Vor 8 Jahren hatte ich schon mal über das OLED Display „Don’t give up! berichtet. Und während der Pandemie diente es als CO2 Anzeige. Das habe ich nun abgebaut und durch ein paar Abfragen per REST-Api von OpenWeatherMap und Coingecko umprogrammiert. Das Ergebnis sieht man in diesem Video:
Es werden mit dem ESP32: Wetterdaten wie Temperatur, Luftfeuchte, Luftdruck und Kursdaten von BTC, ETC, SOL, DOGE, NANO mit DUINO User-Wallet angezeigt. Das Programm sieht ungefähr so aus:
Der Code für das Flow-Diagramm sieht so aus:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
flowchart TD A([Start]) --> B[Initialisierung] B --> C[WiFi verbinden] C --> D[NTP-Zeit synchronisieren] D --> E{Hauptschleife loop} E --> F[Lokale Zeit abrufen] F --> G{Zeit zwischen 0:00 und 5:00?} G -->|Ja| H[Display ausschalten] G -->|Nein| I{Update-Intervall erreicht?} I -->|Ja| J[Preise abrufen] J --> K[LED einschalten] K --> L[HTTP-Anfrage senden] L --> M[JSON parsen und Preise speichern] M --> N[LED ausschalten] N --> O{Display-Intervall erreicht?} I -->|Nein| O O -->|Ja| P[Preise anzeigen] O -->|Nein| Q[1 Sekunde warten] P --> Q Q --> E H --> Q |
Hier der Code, der auch in aktueller Version in meinem GitLab Repo zu finden ist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
// // Kurs Abfrage jede 15. Minuten und Anzeigen wechsl alle 3 Sekunden auf SSD1306 Display. // Nachts von 0 -6 Uhr keine Anzeige und Abfragen // // Details siehe http://blog.wenzlaff.de/ // // Thomas Wenzlaff // #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <WiFi.h> #include <HTTPClient.h> #include <ArduinoJson.h> #include <time.h> // Passwort Datei für das WLAN, mit eigenen Daten anpassen und erstellen, // einfach die credentials.h.templates renamen nach credentials.h und den WLAN Namen und Passwort eintragen. // Die Datei sieht so aus: // #ifndef CREDENTIALS_H // #define CREDENTIALS_H // Anpassen auf die eigenen WLAN-Daten // #define SSID "WLAN-Name" // #define PASSWORT "WLAN-Passwort" // // https://api.openweathermap.org // #define OPENWEATHERMAP_API_KEY "Api Key" // #define ORTS_KEY "Orts Key z.B. Langenhagen 2881062" // Duino User Name // #define DUINO_USER_NAME "kleinhirn" // #endif #include "credentials.h" // Oder anpassen auf die eigenen WLAN-Daten aus credentials.h oder hier setzen und einkommentieren // const char* SSID = "WLAN Name" // const char* PASSWORT = WLAN Passwort #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C #define UPDATE_INTERVAL 60 * 15 * 1000 // 15 minute #define DISPLAY_INTERVAL 3 * 1000 // 3 seconds #define BLUE_LED_PIN 2 enum CryptoIndex { BITCOIN, ETHEREUM, SOLANA, NANO, DOGECOIN, DUINO_USER, WETTER_TEMPERATUR, WETTER_LUFTDRUCK, WETTER_FEUCHTE, NUM_CRYPTOS // muss als letztes stehen }; const char* const cryptoIds[NUM_CRYPTOS] = { "bitcoin", "ethereum", "solana", "nano", "dogecoin", "balance", // https://server.duinocoin.com/users/kleinhirn in result/balance/balance "main", // Temperatur von https://api.openweathermap.org "main", // Luftdruck von https://api.openweathermap.org "main" // Feuchte von https://api.openweathermap.org }; const char* const cryptoNames[NUM_CRYPTOS] PROGMEM = { "Bitcoin", "Ethereum", "Solana", "Nano", "Dogecoin", "DUINO Wallet", // eigener Wert vom User Wallet "Temperatur", "Luftdruck", "Feuchte" }; const char* const urls[NUM_CRYPTOS] = { "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=eur", // "Data provided by CoinGecko" "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=eur", "https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=eur", "https://api.coingecko.com/api/v3/simple/price?ids=nano&vs_currencies=eur", "https://api.coingecko.com/api/v3/simple/price?ids=dogecoin&vs_currencies=eur", "https://server.duinocoin.com/users/" DUINO_USER_NAME , "https://api.openweathermap.org/data/2.5/weather?id=" ORTS_KEY "&appid=" OPENWEATHERMAP_API_KEY "&units=metric", // Temperatur "https://api.openweathermap.org/data/2.5/weather?id=" ORTS_KEY "&appid=" OPENWEATHERMAP_API_KEY "&units=metric", // Luftdruck "https://api.openweathermap.org/data/2.5/weather?id=" ORTS_KEY "&appid=" OPENWEATHERMAP_API_KEY "&units=metric" // Feuchte }; float prices[NUM_CRYPTOS] = {0.0}; unsigned long lastUpdate = 0; unsigned long lastDisplay = 0; CryptoIndex currentIndex = BITCOIN; // NTP-Server und Zeitzone definieren // "CET-1CEST,M3.5.0/2,M10.5.0/3": // CET-1: Mitteleuropäische Zeit (UTC+1). // CEST: Mitteleuropäische Sommerzeit. // M3.5.0/2: Beginn der Sommerzeit am letzten Sonntag im März um 2 Uhr. // M10.5.0/3: Ende der Sommerzeit am letzten Sonntag im Oktober um 3 Uhr. const char* ntpServer = "de.pool.ntp.org"; const char* timeZone = "CET-1CEST,M3.5.0/2,M10.5.0/3"; // Mitteleuropäische Zeit (Berlin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { // setup läuft nur einmal Serial.begin(115200); Wire.begin(); // Display ini if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;) ; } display.setTextColor(WHITE); WiFi.begin(SSID, PASSWORT); display.clearDisplay(); display.setTextSize(1); display.setCursor(4, 25); display.print("Verbinde ..."); display.display(); while (WiFi.status() != WL_CONNECTED) { delay(500); } pinMode(BLUE_LED_PIN, OUTPUT); // Zeitzone und NTP-Server konfigurieren configTzTime(timeZone, ntpServer); printLocalTime(); lastUpdate = millis() - UPDATE_INTERVAL; // Sofortige Initialisierung lastDisplay = millis() - DISPLAY_INTERVAL; // Beim ersten Start direkt Daten abrufen fetchPrices(); } void displayPrice(float price, const char* currency, CryptoIndex currentCrypto) { display.clearDisplay(); display.setTextSize(2); display.setCursor(4, 4); display.print(currency); display.setCursor(4, 25); // Überprüfe die aktuelle Kryptowährung und passe die Dezimalstellen und TextSize an if (currentCrypto == DUINO_USER || currentCrypto == DOGECOIN) { display.setTextSize(2); display.print(price, 6); // Zeigt den Preis mit 6 Dezimalstellen an } else if (currentCrypto == NANO) { display.setTextSize(2); display.print(price, 6); // Zeigt den Preis mit 6 Dezimalstellen an } else if (currentCrypto == BITCOIN) { display.setTextSize(4); display.print(price, 0); // Zeigt den Preis ohne Dezimalstellen an } else if (currentCrypto == ETHEREUM || currentCrypto == WETTER_LUFTDRUCK || currentCrypto == WETTER_FEUCHTE) { display.setTextSize(4); display.print(price, 0); } else if (currentCrypto == SOLANA) { display.setTextSize(4); display.print(price, 1); } else { display.setTextSize(4); display.print(price, 2); // Standardmäßig mit 2 Dezimalstellen } display.display(); // Aktualisiere die Anzeige } void displayError(const char* error) { display.clearDisplay(); display.setTextSize(1); display.setCursor(4, 25); display.print(error); display.display(); } void fetchPrices() { if (WiFi.status() == WL_CONNECTED) { digitalWrite(BLUE_LED_PIN, HIGH); for (int i = 0; i < NUM_CRYPTOS; i++) { HTTPClient http; http.begin(urls[i]); int httpResponseCode = http.GET(); if (httpResponseCode > 0) { String payload = http.getString(); DynamicJsonDocument doc(3072); // Falls notwendig, anpassen (z. B. 3072 oder 4096) DeserializationError error = deserializeJson(doc, payload); // Überprüfen, ob die Deserialisierung erfolgreich war if (error) { Serial.print(F("deserializeJson() fehlgeschlagen: ")); Serial.println(error.c_str()); // Gibt die genaue Fehlermeldung aus } if (!error) { if (i == DUINO_USER) { if (doc["result"]["balance"]["balance"].isNull()) { Serial.println(F("Fehler: Erwarteter JSON-Pfad 'result.balance.balance' fehlt.")); } else { prices[i] = doc["result"]["balance"]["balance"].as<float>(); } } else if (i == WETTER_TEMPERATUR) { if (doc["main"]["temp"].isNull()) { Serial.println(F("Fehler: Erwarteter JSON-Pfad 'main.temp' fehlt.")); } else { prices[i] = doc["main"]["temp"].as<float>(); } } else if (i == WETTER_LUFTDRUCK) { if (doc["main"]["pressure"].isNull()) { Serial.println(F("Fehler: Erwarteter JSON-Pfad 'main.pressure' fehlt.")); } else { prices[i] = doc["main"]["pressure"].as<float>(); } } else if (i == WETTER_FEUCHTE) { if (doc["main"]["humidity"].isNull()) { Serial.println(F("Fehler: Erwarteter JSON-Pfad 'main.humidity' fehlt.")); } else { prices[i] = doc["main"]["humidity"].as<float>(); } } else { if (doc[cryptoIds[i]]["eur"].isNull()) { Serial.print(F("Fehler: Erwarteter JSON-Pfad '")); Serial.print(cryptoIds[i]); Serial.println(F(".eur' fehlt.")); } else { prices[i] = doc[cryptoIds[i]]["eur"]; } } } } http.end(); } lastUpdate = millis(); digitalWrite(BLUE_LED_PIN, LOW); } } void loop() { unsigned long currentMillis = millis(); time_t now; struct tm timeinfo; if (!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); return; } if (timeinfo.tm_hour >= 0 && timeinfo.tm_hour < 6) { Serial.println("Anzeige in bis morgens aus ..."); display.ssd1306_command(SSD1306_DISPLAYOFF); } else { display.ssd1306_command(SSD1306_DISPLAYON); if (currentMillis - lastUpdate >= UPDATE_INTERVAL) { Serial.println("Daten aktualisieren..."); fetchPrices(); } if (currentMillis - lastDisplay >= DISPLAY_INTERVAL) { //Serial.println("Anzeige aktualisieren..."); displayPrice(prices[currentIndex], cryptoNames[currentIndex], static_cast<CryptoIndex>(currentIndex)); currentIndex = static_cast<CryptoIndex>((currentIndex + 1) % NUM_CRYPTOS); lastDisplay = currentMillis; } } delay(1000); } void printLocalTime() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { Serial.println("Fehler: Zeit konnte nicht abgerufen werden"); return; } Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S"); } |
Video Musik: „Sappheiros – Embrace“ is under a Creative Commons (BY 3.0) license: https://creativecommons.org/licenses/… https://open.spotify.com/artist/5ZVHX… Music powered by BreakingCopyright: Chill Instrumental [Non Copyrighted