/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: ESP32 Wetterdaten OLED-Display 1.3" ************************************************************************************************** Server-Abfrage: http://api.weatherstack.com/current?access_key=c161b8689410c8488ff843ae06af29e7&query=Tegernsee ************************************************************************************************** Version: 07.07.2021 ************************************************************************************************** Board: ESP32vn IoT UNO V1.0.4 ************************************************************************************************** C++ Arduino IDE V1.8.13 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json **************************************************************************************************/ #include #include #include const char* ssid = "xxx"; const char* password = "xxx"; String openWeatherMapApiKey = "Hier deine API eintragen"; String city = "Tegernsee"; String countryCode = "DE"; unsigned long lastTime = 0; // Timer set to 10 Minuten (600000) //unsigned long timerDelay = 600000; // Set timer to 10 Sekunden (10000) unsigned long timerDelay = 10000; String jsonBuffer; //----------------------------------------------- #include #include #ifdef U8X8_HAVE_HW_SPI #include #endif #ifdef U8X8_HAVE_HW_I2C #include #endif U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); unsigned long delayTime; //----------------------------------------------- void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.println("Verbinde"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Verbunden mit IP Addresse: "); Serial.println(WiFi.localIP()); Serial.println("Aktualisierung ist auf 10 Sekunden eingestellt."); //----------------------------------------------- u8g2.begin(); //----------------------------------------------- } void loop() { if ((millis() - lastTime) > timerDelay) { // Check WiFi Status if(WiFi.status()== WL_CONNECTED){ String serverPath = "http://api.weatherstack.com/current?access_key=c161b8689410c8488ff843ae06af29e7&query=Tegernsee"; jsonBuffer = httpGETRequest(serverPath.c_str()); Serial.println(jsonBuffer); JSONVar myObject = JSON.parse(jsonBuffer); if (JSON.typeof(myObject) == "undefined") { Serial.println("Eingabe fehlgeschlagen!"); return; } Serial.print("JSON object = "); Serial.println("Meine Wetterdaten:"); Serial.print("Ort: "); Serial.println(myObject["location"]["name"]); Serial.print("Aktuelle Temperatur: "); Serial.print(myObject["current"]["temperature"]); Serial.println("°C"); Serial.print("Luftdruck: "); Serial.print(myObject["current"]["pressure"]); Serial.println("hPa"); Serial.print("Luftfeuchte: "); Serial.print(myObject["current"]["humidity"]); Serial.println("%"); Serial.print("Bewölkung: "); Serial.print(myObject["current"]["cloudcover"]); Serial.println("%"); Serial.print("Windrichtung: "); Serial.println(myObject["current"]["wind_dir"]); //--------------------------------------------------------- Serial.println("================================="); u8g2.setFont(u8g2_font_courR10_tf); u8g2.firstPage(); do { u8g2.setCursor(12, 9); u8g2.print("Wetterdaten"); u8g2.setCursor(18, 22); u8g2.print("Tegernsee"); u8g2.setCursor(2, 35); u8g2.print("Temp: "); u8g2.setCursor(51, 35); u8g2.print(myObject["current"]["temperature"]); u8g2.print("\xB0""C"); u8g2.setCursor(2, 48); u8g2.print("Wind:"); u8g2.setCursor(51, 48); u8g2.print(myObject["current"]["wind_speed"]); u8g2.print("m/s"); /* u8g2.setCursor(2, 48); u8g2.print("LuFe:"); u8g2.setCursor(51, 48); u8g2.print(myObject["main"]["humidity"]); u8g2.print("%"); */ u8g2.setCursor(2, 63); u8g2.print("LuDr:"); u8g2.setCursor(51, 63); u8g2.print(myObject["current"]["pressure"]); u8g2.print("hPa"); } while ( u8g2.nextPage() ); delay(delayTime); //--------------------------------------------------------- } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } } String httpGETRequest(const char* serverName) { WiFiClient client; HTTPClient http; // Domain-Name http.begin(client, serverName); // HTTP POST request 200=OK int httpResponseCode = http.GET(); String payload = "{}"; if (httpResponseCode>0) { Serial.print("HTTP-Antwort Code (200=OK): "); Serial.println(httpResponseCode); Serial.println("Server-Meldung: "); payload = http.getString(); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } http.end(); return payload; }