diff --git a/main/User_config.h b/main/User_config.h index 59f76d7..d0a72d6 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -62,6 +62,7 @@ #define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point #define WifiManager_ssid "OpenMQTTGateway" //this is the network name of the initial setup access point #define WifiManager_ConfigPortalTimeOut 120 +#define WifiManager_TimeOut 5 /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ //MQTT Parameters definition diff --git a/main/main.ino b/main/main.ino index 85c6a7c..9dbba18 100644 --- a/main/main.ino +++ b/main/main.ino @@ -462,7 +462,7 @@ void setup() { //Launch serial for debugging purposes Serial.begin(SERIAL_BAUD); - + #if defined(ESP8266) || defined(ESP32) #ifdef ESP8266 @@ -662,35 +662,35 @@ void setup_wifimanager(bool reset_settings){ if (SPIFFS.begin()) { trc(F("mounted file system")); - if (SPIFFS.exists("/config.json")) { - //file exists, reading and loading - trc(F("reading config file")); - File configFile = SPIFFS.open("/config.json", "r"); - if (configFile) { - trc(F("opened config file")); - size_t size = configFile.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - json.printTo(Serial); - if (json.success()) { - trc(F("\nparsed json")); - - strcpy(mqtt_server, json["mqtt_server"]); - strcpy(mqtt_port, json["mqtt_port"]); - strcpy(mqtt_user, json["mqtt_user"]); - strcpy(mqtt_pass, json["mqtt_pass"]); - - } else { - trc(F("failed to load json config")); - } + }else{ + trc(F("failed to mount FS -> formating")); + SPIFFS.format(); + if(SPIFFS.begin()) + trc(F("mounted file system after formating")); + } + if (SPIFFS.exists("/config.json")) { + //file exists, reading and loading + trc(F("reading config file")); + File configFile = SPIFFS.open("/config.json", "r"); + if (configFile) { + trc(F("opened config file")); + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject& json = jsonBuffer.parseObject(buf.get()); + json.printTo(Serial); + if (json.success()) { + trc(F("\nparsed json")); + strcpy(mqtt_server, json["mqtt_server"]); + strcpy(mqtt_port, json["mqtt_port"]); + strcpy(mqtt_user, json["mqtt_user"]); + strcpy(mqtt_pass, json["mqtt_pass"]); + } else { + trc(F("failed to load json config")); } } - } else { - trc(F("failed to mount FS")); } // The extra parameters to be configured (can be either global or just in the setup) @@ -704,6 +704,8 @@ void setup_wifimanager(bool reset_settings){ //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; + + wifiManager.setConnectTimeout(WifiManager_TimeOut); //Set timeout before going to portal wifiManager.setConfigPortalTimeout(WifiManager_ConfigPortalTimeOut); diff --git a/test/Test_config.h b/test/Test_config.h index ad3775a..3072cd3 100644 --- a/test/Test_config.h +++ b/test/Test_config.h @@ -57,6 +57,7 @@ #define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point #define WifiManager_ssid "OpenMQTTGateway" //this is the network name of the initial setup access point #define WifiManager_ConfigPortalTimeOut 120 +#define WifiManager_TimeOut 5 /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ //MQTT Parameters definition @@ -254,4 +255,4 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define TimeBetweenReadingSYS 120000 // time between system readings (like memory) #define subjectSYStoMQTT Base_Topic Gateway_Name "/SYStoMQTT" -#endif \ No newline at end of file +#endif