Skip to content

Commit

Permalink
main.cpp, network.h, network.cpp: resync with SmartEVSEv3
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo35 committed Nov 30, 2024
1 parent 7c5ff70 commit 1fbca5e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Sensorbox2_ESP/include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions Sensorbox2_ESP/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include <HTTPClient.h>

extern struct tm timeinfo;
extern String TZinfo;

//String APhostname = "SmartEVSE-" + String( MacId() & 0xffff, 10); // SmartEVSE access point Name = SmartEVSE-xxxxx
extern String APhostname;
Expand Down Expand Up @@ -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();
Expand Down
70 changes: 50 additions & 20 deletions Sensorbox2_ESP/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,33 +659,52 @@ 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
DynamicJsonDocument filter(16);
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
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
}


Expand Down

0 comments on commit 1fbca5e

Please sign in to comment.