/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: ESP32 WWetterdaten ************************************************************************************************** Version: 05.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 deinen 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; 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("Timer ist auf 10 Sekunden eingestellt."); } void loop() { if ((millis() - lastTime) > timerDelay) { // Check WiFi Status if(WiFi.status()== WL_CONNECTED){ String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey; jsonBuffer = httpGETRequest(serverPath.c_str()); Serial.println(jsonBuffer); JSONVar myObject = JSON.parse(jsonBuffer); if (JSON.typeof(myObject) == "undefined") { Serial.println("Eingabe fehlgeschlagen!"); return; } ktemp=(myObject["main"]["temp"]); // Serial.print("JSON object = "); Serial.println("Meine Wetterdaten"); Serial.print("Ort: "); Serial.println(myObject["name"]); Serial.print("Temperatur: "); // Serial.println(myObject["main"]["temp"]); Serial.print(ktemp-273); Serial.println(" °C "); Serial.print("Luftdruck: "); Serial.print(myObject["main"]["pressure"]); Serial.println(" hPa"); Serial.print("Luftfeuchte: "); Serial.print(myObject["main"]["humidity"]); Serial.println(" % "); Serial.print("Wind Geschwindigkeit: "); Serial.print(myObject["wind"]["speed"]); Serial.println(" m/s "); } 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: "); Serial.println(httpResponseCode); payload = http.getString(); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } http.end(); return payload; }