Hallo Zusammen
Ich hole via API das aktuelle Wetter via OpenWeather. Am Anfang läuft der Code auch sauber durch aber mit der Zeit spuckt er mir immer ein: Error on HTTP request: -1 heraus.
Kann sich das jemand erklären? Starte ich den ESP8266 neu, funktioniert es schon wieder. Der WLan Status und der Heap-Speicher sehen auch in Ordnung aus.
Vielen Dank und ein schönes Wochenende.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
WiFiClient wifiClient;
const char* ssid = "***";
const char* password = "***";
IPAddress staticIP(192, 168, 50, 6); // statische IP
IPAddress gateway(192, 168, 50, 1); // Gateway
IPAddress subnet(255, 255, 255, 0); // Subnetzmaske
IPAddress dns(192, 168, 178, 34); // DNS-Server
const char* host = "api.openweathermap.org";
const char* apiKey = "***";
const char* city = "***";
const char* country = "***";
unsigned long previousMillis = 0; // will store the last time the weather data was updated
const long interval = 240000; // interval to get weather data (5 minutes)
String translateWeather(String weather) {
//.....
}
void setup() {
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(staticIP, gateway, subnet, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
// Set connection and response timeouts
http.setTimeout(15000); // 10 seconds
String serverPath = "http://" + String(host) + "/data/2.5/weather?q=" + city + "," + country + "&appid=" + apiKey;
// Your Domain name with URL path or IP address with path
http.begin(wifiClient, serverPath);
// Send HTTP POST request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString(); //Get the response to the request
// Serial.println("Response: " + response);
DynamicJsonDocument doc(8192);
DeserializationError error = deserializeJson(doc, response);
if (error) {
Serial.println(F("Deserialization failed"));
return;
}
float temp = doc["main"]["temp"].as<float>() - 273.15;
int humidity = doc["main"]["humidity"];
String weather = doc["weather"][0]["description"];
Serial.println("Temperatur: " + String(temp));
Serial.println("Feuchtigkeit: " + String(humidity));
String weather_de = translateWeather(weather);
Serial.println("Wetter: " + weather_de);
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode)); // Print the error message for the returned code
Serial.println();
Serial.println(WiFi.status());
Serial.println(ESP.getFreeHeap());
Serial.println();
}
// Free resources
http.end();
} else {
Serial.println("WiFi Disconnected");
// Reconnect to WiFi
WiFi.begin(ssid, password);
}
}
}