|
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 |
#ifdef ESP8266 #include "ESP8266WiFi.h" #include "ESP8266HTTPClient.h" #include "ESP8266WiFiMulti.h" ESP8266WiFiMulti WiFiNetzwerke; WiFiClient Klient; #endif #ifdef ESP32 #include "WiFi.h" #include "HTTPClient.h" #include "WiFiMulti.h" WiFiMulti WiFiNetzwerke; #endif #include "Wire.h" // https://werner.rothschopf.net/202003_arduino_liquid_crystal_umlaute.htm // Download zip #include "NoiascaLiquidCrystal.h" // Hardware deklarieren #include "NoiascaHW/lcd_PCF8574.h" // Objekt lcd erzeugen // Adresse, Anzahl Zeichen, Anzahl Reihen, Konvertierung für große Umlaute LiquidCrystal_PCF8574 lcd(0x27, 20, 4, convert_special); #include "ArduinoJson.h" #include "NTP3.h" #include "WiFiUdp.h" WiFiUDP wifiUdp; NTP3 ntp(wifiUdp); // ESP-01 // bei Standard-I2C mit // ergänzen #define SDA_PIN 0 #define SCL_PIN 2 // ESP8266 -> kein https #ifdef ESP32 const char* WetterDaten = "https://api.open-meteo.com/v1/forecast?latitude=50.9856&longitude=7.133&daily=temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,precipitation_probability_max,weather_code,precipitation_sum,sunshine_duration,daylight_duration&models=dwd_icon_seamless¤t=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,rain,showers,snowfall,surface_pressure,cloud_cover,precipitation,wind_speed_10m,wind_direction_10m,weather_code&timezone=Europe%2FBerlin&forecast_days=7"; #else const char* WetterDaten = "http://api.open-meteo.com/v1/forecast?latitude=50.9856&longitude=7.133&daily=temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,precipitation_probability_max,weather_code,precipitation_sum,sunshine_duration,daylight_duration&models=dwd_icon_seamless¤t=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,rain,showers,snowfall,surface_pressure,cloud_cover,precipitation,wind_speed_10m,wind_direction_10m,weather_code&timezone=Europe%2FBerlin&forecast_days=7"; #endif String Stadt = "Bergisch Gladbach"; unsigned long Intervall = 300000; // String für die vom Server gelieferten Daten String JSONDaten; void setup() { // ESP-01 -> I2C mit den angegegeben Pins starten // ESP mit Standard-I2C -> // ergänzen Wire.begin(SCL_PIN, SDA_PIN); // LCD starten lcd.begin(); lcd.createUml(); lcd.backlight(); lcd.clear(); Serial.begin(9600); // Stations-Modus WiFi.mode(WIFI_STA); // verfügbare WiFi-Netzwerke WiFiNetzwerke.addAP("Router_SSID", "xxxxxxxx"); WiFiNetzwerke.addAP("Repeater_1", "xxxxxxxx"); WiFiNetzwerke.addAP("Repeater_2", "xxxxxxxx"); // Verbindung aufbauen if (WiFiNetzwerke.run() == WL_CONNECTED) { Serial.println("WiFi verbunden ..."); } delay(2000); lcd.setCursor(0, 0); lcd.print("WiFi verbunden ..."); /* Zeitzone CEST: Central European Summertime Beginn europäische Sommerzeit letzter Sonntag im März 2 Uhr GMT + 2 Stunden */ ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // CET: Central European Time // Beginn Normalzeit letzter Sonntag im Oktober 3 Uhr GMT + 1 Stunde ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // ntp starten Zeitserver deutscher NTP-Pool ntp.begin("de.pool.ntp.org"); ntp.updateInterval(10000); lcd.setCursor(0, 1); lcd.print(WiFi.SSID()); lcd.setCursor(0, 2); lcd.print(String(WiFi.localIP().toString())); if (ntp.update()) { lcd.setCursor(0, 3); lcd.print(String(ntp.formattedTime("%H:%M:%S"))); } delay(5000); } void loop() { int AnzahlVersuche = 0; HTTPClient http; bool Verbindung = false; HTTPClient httpWetter; // Aufruf http.begin für ESP8266 und ESP unterschiedlich #ifdef ESP32 http.begin(WetterDaten); #else http.begin(Klient, WetterDaten); #endif int httpCode = http.GET(); // Verbindung prüfen if (WiFiNetzwerke.run() == WL_CONNECTED) { ntp.update(); Verbindung = false; AnzahlVersuche = 0; // Meteo kontaktieren while (!Verbindung) { httpCode = http.GET(); if (httpCode > 0) Verbindung = true; // Server antwortet nach 5 Versuchen nicht AnzahlVersuche++; if (AnzahlVersuche > 5) break; } // Verbindung erfolgreich if (Verbindung) { String ServerAntwort = http.getString(); JsonDocument JSONDaten; deserializeJson(JSONDaten, ServerAntwort); // float-Daten sammeln float aktuelleTemperatur = JSONDaten["current"]["temperature_2m"]; float RegenMenge = JSONDaten["current"]["rain"]; String Sonnenaufgang = JSONDaten["daily"]["sunrise"][0]; String Sonnenuntergang = JSONDaten["daily"]["sunset"][0]; // . durch , ersetzen auf 1 Nachkommastelle kürzen String AnzeigeAktuelleTemperatur = String(aktuelleTemperatur, 1); AnzeigeAktuelleTemperatur.replace(".", ","); String Regen = String(RegenMenge, 1); Regen.replace(".", ","); lcd.clear(); lcd.setCursor(0, 0); lcd.print(Stadt); lcd.setCursor(0, 1); lcd.print("Messung: " + String(JSONDaten["current"]["time"]).substring(11, String(JSONDaten["current"]["time"]).length())) + " Uhr"; lcd.setCursor(0, 2); lcd.print(AnzeigeAktuelleTemperatur + "°"); lcd.setCursor(7, 2); lcd.print(String(int(JSONDaten["current"]["surface_pressure"])) + " hPa"); lcd.setCursor(16, 2); lcd.print(String(JSONDaten["current"]["relative_humidity_2m"]) + "%"); lcd.setCursor(0, 3); lcd.print(Regen + " mm"); lcd.setCursor(8, 3); // zwischen 0 und 11 Uhr -> Sonnenaufgang anzeigen // zwischen 12 und 23 Uhr -> Sonnenuntergang anzeigen Sonnenuntergang = Sonnenuntergang.substring(11, Sonnenuntergang.length()); Sonnenaufgang = Sonnenaufgang.substring(11, Sonnenaufgang.length()); switch (ntp.hours()) { case 0 ... 11: lcd.print("SA: "); lcd.print(Sonnenaufgang); break; case 12 ... 23: lcd.print("SU: "); lcd.print(Sonnenuntergang); break; } http.end(); delay(Intervall); } } // keine WiFi-Verbindung else { if (WiFiNetzwerke.run() == WL_CONNECTED) { delay(200); } } } |
Letzte Aktualisierung: