format FS if begin() fail, to avoid setting lost when FS is not mounted

add of timeout parameter
This commit is contained in:
1technophile committed 2019-07-29 08:12:07 +02:00
1 parent 6795475e03
commit e20339857e
3 files changed
+33 -29

No files matched your search

+1
View File
@@ -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
+30 -28
View File
@@ -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<char[]> 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<char[]> 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);
+2 -1
View File
@@ -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
#endif