[SYS] Fix connection validation on unknown indexes and improve erase method (#2075)

Before this fix, connections with Index 1 and 2 were supposed valid when not present in the configuration, generating attempts to connect with those indexes.
Also improve the erase config by adding an erase when the config file is not present (ESP is connected to WiFi without the config file). This way the ESP will not reconnect to the WiFi AP.

Co-authored-by: Florian <1technophile@users.noreply.github.com>
This commit is contained in:
FlorianandFlorian authored and GitHub committed 2024-10-01 11:48:39 -05:00
1 parent 9e5f56580d
commit c234afa9c8
3 files changed
+24 -25

No files matched your search

+4 -7
View File
@@ -257,10 +257,6 @@ const char* OTAserver_cert = "";
# 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 # 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
#endif #endif
#ifndef CNT_DEFAULT_INDEX
# define CNT_DEFAULT_INDEX 0 // Default set of connection parameters
#endif
#ifdef PRIVATE_CERTS #ifdef PRIVATE_CERTS
# include "certs/private_client_cert.h" # include "certs/private_client_cert.h"
# include "certs/private_client_key.h" # include "certs/private_client_key.h"
@@ -273,6 +269,10 @@ const char* OTAserver_cert = "";
#include <string> #include <string>
#ifndef CNT_DEFAULT_INDEX
# define CNT_DEFAULT_INDEX 0 // Default set of connection parameters
#endif
#if !MQTT_BROKER_MODE #if !MQTT_BROKER_MODE
struct ss_cnt_parameters { struct ss_cnt_parameters {
std::string server_cert; std::string server_cert;
@@ -288,9 +288,6 @@ struct ss_cnt_parameters {
bool validConnection; bool validConnection;
}; };
// Index 0 is used for connection parameters provided in the build that can be overloaded by WiFi Manager/Onboarding/WebUI,MQTT
# define CNT_DEFAULT_INDEX 0
// Index 1 and more are used for connection parameters provided at runtime by MQTT
# define cnt_parameters_array_size 3 # define cnt_parameters_array_size 3
ss_cnt_parameters cnt_parameters_array[cnt_parameters_array_size] = { ss_cnt_parameters cnt_parameters_array[cnt_parameters_array_size] = {
+2 -2
View File
@@ -46,7 +46,7 @@ QueueHandle_t webUIQueue;
WebServer server(80); WebServer server(80);
/*------------------- External functions ----------------------*/ /*------------------- External functions ----------------------*/
extern void eraseAndRestart(); extern void erase(bool restart);
extern unsigned long uptime(); extern unsigned long uptime();
/*------------------- Web Console Globals ----------------------*/ /*------------------- Web Console Globals ----------------------*/
@@ -1237,7 +1237,7 @@ void handleRT() {
response += String(buffer); response += String(buffer);
server.send(200, "text/html", response); server.send(200, "text/html", response);
eraseAndRestart(); erase(true);
} else { } else {
handleCN(); handleCN();
} }
+18 -16
View File
@@ -1029,7 +1029,7 @@ void setupMQTT() {
if (cnt_parameters_backup) { if (cnt_parameters_backup) {
// this was the first attempt to connect to a new server and it succeeded // this was the first attempt to connect to a new server and it succeeded
Log.notice(F("MQTT connection successful" CR)); Log.notice(F("MQTT connection parameters %d successful" CR), cnt_index);
cnt_parameters_array[cnt_index].validConnection = true; cnt_parameters_array[cnt_index].validConnection = true;
readCntParameters(cnt_index); readCntParameters(cnt_index);
@@ -1754,6 +1754,7 @@ void setupTLS(int index) {
/* /*
Reboot for Reason Codes Reboot for Reason Codes
0 - Erase and Restart
1 - Repeated MQTT Connection Failure 1 - Repeated MQTT Connection Failure
2 - Repeated WiFi Connection Failure 2 - Repeated WiFi Connection Failure
3 - Failed WiFiManager configuration portal 3 - Failed WiFiManager configuration portal
@@ -1863,7 +1864,7 @@ void blockingWaitForReset() {
Log.trace(F("Trigger button Pressed" CR)); Log.trace(F("Trigger button Pressed" CR));
delay(3000); // reset delay hold delay(3000); // reset delay hold
if (digitalRead(TRIGGER_GPIO) == LOW) { if (digitalRead(TRIGGER_GPIO) == LOW) {
Log.trace(F("Button Held" CR)); Log.notice(F("Button Held" CR));
// Switching off the relay during reset or failsafe operations // Switching off the relay during reset or failsafe operations
# ifdef ZactuatorONOFF # ifdef ZactuatorONOFF
uint8_t level = digitalRead(ACTUATOR_ONOFF_GPIO); uint8_t level = digitalRead(ACTUATOR_ONOFF_GPIO);
@@ -1877,7 +1878,10 @@ void blockingWaitForReset() {
Log.trace(F("mounted file system" CR)); Log.trace(F("mounted file system" CR));
if (SPIFFS.exists("/config.json")) { if (SPIFFS.exists("/config.json")) {
Log.notice(F("Erasing ESP Config, restarting" CR)); Log.notice(F("Erasing ESP Config, restarting" CR));
setupWiFiManager(true); erase(true);
} else {
Log.notice(F("Erasing ESP Config, without restart" CR));
erase(false);
} }
} }
delay(30000); delay(30000);
@@ -1887,6 +1891,7 @@ void blockingWaitForReset() {
failSafeMode = true; failSafeMode = true;
setupWiFiManager(false); setupWiFiManager(false);
} }
ESPRestart(5);
} }
} }
} }
@@ -2114,8 +2119,9 @@ bool loadConfigFromFlash() {
strcat(key, index_suffix); strcat(key, index_suffix);
if (json.containsKey(key)) { if (json.containsKey(key)) {
cnt_parameters_array[i].validConnection = json[key].as<bool>(); cnt_parameters_array[i].validConnection = json[key].as<bool>();
} else { } else if (i == CNT_DEFAULT_INDEX) {
// For backward compatibility, if valid_cnt is not found, we assume the connection is valid // For backward compatibility, if valid_cnt is not found, we assume the connection is valid for CNT_DEFAULT_INDEX
Log.warning(F("valid_cnt not found, assuming connection is valid" CR));
cnt_parameters_array[i].validConnection = true; cnt_parameters_array[i].validConnection = true;
} }
} }
@@ -2170,7 +2176,7 @@ void setupWiFiManager(bool reset_settings) {
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
if (reset_settings) if (reset_settings)
eraseAndRestart(); erase(true);
# ifdef USE_MAC_AS_GATEWAY_NAME # ifdef USE_MAC_AS_GATEWAY_NAME
String s = WiFi.macAddress(); String s = WiFi.macAddress();
@@ -2374,9 +2380,6 @@ void setup_ethernet_esp32() {
# endif # endif
if (ethBeginSuccess) { if (ethBeginSuccess) {
Log.notice(F("Ethernet started" CR)); Log.notice(F("Ethernet started" CR));
Log.notice(F("OpenMQTTGateway MAC: %s" CR), ETH.macAddress().c_str());
Log.notice(F("OpenMQTTGateway IP: %s" CR), ETH.localIP().toString().c_str());
Log.notice(F("OpenMQTTGateway link speed: %d Mbps" CR), ETH.linkSpeed());
while (!ethConnected && failure_number_ntwk <= maxConnectionRetryNetwork) { while (!ethConnected && failure_number_ntwk <= maxConnectionRetryNetwork) {
delay(500); delay(500);
Log.notice(F("." CR)); Log.notice(F("." CR));
@@ -2398,9 +2401,9 @@ void WiFiEvent(WiFiEvent_t event) {
Log.notice(F("Ethernet Connected" CR)); Log.notice(F("Ethernet Connected" CR));
break; break;
case ARDUINO_EVENT_ETH_GOT_IP: case ARDUINO_EVENT_ETH_GOT_IP:
Log.trace(F("OpenMQTTGateway MAC: %s" CR), ETH.macAddress().c_str()); Log.notice(F("OpenMQTTGateway Ethernet MAC: %s" CR), ETH.macAddress().c_str());
Log.trace(F("OpenMQTTGateway IP: %s" CR), ETH.localIP().toString().c_str()); Log.notice(F("OpenMQTTGateway Ethernet IP: %s" CR), ETH.localIP().toString().c_str());
Log.trace(F("OpenMQTTGateway link speed: %d Mbps" CR), ETH.linkSpeed()); Log.notice(F("OpenMQTTGateway Ethernet link speed: %d Mbps" CR), ETH.linkSpeed());
gatewayState = GatewayState::NTWK_CONNECTED; gatewayState = GatewayState::NTWK_CONNECTED;
ethConnected = true; ethConnected = true;
break; break;
@@ -2725,7 +2728,7 @@ float intTemperatureRead() {
/* /*
Erase flash and restart the ESP Erase flash and restart the ESP
*/ */
void eraseAndRestart() { void erase(bool restart) {
Log.trace(F("Formatting requested, result: %d" CR), SPIFFS.format()); Log.trace(F("Formatting requested, result: %d" CR), SPIFFS.format());
#if defined(ESP8266) #if defined(ESP8266)
@@ -2734,12 +2737,11 @@ void eraseAndRestart() {
wifiManager.resetSettings(); wifiManager.resetSettings();
# endif # endif
delay(5000); delay(5000);
ESP.reset();
#else #else
//esp_task_wdt_delete(NULL);
nvs_flash_erase(); nvs_flash_erase();
ESP.restart();
#endif #endif
if (restart)
ESPRestart(0);
} }
String stateMeasures() { String stateMeasures() {