diff --git a/Sensorbox2_ESP/include/network.h b/Sensorbox2_ESP/include/network.h index c071f1f..178b6eb 100644 --- a/Sensorbox2_ESP/include/network.h +++ b/Sensorbox2_ESP/include/network.h @@ -69,8 +69,10 @@ class MQTTclient_t { default_opts.qos = 0; default_opts.retain = false; } + void disconnect(void) { mg_mqtt_disconnect(s_conn, &default_opts); }; #else void connect(void); + void disconnect(void) { esp_mqtt_client_stop(client); }; #endif void publish(const String &topic, const int32_t &payload, bool retained, int qos) { publish(topic, String(payload), retained, qos); }; void publish(const String &topic, const String &payload, bool retained, int qos); @@ -107,7 +109,6 @@ extern void network_loop(void); extern String APhostname; extern webServerRequest* request; extern struct mg_mgr mgr; -extern String TZinfo; extern uint8_t WIFImode; extern char *downloadUrl; extern uint32_t serialnr; diff --git a/Sensorbox2_ESP/src/main.cpp b/Sensorbox2_ESP/src/main.cpp index f023b4f..442461f 100644 --- a/Sensorbox2_ESP/src/main.cpp +++ b/Sensorbox2_ESP/src/main.cpp @@ -71,7 +71,6 @@ #include extern struct tm timeinfo; -extern String TZinfo; //String APhostname = "SmartEVSE-" + String( MacId() & 0xffff, 10); // SmartEVSE access point Name = SmartEVSE-xxxxx extern String APhostname; @@ -228,11 +227,6 @@ void read_settings(bool write) { if (preferences.begin("settings", false) == true) { WIFImode = preferences.getUChar("WIFImode", WIFI_MODE); SmartEVSEHost = preferences.getString("SmartEVSEHost", ""); - TZinfo = preferences.getString("TimezoneInfo",""); - if (TZinfo != "") { - setenv("TZ",TZinfo.c_str(),1); - tzset(); - } preferences.end(); if (write) write_settings(); diff --git a/Sensorbox2_ESP/src/network.cpp b/Sensorbox2_ESP/src/network.cpp index 7e1e9ac..7f15341 100644 --- a/Sensorbox2_ESP/src/network.cpp +++ b/Sensorbox2_ESP/src/network.cpp @@ -659,16 +659,35 @@ void RunFirmwareUpdate(void) { void setTimeZone(void * parameter) { HTTPClient httpClient; + //we use lambda function because normal function collides with HTTPClient class + auto onErrorCloseTask = [&httpClient]() { + _LOG_A("Could not detect timezone, set it to CEST and retry next reboot.\n"); + setenv("TZ","CET-1CEST,M3.5.0,M10.5.0/3",1); // CEST tzinfo string + tzset(); + httpClient.end(); + vTaskDelete(NULL); //end this task so it will not take up resources + }; // lookup current timezone httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); - httpClient.begin("http://worldtimeapi.org/api/ip"); - int httpCode = httpClient.GET(); //Make the request + String host[2] = { "http://worldtimeapi.org/api/ip", "http://ip-api.com/json"}; + int httpCode; + for (int i=0; i<15; i++) { + httpClient.begin(host[i%2]); + httpCode = httpClient.GET(); //Make the request + // only handle 200/301, fail on everything else + if ( httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY ) { //fail + httpClient.end(); + _LOG_A("Error on HTTP request (httpCode=%i), host=%s, try=%i.\n", httpCode, host[i%2].c_str(), i); + delay(1000); + } else { + break; + } + } // only handle 200/301, fail on everything else if( httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY ) { _LOG_A("Error on HTTP request (httpCode=%i)\n", httpCode); - httpClient.end(); - vTaskDelete(NULL); //end this task so it will not take up resources + onErrorCloseTask(); } // The filter: it contains "true" for each value we want to keep @@ -676,16 +695,16 @@ void setTimeZone(void * parameter) { filter["timezone"] = true; DynamicJsonDocument doc2(80); DeserializationError error = deserializeJson(doc2, httpClient.getStream(), DeserializationOption::Filter(filter)); - httpClient.end(); if (error) { _LOG_A("deserializeJson() failed: %s\n", error.c_str()); - vTaskDelete(NULL); //end this task so it will not take up resources + onErrorCloseTask(); } String tzname = doc2["timezone"]; if (tzname == "") { _LOG_A("Could not detect Timezone.\n"); - vTaskDelete(NULL); //end this task so it will not take up resources + onErrorCloseTask(); } + httpClient.end(); _LOG_A("Timezone detected: tz=%s.\n", tzname.c_str()); // takes TZname (format: Europe/Berlin) , gets TZ_INFO (posix string, format: CET-1CEST,M3.5.0,M10.5.0/3) and sets and stores timezonestring accordingly @@ -699,10 +718,9 @@ void setTimeZone(void * parameter) { // only handle 200/301, fail on everything else if( httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY ) { - _LOG_A("Error on HTTP request (httpCode=%i)\n", httpCode); - httpClient.end(); + _LOG_A("Error on zones.csv HTTP request (httpCode=%i)\n", httpCode); FREE(URL); - vTaskDelete(NULL); //end this task so it will not take up resources + onErrorCloseTask(); } stream = httpClient.getStreamPtr(); @@ -721,6 +739,11 @@ void setTimeZone(void * parameter) { break; } } + if (TZinfo == "") { + _LOG_A("Could not find TZname %s in zones.csv.\n", tzname.c_str()); + FREE(URL); + onErrorCloseTask(); + } httpClient.end(); FREE(URL); vTaskDelete(NULL); //end this task so it will not take up resources @@ -1037,9 +1060,6 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) { if(request->hasParam("mqtt_host")) { MQTTHost = request->getParam("mqtt_host")->value(); -#if MQTT_ESP == 1 - MQTTclient.connect(); -#endif doc["mqtt_host"] = MQTTHost; } @@ -1072,6 +1092,13 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) { } doc["mqtt_password_set"] = (MQTTpassword != ""); } + + // disconnect mqtt so it will automatically reconnect with then new params + MQTTclient.disconnect(); +#if MQTT_ESP == 1 + MQTTclient.connect(); +#endif + if (preferences.begin("settings", false) ) { preferences.putString("MQTTpassword", MQTTpassword); preferences.putString("MQTTuser", MQTTuser); @@ -1322,7 +1349,6 @@ void WiFiSetup(void) { WiFi.setAutoReconnect(true); //actually does nothing since this is the default value //WiFi.persistent(true); WiFi.onEvent(onWifiEvent); - handleWIFImode(); //go into the mode that was saved in nonvolatile memory // Init and get the time // First option to get time from local ntp server blocks the second fallback option since 2021: @@ -1341,10 +1367,12 @@ void WiFiSetup(void) { #endif if (preferences.begin("settings", false) ) { -#if MQTT == 0 - preferences.end(); - } -#else + TZinfo = preferences.getString("TimezoneInfo",""); + if (TZinfo != "") { + setenv("TZ",TZinfo.c_str(),1); + tzset(); + } +#if MQTT == 1 MQTTpassword = preferences.getString("MQTTpassword"); MQTTuser = preferences.getString("MQTTuser"); #ifdef SENSORBOX_VERSION @@ -1354,14 +1382,16 @@ void WiFiSetup(void) { #endif MQTTHost = preferences.getString("MQTTHost", ""); MQTTPort = preferences.getUShort("MQTTPort", 1883); +#endif //MQTT preferences.end(); } -#if MQTT_ESP == 1 + handleWIFImode(); //go into the mode that was saved in nonvolatile memory + +#if MQTT ==1 && MQTT_ESP == 1 MQTTclient.connect(); #endif -#endif //MQTT }