Skip to content

Commit 0082733

Browse files
committed
Fix connection validation on unknown indexes
Connection with Index 1 and 2 were supposed valid when not present in the configuration, generating attempts to connect with those indexes. And improve erase config by adding an erase when the config file is not present (ESP is connected to WiFi without config file). This way the ESP will not reconnect to the WiFi AP
1 parent 88e0909 commit 0082733

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

main/User_config.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ const char* OTAserver_cert = "";
257257
# define MQTT_SECURE_SIGNED_CLIENT 0 // If using a signed certificate for the broker and using client certificate/key set this to true or 1
258258
#endif
259259

260-
#ifndef CNT_DEFAULT_INDEX
261-
# define CNT_DEFAULT_INDEX 0 // Default set of connection parameters
262-
#endif
263-
264260
#ifdef PRIVATE_CERTS
265261
# include "certs/private_client_cert.h"
266262
# include "certs/private_client_key.h"
@@ -273,6 +269,10 @@ const char* OTAserver_cert = "";
273269

274270
#include <string>
275271

272+
#ifndef CNT_DEFAULT_INDEX
273+
# define CNT_DEFAULT_INDEX 0 // Default set of connection parameters
274+
#endif
275+
276276
#if !MQTT_BROKER_MODE
277277
struct ss_cnt_parameters {
278278
std::string server_cert;
@@ -288,9 +288,6 @@ struct ss_cnt_parameters {
288288
bool validConnection;
289289
};
290290

291-
// Index 0 is used for connection parameters provided in the build that can be overloaded by WiFi Manager/Onboarding/WebUI,MQTT
292-
# define CNT_DEFAULT_INDEX 0
293-
// Index 1 and more are used for connection parameters provided at runtime by MQTT
294291
# define cnt_parameters_array_size 3
295292

296293
ss_cnt_parameters cnt_parameters_array[cnt_parameters_array_size] = {

main/ZwebUI.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ QueueHandle_t webUIQueue;
4646
WebServer server(80);
4747

4848
/*------------------- External functions ----------------------*/
49-
extern void eraseAndRestart();
49+
extern void erase(bool restart);
5050
extern unsigned long uptime();
5151

5252
/*------------------- Web Console Globals ----------------------*/
@@ -1237,7 +1237,7 @@ void handleRT() {
12371237
response += String(buffer);
12381238
server.send(200, "text/html", response);
12391239

1240-
eraseAndRestart();
1240+
erase(true);
12411241
} else {
12421242
handleCN();
12431243
}

main/main.ino

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ void setupMQTT() {
10291029

10301030
if (cnt_parameters_backup) {
10311031
// this was the first attempt to connect to a new server and it succeeded
1032-
Log.notice(F("MQTT connection successful" CR));
1032+
Log.notice(F("MQTT connection parameters %d successful" CR), cnt_index);
10331033
cnt_parameters_array[cnt_index].validConnection = true;
10341034
readCntParameters(cnt_index);
10351035

@@ -1754,6 +1754,7 @@ void setupTLS(int index) {
17541754

17551755
/*
17561756
Reboot for Reason Codes
1757+
0 - Erase and Restart
17571758
1 - Repeated MQTT Connection Failure
17581759
2 - Repeated WiFi Connection Failure
17591760
3 - Failed WiFiManager configuration portal
@@ -1863,7 +1864,7 @@ void blockingWaitForReset() {
18631864
Log.trace(F("Trigger button Pressed" CR));
18641865
delay(3000); // reset delay hold
18651866
if (digitalRead(TRIGGER_GPIO) == LOW) {
1866-
Log.trace(F("Button Held" CR));
1867+
Log.notice(F("Button Held" CR));
18671868
// Switching off the relay during reset or failsafe operations
18681869
# ifdef ZactuatorONOFF
18691870
uint8_t level = digitalRead(ACTUATOR_ONOFF_GPIO);
@@ -1877,7 +1878,10 @@ void blockingWaitForReset() {
18771878
Log.trace(F("mounted file system" CR));
18781879
if (SPIFFS.exists("/config.json")) {
18791880
Log.notice(F("Erasing ESP Config, restarting" CR));
1880-
setupWiFiManager(true);
1881+
erase(true);
1882+
} else {
1883+
Log.notice(F("Erasing ESP Config, without restart" CR));
1884+
erase(false);
18811885
}
18821886
}
18831887
delay(30000);
@@ -1887,6 +1891,7 @@ void blockingWaitForReset() {
18871891
failSafeMode = true;
18881892
setupWiFiManager(false);
18891893
}
1894+
ESPRestart(5);
18901895
}
18911896
}
18921897
}
@@ -2114,8 +2119,9 @@ bool loadConfigFromFlash() {
21142119
strcat(key, index_suffix);
21152120
if (json.containsKey(key)) {
21162121
cnt_parameters_array[i].validConnection = json[key].as<bool>();
2117-
} else {
2118-
// For backward compatibility, if valid_cnt is not found, we assume the connection is valid
2122+
} else if (i == CNT_DEFAULT_INDEX) {
2123+
// For backward compatibility, if valid_cnt is not found, we assume the connection is valid for CNT_DEFAULT_INDEX
2124+
Log.warning(F("valid_cnt not found, assuming connection is valid" CR));
21192125
cnt_parameters_array[i].validConnection = true;
21202126
}
21212127
}
@@ -2170,7 +2176,7 @@ void setupWiFiManager(bool reset_settings) {
21702176
WiFi.mode(WIFI_STA);
21712177

21722178
if (reset_settings)
2173-
eraseAndRestart();
2179+
erase(true);
21742180

21752181
# ifdef USE_MAC_AS_GATEWAY_NAME
21762182
String s = WiFi.macAddress();
@@ -2374,9 +2380,6 @@ void setup_ethernet_esp32() {
23742380
# endif
23752381
if (ethBeginSuccess) {
23762382
Log.notice(F("Ethernet started" CR));
2377-
Log.notice(F("OpenMQTTGateway MAC: %s" CR), ETH.macAddress().c_str());
2378-
Log.notice(F("OpenMQTTGateway IP: %s" CR), ETH.localIP().toString().c_str());
2379-
Log.notice(F("OpenMQTTGateway link speed: %d Mbps" CR), ETH.linkSpeed());
23802383
while (!ethConnected && failure_number_ntwk <= maxConnectionRetryNetwork) {
23812384
delay(500);
23822385
Log.notice(F("." CR));
@@ -2398,9 +2401,9 @@ void WiFiEvent(WiFiEvent_t event) {
23982401
Log.notice(F("Ethernet Connected" CR));
23992402
break;
24002403
case ARDUINO_EVENT_ETH_GOT_IP:
2401-
Log.trace(F("OpenMQTTGateway MAC: %s" CR), ETH.macAddress().c_str());
2402-
Log.trace(F("OpenMQTTGateway IP: %s" CR), ETH.localIP().toString().c_str());
2403-
Log.trace(F("OpenMQTTGateway link speed: %d Mbps" CR), ETH.linkSpeed());
2404+
Log.notice(F("OpenMQTTGateway Ethernet MAC: %s" CR), ETH.macAddress().c_str());
2405+
Log.notice(F("OpenMQTTGateway Ethernet IP: %s" CR), ETH.localIP().toString().c_str());
2406+
Log.notice(F("OpenMQTTGateway Ethernet link speed: %d Mbps" CR), ETH.linkSpeed());
24042407
gatewayState = GatewayState::NTWK_CONNECTED;
24052408
ethConnected = true;
24062409
break;
@@ -2725,7 +2728,7 @@ float intTemperatureRead() {
27252728
/*
27262729
Erase flash and restart the ESP
27272730
*/
2728-
void eraseAndRestart() {
2731+
void erase(bool restart) {
27292732
Log.trace(F("Formatting requested, result: %d" CR), SPIFFS.format());
27302733

27312734
#if defined(ESP8266)
@@ -2734,12 +2737,11 @@ void eraseAndRestart() {
27342737
wifiManager.resetSettings();
27352738
# endif
27362739
delay(5000);
2737-
ESP.reset();
27382740
#else
2739-
//esp_task_wdt_delete(NULL);
27402741
nvs_flash_erase();
2741-
ESP.restart();
27422742
#endif
2743+
if (restart)
2744+
ESPRestart(0);
27432745
}
27442746

27452747
String stateMeasures() {

0 commit comments

Comments
 (0)