diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 861c3c6..a8edd42 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -40,83 +40,15 @@ int WiFiManagerParameter::getValueLength() { } WiFiManager::WiFiManager() { - //for(int i=0; igetID()); - //} - } -/* - void WiFiManager::loadParameters() { - DEBUG_PRINT("Load parameters - not needed"); - if(_eepromLength==0){ - // if not initialized - for(int i=0; igetValueLength(); - } - DEBUG_PRINT(F("EEPROM initialized with length ")); - DEBUG_PRINT(_eepromLength); - - EEPROM.begin(_eepromLength); - } - - int pos = 0; - for(int i=0; igetValueLength(); - for(int j=0; j_value[j] = EEPROM.read(pos++); - _params[i]->_value[l-1] = 0; - DEBUG_PRINT(F("Parameter loaded")); - DEBUG_PRINT(_params[i]->getID()); - DEBUG_PRINT(_params[i]->getValue()); - } - } -*/ -void WiFiManager::saveParameters() { - DEBUG_PRINT("save params callback"); - /* - if(_eepromLength==0){ - // if not initialized - for(int i=0; igetValueLength(); - } - DEBUG_PRINT(F("EEPROM initialized with length ")); - DEBUG_PRINT(_eepromLength); - - EEPROM.begin(_eepromLength); - } - int pos = 0; - for(int i=0; igetValueLength(); - for(int j=0; j_value[j]); - } - EEPROM.commit(); - */ -} void WiFiManager::begin() { begin("NoNetESP"); @@ -242,6 +174,11 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) { } else { //connected WiFi.mode(WIFI_STA); + //notify that configuration has changed and any optional parameters should be saved + if ( _savecallback != NULL) { + //todo: check if any custom parameters actually exist, and check if they really changed maybe + _savecallback(); + } break; } @@ -360,9 +297,6 @@ void WiFiManager::resetSettings() { DEBUG_PRINT(F("THIS MAY CAUSE AP NOT TO STRT UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.")); WiFi.disconnect(true); //delay(200); - - //overwrite the current parameter values (with defaults) - //saveParameters(); } void WiFiManager::setTimeout(unsigned long seconds) { @@ -508,7 +442,6 @@ void WiFiManager::handleWifiSave() { DEBUG_PRINT(_params[i]->getID()); DEBUG_PRINT(value); } - saveParameters(); server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server->sendHeader("Pragma", "no-cache"); @@ -580,6 +513,12 @@ void WiFiManager::setAPCallback( void (*func)(void) ) { _apcallback = func; } +//start up save config callback +void WiFiManager::setSaveConfigCallback( void (*func)(void) ) { + _savecallback = func; +} + + template void WiFiManager::DEBUG_PRINT(Generic text) { diff --git a/WiFiManager.h b/WiFiManager.h index 9515356..b209215 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -80,13 +80,14 @@ public: //sets a custom ip /gateway /subnet configuration void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn); + //called when AP mode and config portal is started void setAPCallback( void (*func)(void) ); + //called when settings have been changed and connection was successful + void setSaveConfigCallback( void (*func)(void) ); //adds a custom parameter void addParameter(WiFiManagerParameter *p); - //void loadParameters(); - void saveParameters(); private: std::unique_ptr dnsServer; std::unique_ptr server; @@ -150,6 +151,7 @@ private: boolean _debug = true; void (*_apcallback)(void) = NULL; + void (*_savecallback)(void) = NULL; WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; //int _eepromLength; diff --git a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino index e736fcb..5e69973 100644 --- a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino +++ b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino @@ -14,49 +14,61 @@ const char *mqtt_server = NULL; const char *mqtt_port = "8080"; const char *blynk_token = "YOUR_BLYNK_TOKEN"; + //flag for saving data + bool shouldSaveConfig = false; + +//callback notifying us of the need to save config +void saveConfigCallback () { + Serial.println("Should save config"); + shouldSaveConfig = true; +} + + void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println(); - //read configuration from FS json + //clean FS, for testing //SPIFFS.format(); - Serial.println("Mounting FS..."); + //read configuration from FS json + Serial.println("mounting FS..."); - if (!SPIFFS.begin()) { - Serial.println("Failed to mount file system"); - //abort - return; - } + if (SPIFFS.begin()) { + Serial.println("mounted file system"); + if (SPIFFS.exists("/config.json")) { + //file exists, reading and loading + Serial.println("reading config file"); + File configFile = SPIFFS.open("/config.json", "r"); + if (configFile) { + Serial.println("opened config file"); + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); - if (SPIFFS.exists("/config.json")) { - //file exists, reading and loading - Serial.println("Reading config file"); - File configFile = SPIFFS.open("/config.json", "r"); - if (configFile) { - Serial.println("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); + StaticJsonBuffer<200> jsonBuffer; + JsonObject& json = jsonBuffer.parseObject(buf.get()); + json.printTo(Serial); + if (json.success()) { + Serial.println("\nparsed json"); - configFile.readBytes(buf.get(), size); - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - json.printTo(Serial); - if (json.success()) { - Serial.println("\nParsed json"); - - mqtt_server = json["mqtt_server"]; - mqtt_port = json["mqtt_port"]; - blynk_token = json["blynk_token"]; - } else { - Serial.println("Failed to load json config"); + mqtt_server = json["mqtt_server"]; + mqtt_port = json["mqtt_port"]; + blynk_token = json["blynk_token"]; + } else { + Serial.println("failed to load json config"); + } } } + } else { + Serial.println("failed to mount FS"); } //end read + + // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length @@ -64,11 +76,13 @@ void setup() { WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 8); WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32); - //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; - + + //set config save notify callback + wifiManager.setSaveConfigCallback(saveConfigCallback); + //add all your parameters here wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); @@ -101,24 +115,26 @@ void setup() { mqtt_server = custom_mqtt_server.getValue(); mqtt_port = custom_mqtt_port.getValue(); blynk_token = custom_blynk_token.getValue(); - + //save the custom parameters to FS - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.createObject(); - json["mqtt_server"] = mqtt_server; - json["mqtt_port"] = mqtt_port; - json["blynk_token"] = blynk_token; + if (shouldSaveConfig) { + Serial.println("saving config"); + StaticJsonBuffer<200> jsonBuffer; + JsonObject& json = jsonBuffer.createObject(); + json["mqtt_server"] = mqtt_server; + json["mqtt_port"] = mqtt_port; + json["blynk_token"] = blynk_token; - File configFile = SPIFFS.open("/config.json", "w"); - if (!configFile) { - Serial.println("Failed to open config file for writing"); - return; + File configFile = SPIFFS.open("/config.json", "w"); + if (!configFile) { + Serial.println("failed to open config file for writing"); + } + + json.printTo(Serial); + json.printTo(configFile); + configFile.close(); + //end save } - - json.printTo(Serial); - json.printTo(configFile); - configFile.close(); - //end save } void loop() {