diff --git a/WiFiManager.cpp b/WiFiManager.cpp index fc79a72..fa970c5 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -13,7 +13,95 @@ #include "WiFiManager.h" +WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length){ + _id = id; + _placeholder = placeholder; + _length = length; + _value = new char[length]; + 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(){ + 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() { @@ -258,6 +346,9 @@ 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) { @@ -353,7 +444,26 @@ void WiFiManager::handleWifi(bool scan) { } } - server->sendContent_P(HTTP_FORM); + server->sendContent_P(HTTP_FORM_START); + char parLength[2]; + // add the extra parameters to the form + for(int i=0; igetID()); + pitem.replace("{n}", _params[i]->getID()); + pitem.replace("{p}", _params[i]->getPlaceholder()); + snprintf(parLength, 2, "%d", _params[i]->getValueLength()); + pitem.replace("{l}", parLength); + pitem.replace("{v}", _params[i]->getValue()); + + server->sendContent(pitem); + } + if(_params[0]!=NULL) + server->sendContent_P("
"); + + server->sendContent_P(HTTP_FORM_END); server->sendContent_P(HTTP_END); server->client().stop(); @@ -368,6 +478,18 @@ void WiFiManager::handleWifiSave() { _ssid = urldecode(server->arg("s").c_str()); _pass = urldecode(server->arg("p").c_str()); + //parameter + for(int i=0; iarg(_params[i]->getID()).c_str()); + value.toCharArray(_params[i]->_value, _params[i]->_length); + DEBUG_PRINT(F("Parameter")); + DEBUG_PRINT(_params[i]->getID()); + DEBUG_PRINT(value); + } + saveParameters(); + server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server->sendHeader("Pragma", "no-cache"); server->sendHeader("Expires", "-1"); diff --git a/WiFiManager.h b/WiFiManager.h index 27deed9..7192d24 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -14,7 +14,7 @@ #include -//#include +#include //#include #include #include @@ -29,10 +29,32 @@ const char HTTP_HEAD_END[] PROGMEM = "
{v} {r}% {i}
"; const char HTTP_ITEM_PADLOCK[] PROGMEM = ""; -const char HTTP_FORM[] PROGMEM = "



"; +const char HTTP_FORM_START[] PROGMEM = "


"; +const char HTTP_FORM_PARAM[] PROGMEM = "
"; +const char HTTP_FORM_END[] PROGMEM = "
"; + const char HTTP_SAVED[] PROGMEM = "
Credentials Saved
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; const char HTTP_END[] PROGMEM = ""; +#define WIFI_MANAGER_MAX_PARAMS 6 + +class WiFiManagerParameter { +public: + WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length); + + const char* getID(); + const char* getValue(); + const char* getPlaceholder(); + int getValueLength(); +private: + const char* _id; + const char* _placeholder; + char* _value; + int _length; + +friend class WiFiManager; +}; + class WiFiManager { @@ -60,6 +82,10 @@ public: void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn); void setAPCallback( void (*func)(void) ); + void addParameter(WiFiManagerParameter *p); + + void loadParameters(); + void saveParameters(); private: std::unique_ptr dnsServer; std::unique_ptr server; @@ -123,6 +149,9 @@ private: void (*_apcallback)(void) = NULL; + WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; + int _eepromLength; + template void DEBUG_PRINT(Generic text); }; diff --git a/examples/AutoConnectParametersWithTimeout/AutoConnectParametersWithTimeout.ino b/examples/AutoConnectParametersWithTimeout/AutoConnectParametersWithTimeout.ino new file mode 100644 index 0000000..e933bdf --- /dev/null +++ b/examples/AutoConnectParametersWithTimeout/AutoConnectParametersWithTimeout.ino @@ -0,0 +1,65 @@ +#include //https://github.com/esp8266/Arduino + +//needed for library +#include +#include +#include //https://github.com/tzapu/WiFiManager + + +// The extra parameters to be configured (can be either global or just in the setup) +WiFiManagerParameter mqtt_server("server", "mqtt server", NULL, 40); +WiFiManagerParameter mqtt_port("port", "mqtt port", "8080", 8); + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + + //WiFiManager + //Local intialization. Once its business is done, there is no need to keep it around + WiFiManager wifiManager; + + //add all your parameters here + wifiManager.addParameter(&mqtt_server); + wifiManager.addParameter(&mqtt_port); + + //reset settings - for testing + wifiManager.resetSettings(); + + //load previously saved parameters + wifiManager.loadParameters(); + + //sets timeout until configuration portal gets turned off + //useful to make it all retry or go to sleep + //in seconds + wifiManager.setTimeout(180); + + //fetches ssid and pass and tries to connect + //if it does not connect it starts an access point with the specified name + //here "AutoConnectAP" + //and goes into a blocking loop awaiting configuration + if(!wifiManager.autoConnect("AutoConnectAP")) { + Serial.println("failed to connect and hit timeout"); + delay(3000); + //reset and try again, or maybe put it to deep sleep + ESP.reset(); + delay(5000); + } + + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); + + // use your parameter values as needed + Serial.print(mqtt_server.getPlaceholder()); + Serial.print(": "); + Serial.println(mqtt_server.getValue()); + + Serial.print(mqtt_port.getPlaceholder()); + Serial.print(": "); + Serial.println(mqtt_port.getValue()); +} + +void loop() { + // put your main code here, to run repeatedly: + + +}