From 9917f619b366ff941411128b125039e20eb228ea Mon Sep 17 00:00:00 2001 From: Shawn A Date: Thu, 28 Jun 2018 12:53:36 -0500 Subject: [PATCH] reworking examples --- examples/AutoConnect/AutoConnect.ino | 32 ----- .../AutoConnectWithFSParameters.ino | 39 ++++-- ...AutoConnectWithFSParametersAndCustomIP.ino | 102 ++++++-------- .../AutoConnectWithFeedbackLED.ino | 18 +-- .../AutoConnectWithTimeout/platformio.ini | 18 --- .../OnDemandConfigPortal.ino | 127 ++++++++++++++++++ .../AutoConnectWithFeedback.ino | 0 .../AutoConnectWithReset.ino | 0 .../AutoConnectWithStaticIP.ino | 0 .../AutoConnectWithTimeout.ino | 0 .../OnDemandConfigPortal.ino | 124 ++++------------- .../OnDemandWebPortal/OnDemandWebPortal.ino | 16 +-- examples/Simple/Simple.ino | 31 +++++ 13 files changed, 271 insertions(+), 236 deletions(-) delete mode 100644 examples/AutoConnect/AutoConnect.ino rename examples/{ => AutoConnect}/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino (81%) rename examples/{ => AutoConnect}/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino (66%) rename examples/{ => AutoConnect}/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino (80%) delete mode 100644 examples/AutoConnectWithTimeout/platformio.ini create mode 100644 examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino rename examples/{ => Old Examples}/AutoConnectWithFeedback/AutoConnectWithFeedback.ino (100%) rename examples/{ => Old Examples}/AutoConnectWithReset/AutoConnectWithReset.ino (100%) rename examples/{ => Old Examples}/AutoConnectWithStaticIP/AutoConnectWithStaticIP.ino (100%) rename examples/{ => Old Examples}/AutoConnectWithTimeout/AutoConnectWithTimeout.ino (100%) create mode 100644 examples/Simple/Simple.ino diff --git a/examples/AutoConnect/AutoConnect.ino b/examples/AutoConnect/AutoConnect.ino deleted file mode 100644 index 0f58900..0000000 --- a/examples/AutoConnect/AutoConnect.ino +++ /dev/null @@ -1,32 +0,0 @@ -#include // https://github.com/tzapu/WiFiManager - -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; - //reset saved settings - //wifiManager.resetSettings(); - - //set custom ip for portal - //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); - - //fetches ssid and pass from eeprom 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 - wifiManager.autoConnect("AutoConnectAP"); - //or use this for auto generated name ESP + ChipID - //wifiManager.autoConnect(); - - - //if you get here you have connected to the WiFi - Serial.println("connected...yeey :)"); -} - -void loop() { - // put your main code here, to run repeatedly: - -} diff --git a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino b/examples/AutoConnect/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino similarity index 81% rename from examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino rename to examples/AutoConnect/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino index d2ca15f..07c7dd9 100644 --- a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino +++ b/examples/AutoConnect/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino @@ -11,6 +11,11 @@ char mqtt_server[40]; char mqtt_port[6] = "8080"; char api_token[32] = "YOUR_API_TOKEN"; +//default custom static IP +char static_ip[16] = "10.0.1.56"; +char static_gw[16] = "10.0.1.1"; +char static_sn[16] = "255.255.255.0"; + //flag for saving data bool shouldSaveConfig = false; @@ -50,6 +55,16 @@ void setupSpiffs(){ strcpy(mqtt_port, json["mqtt_port"]); strcpy(api_token, json["api_token"]); + if(json["ip"]) { + Serial.println("setting custom ip from config"); + strcpy(static_ip, json["ip"]); + strcpy(static_gw, json["gateway"]); + strcpy(static_sn, json["subnet"]); + Serial.println(static_ip); + } else { + Serial.println("no custom ip in config"); + } + } else { Serial.println("failed to load json config"); } @@ -88,19 +103,19 @@ void setup() { wm.addParameter(&custom_mqtt_port); wm.addParameter(&custom_api_token); + // set static ip + IPAddress _ip,_gw,_sn; + _ip.fromString(static_ip); + _gw.fromString(static_gw); + _sn.fromString(static_sn); + wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); + //reset settings - wipe credentials for testing //wm.resetSettings(); - //set minimu quality of signal so it ignores AP's under that quality, default is 8% - //wm.setMinimumSignalQuality(); - - //sets timeout until configuration portal gets turned off - //wm.setConfigPortalTimeout(120); - //automatically connect using saved credentials if they exist //If connection fails it starts an access point with the specified name //here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous - //and goes into a blocking loop awaiting configuration if (!wm.autoConnect("AutoConnectAP", "password")) { Serial.println("failed to connect and hit timeout"); @@ -127,20 +142,26 @@ void setup() { json["mqtt_port"] = mqtt_port; json["api_token"] = api_token; + json["ip"] = WiFi.localIP().toString(); + json["gateway"] = WiFi.gatewayIP().toString(); + json["subnet"] = WiFi.subnetMask().toString(); + File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("failed to open config file for writing"); } - json.printTo(Serial); + json.prettyPrintTo(Serial); json.printTo(configFile); configFile.close(); //end save + shouldSaveConfig = false; } Serial.println("local ip"); Serial.println(WiFi.localIP()); - + Serial.println(WiFi.gatewayIP()); + Serial.println(WiFi.subnetMask()); } void loop() { diff --git a/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino b/examples/AutoConnect/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino similarity index 66% rename from examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino rename to examples/AutoConnect/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino index 3fa9bcd..35b32ed 100644 --- a/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino +++ b/examples/AutoConnect/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino @@ -7,10 +7,10 @@ #endif //define your default values here, if there are different values in config.json, they are overwritten. -//length should be max size + 1 char mqtt_server[40]; -char mqtt_port[6] = "8080"; -char blynk_token[33] = "YOUR_BLYNK_TOKEN"; +char mqtt_port[6] = "8080"; +char api_token[32] = "YOUR_API_TOKEN"; + //default custom static IP char static_ip[16] = "10.0.1.56"; char static_gw[16] = "10.0.1.1"; @@ -25,13 +25,9 @@ void saveConfigCallback () { shouldSaveConfig = true; } -void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - Serial.println(); - +void setupSpiffs(){ //clean FS, for testing - //SPIFFS.format(); + // SPIFFS.format(); //read configuration from FS json Serial.println("mounting FS..."); @@ -57,24 +53,18 @@ void setup() { strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); - strcpy(blynk_token, json["blynk_token"]); + strcpy(api_token, json["api_token"]); if(json["ip"]) { Serial.println("setting custom ip from config"); - //static_ip = json["ip"]; strcpy(static_ip, json["ip"]); strcpy(static_gw, json["gateway"]); strcpy(static_sn, json["subnet"]); - //strcat(static_ip, json["ip"]); - //static_gw = json["gateway"]; - //static_sn = json["subnet"]; Serial.println(static_ip); -/* Serial.println("converting ip"); - IPAddress ip = ipFromCharArray(static_ip); - Serial.println(ip);*/ } else { Serial.println("no custom ip in config"); } + } else { Serial.println("failed to load json config"); } @@ -84,58 +74,53 @@ void setup() { Serial.println("failed to mount FS"); } //end read - Serial.println(static_ip); - Serial.println(blynk_token); - Serial.println(mqtt_server); +} +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.println(); + setupSpiffs(); + + // WiFiManager, Local intialization. Once its business is done, there is no need to keep it around + WiFiManager wm; + + //set config save notify callback + wm.setSaveConfigCallback(saveConfigCallback); + + // setup custom parameters + // // 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 WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); - WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5); - WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34); + WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6); + WiFiManagerParameter custom_api_token("api", "api token", "", 32); - //WiFiManager - //Local intialization. Once its business is done, there is no need to keep it around - WiFiManager wifiManager; + //add all your parameters here + wm.addParameter(&custom_mqtt_server); + wm.addParameter(&custom_mqtt_port); + wm.addParameter(&custom_api_token); - //set config save notify callback - wifiManager.setSaveConfigCallback(saveConfigCallback); - - //set static ip + // set static ip IPAddress _ip,_gw,_sn; _ip.fromString(static_ip); _gw.fromString(static_gw); _sn.fromString(static_sn); + wm.setSTAStaticIPConfig(_ip, _gw, _sn); - wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); - - //add all your parameters here - wifiManager.addParameter(&custom_mqtt_server); - wifiManager.addParameter(&custom_mqtt_port); - wifiManager.addParameter(&custom_blynk_token); + //reset settings - wipe credentials for testing + //wm.resetSettings(); - //reset settings - for testing - //wifiManager.resetSettings(); - - //set minimu quality of signal so it ignores AP's under that quality - //defaults to 8% - wifiManager.setMinimumSignalQuality(); - - //sets timeout until configuration portal gets turned off - //useful to make it all retry or go to sleep - //in seconds - //wifiManager.setConfigPortalTimeout(120); - - //fetches ssid and pass and tries to connect - //if it does not connect it starts an access point with the specified name - //here "AutoConnectAP" + //automatically connect using saved credentials if they exist + //If connection fails it starts an access point with the specified name + //here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous //and goes into a blocking loop awaiting configuration - if (!wifiManager.autoConnect("AutoConnectAP", "password")) { + if (!wm.autoConnect("AutoConnectAP", "password")) { Serial.println("failed to connect and hit timeout"); delay(3000); - //reset and try again, or maybe put it to deep sleep + // if we still have not connected restart and try all over again ESP.restart(); delay(5000); } @@ -146,7 +131,7 @@ void setup() { //read updated parameters strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); - strcpy(blynk_token, custom_blynk_token.getValue()); + strcpy(api_token, custom_api_token.getValue()); //save the custom parameters to FS if (shouldSaveConfig) { @@ -154,12 +139,12 @@ void setup() { DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["mqtt_server"] = mqtt_server; - json["mqtt_port"] = mqtt_port; - json["blynk_token"] = blynk_token; + json["mqtt_port"] = mqtt_port; + json["api_token"] = api_token; - json["ip"] = WiFi.localIP().toString(); - json["gateway"] = WiFi.gatewayIP().toString(); - json["subnet"] = WiFi.subnetMask().toString(); + json["ip"] = WiFi.localIP().toString(); + json["gateway"] = WiFi.gatewayIP().toString(); + json["subnet"] = WiFi.subnetMask().toString(); File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { @@ -170,6 +155,7 @@ void setup() { json.printTo(configFile); configFile.close(); //end save + shouldSaveConfig = false; } Serial.println("local ip"); diff --git a/examples/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino b/examples/AutoConnect/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino similarity index 80% rename from examples/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino rename to examples/AutoConnect/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino index df74e42..4a2c4dd 100644 --- a/examples/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino +++ b/examples/AutoConnect/AutoConnectWithFeedbackLED/AutoConnectWithFeedbackLED.ino @@ -1,14 +1,16 @@ +// LED will blink when in config mode + #include // https://github.com/tzapu/WiFiManager //for LED status #include Ticker ticker; +int LED = BUILTIN_LED; void tick() { //toggle state - int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin - digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state + digitalWrite(LED, !digitalRead(LED)); // set pin to the opposite state } //gets called when WiFiManager enters configuration mode @@ -26,24 +28,24 @@ void setup() { Serial.begin(115200); //set led pin as output - pinMode(BUILTIN_LED, OUTPUT); + pinMode(LED, OUTPUT); // start ticker with 0.5 because we start in AP mode and try to connect ticker.attach(0.6, tick); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around - WiFiManager wifiManager; + WiFiManager wm; //reset settings - for testing - //wifiManager.resetSettings(); + wm.resetSettings(); //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode - wifiManager.setAPCallback(configModeCallback); + wm.setAPCallback(configModeCallback); //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()) { + if (!wm.autoConnect()) { Serial.println("failed to connect and hit timeout"); //reset and try again, or maybe put it to deep sleep ESP.restart(); @@ -54,7 +56,7 @@ void setup() { Serial.println("connected...yeey :)"); ticker.detach(); //keep LED on - digitalWrite(BUILTIN_LED, LOW); + digitalWrite(LED, LOW); } void loop() { diff --git a/examples/AutoConnectWithTimeout/platformio.ini b/examples/AutoConnectWithTimeout/platformio.ini deleted file mode 100644 index 7e80288..0000000 --- a/examples/AutoConnectWithTimeout/platformio.ini +++ /dev/null @@ -1,18 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; http://docs.platformio.org/page/projectconf.html - -[env:nodemcuv2] -platform=espressif8266 -board=nodemcuv2 -framework=arduino - -[platformio] -src_dir=AutoConnectWithTimeout -lib_dir=/Users/shawn/projects/microcontrollers/dev/libraries diff --git a/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino new file mode 100644 index 0000000..0060d6d --- /dev/null +++ b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -0,0 +1,127 @@ +#include // https://github.com/tzapu/WiFiManager + +// select which pin will trigger the configuration portal when set to LOW +// ESP-01 users please note: the only pins available (0 and 2), are shared +// with the bootloader, so always set them HIGH at power-up +#define TRIGGER_PIN 0 +const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; + +WiFiManager wifiManager; + + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.setDebugOutput(true); + delay(3000); + Serial.println("\n Starting"); + + // WiFi.mode(WIFI_STA); + // WiFi.printDiag(Serial); + // Serial.println(modes[WiFi.getMode()]); + //WiFiManager + //Local intialization. Once its business is done, there is no need to keep it around + // WiFiManager wifiManager; + + //Local intialization. Once its business is done, there is no need to keep it around + //reset settings - for testing + //wifiManager.resetSettings(); + + //sets timeout until configuration portal gets turned off + //useful to make it all retry or go to sleep + //in seconds + wifiManager.setConfigPortalTimeout(60); + wifiManager.setConnectTimeout(20); + // wifiManager.setShowStaticFields(true); + + // uint8_t menu[] = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE}; + // wifiManager.setMenu(menu); + + // std::vector menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT}; + // wifiManager.setMenu(menu); + +// std::vector menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT}; + // wifiManager.setMenu(menu); + + + WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40); + WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "", 6); + WiFiManagerParameter custom_token("api_token", "api token", "", 0); + WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "", 0); + + //add all your parameters here + wifiManager.addParameter(&custom_mqtt_server); + wifiManager.addParameter(&custom_mqtt_port); + wifiManager.addParameter(&custom_token); + wifiManager.addParameter(&custom_tokenb); + + // MENU_WIFI = 0, + // MENU_WIFINOSCAN = 1, + // MENU_INFO = 2, + // MENU_PARAM = 3, + // MENU_CLOSE = 4, + // MENU_RESTART = 5, + // MENU_EXIT = 6, + // MENU_ERASE = 7, + // MENU_SEP = 8 + + // const char* menu[] = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"}; + // wifiManager.setMenu(menu,9); + + std::vector menu = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"}; + wifiManager.setMenu(menu); + + //set static ip + // wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); + // wifiManager.setShowStaticFields(false); + // wifiManager.setShowDnsFields(false); + + wifiManager.setConfigPortalTimeout(60); + wifiManager.startConfigPortal("AutoConnectAP", "password"); + + //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"); + } + + pinMode(TRIGGER_PIN, INPUT); +} + + +void loop() { + // is configuration portal requested? + if ( digitalRead(TRIGGER_PIN) == LOW ) { + //WiFiManager + //Local intialization. Once its business is done, there is no need to keep it around + //reset settings - for testing + //wifiManager.resetSettings(); + + //sets timeout until configuration portal gets turned off + //useful to make it all retry or go to sleep + //in seconds + wifiManager.setConfigPortalTimeout(120); + + //it starts an access point with the specified name + //here "AutoConnectAP" + //and goes into a blocking loop awaiting configuration + + //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1 + //WiFi.mode(WIFI_STA); + + // disable captive portal redirection + // wifiManager.setCaptivePortalEnable(false); + + if (!wifiManager.startConfigPortal("OnDemandAP")) { + Serial.println("failed to connect and hit timeout"); + delay(3000); + } else { + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); + } + } + + // put your main code here, to run repeatedly: +} diff --git a/examples/AutoConnectWithFeedback/AutoConnectWithFeedback.ino b/examples/Old Examples/AutoConnectWithFeedback/AutoConnectWithFeedback.ino similarity index 100% rename from examples/AutoConnectWithFeedback/AutoConnectWithFeedback.ino rename to examples/Old Examples/AutoConnectWithFeedback/AutoConnectWithFeedback.ino diff --git a/examples/AutoConnectWithReset/AutoConnectWithReset.ino b/examples/Old Examples/AutoConnectWithReset/AutoConnectWithReset.ino similarity index 100% rename from examples/AutoConnectWithReset/AutoConnectWithReset.ino rename to examples/Old Examples/AutoConnectWithReset/AutoConnectWithReset.ino diff --git a/examples/AutoConnectWithStaticIP/AutoConnectWithStaticIP.ino b/examples/Old Examples/AutoConnectWithStaticIP/AutoConnectWithStaticIP.ino similarity index 100% rename from examples/AutoConnectWithStaticIP/AutoConnectWithStaticIP.ino rename to examples/Old Examples/AutoConnectWithStaticIP/AutoConnectWithStaticIP.ino diff --git a/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino b/examples/Old Examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino similarity index 100% rename from examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino rename to examples/Old Examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino diff --git a/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino index c9b1fe7..8c67274 100644 --- a/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino +++ b/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -1,124 +1,46 @@ +/** + * OnDemandConfigPortal.ino + * example of running the configPortal AP manually, independantly from the captiveportal + * trigger pin will start a configPortal AP for 120 seconds then turn it off. + * + */ #include // https://github.com/tzapu/WiFiManager // select which pin will trigger the configuration portal when set to LOW -// ESP-01 users please note: the only pins available (0 and 2), are shared -// with the bootloader, so always set them HIGH at power-up #define TRIGGER_PIN 0 -const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; - -WiFiManager wifiManager; +int timeout = 120; // seconds to run for +bool webportalrunning = false; void setup() { // put your setup code here, to run once: Serial.begin(115200); - Serial.setDebugOutput(true); - delay(3000); Serial.println("\n Starting"); - - // Serial.println(modes[WiFi.getMode()]); - // WiFi.printDiag(Serial); - - //WiFiManager - //Local intialization. Once its business is done, there is no need to keep it around - WiFiManager wifiManager; - - //Local intialization. Once its business is done, there is no need to keep it around - //reset settings - for testing - //wifiManager.resetSettings(); - - //sets timeout until configuration portal gets turned off - //useful to make it all retry or go to sleep - //in seconds - wifiManager.setConfigPortalTimeout(20); - wifiManager.setConnectTimeout(10); - // wifiManager.setShowStaticFields(true); - - // uint8_t menu[] = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE}; - // wifiManager.setMenu(menu); - - // std::vector menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT}; - // wifiManager.setMenu(menu); - -// std::vector menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT}; - // wifiManager.setMenu(menu); - - - WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40); - WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "", 6); - WiFiManagerParameter custom_token("api_token", "api token", "", 0); - WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "", 0); - - //add all your parameters here - wifiManager.addParameter(&custom_mqtt_server); - wifiManager.addParameter(&custom_mqtt_port); - wifiManager.addParameter(&custom_token); - wifiManager.addParameter(&custom_tokenb); - - // MENU_WIFI = 0, - // MENU_WIFINOSCAN = 1, - // MENU_INFO = 2, - // MENU_PARAM = 3, - // MENU_CLOSE = 4, - // MENU_RESTART = 5, - // MENU_EXIT = 6, - // MENU_ERASE = 7, - // MENU_SEP = 8 - - // const char* menu[] = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"}; - // wifiManager.setMenu(menu,9); - - std::vector menu = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"}; - wifiManager.setMenu(menu); - - //set static ip - wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); - // wifiManager.setShowStaticFields(false); - // wifiManager.setShowDnsFields(false); - - //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"); - } - - pinMode(TRIGGER_PIN, INPUT); + pinMode(TRIGGER_PIN, INPUT_PULLUP); } - void loop() { // is configuration portal requested? - if ( digitalRead(TRIGGER_PIN) == LOW ) { - //WiFiManager - //Local intialization. Once its business is done, there is no need to keep it around + if ( digitalRead(TRIGGER_PIN) == LOW) { + WiFiManager wifiManager; //reset settings - for testing //wifiManager.resetSettings(); + + // set configportal timeout + wm.setConfigPortaltimeout(timeout); - //sets timeout until configuration portal gets turned off - //useful to make it all retry or go to sleep - //in seconds - wifiManager.setConfigPortalTimeout(120); - - //it starts an access point with the specified name - //here "AutoConnectAP" - //and goes into a blocking loop awaiting configuration - - //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1 - //WiFi.mode(WIFI_STA); - - // disable captive portal redirection - // wifiManager.setCaptivePortalEnable(false); - - if (!wifiManager.startConfigPortal("OnDemandAP")) { + if (!wm.startConfigPortal("OnDemandAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); - } else { - //if you get here you have connected to the WiFi - Serial.println("connected...yeey :)"); - } + //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 :)"); + } // put your main code here, to run repeatedly: diff --git a/examples/OnDemandWebPortal/OnDemandWebPortal.ino b/examples/OnDemandWebPortal/OnDemandWebPortal.ino index b45e4a0..07f61e9 100644 --- a/examples/OnDemandWebPortal/OnDemandWebPortal.ino +++ b/examples/OnDemandWebPortal/OnDemandWebPortal.ino @@ -7,14 +7,11 @@ #include // https://github.com/tzapu/WiFiManager // select which pin will trigger the configuration portal when set to LOW -// ESP-01 users please note: the only pins available (0 and 2), are shared -// with the bootloader, so always set them HIGH at power-up #define TRIGGER_PIN 0 -int timeout = 120; // seconds - -WiFiManager wifiManager; +WiFiManager wm; +int timeout = 120; // seconds to run for bool webportalrunning = false; int start = millis(); @@ -27,19 +24,18 @@ void setup() { void loop() { - if(webportalrunning){ - wifiManager.process(); + wm.process(); if(millis()-start > timeout*1000){ Serial.println("webportaltimeout"); webportalrunning = false; - wifiManager.stopWebPortal(); // auto off + wm.stopWebPortal(); // auto off } } - // is configuration portal requested? if ( digitalRead(TRIGGER_PIN) == LOW && !webportalrunning) { - wifiManager.startWebPortal(); + wm.startWebPortal(); + // wm.startWebPortal(); webportalrunning = true; start = millis(); } diff --git a/examples/Simple/Simple.ino b/examples/Simple/Simple.ino new file mode 100644 index 0000000..e244fde --- /dev/null +++ b/examples/Simple/Simple.ino @@ -0,0 +1,31 @@ +#include // https://github.com/tzapu/WiFiManager + +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 wm; + + //reset settings - wipe credentials for testing + //wm.resetSettings(); + + //set custom ip for portal + //wm.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); + + //automatically connect using saved credentials if they exist + //If connection fails it starts an access point with the specified name + //here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous + //and goes into a blocking loop awaiting configuration + wm.autoConnect("AutoConnectAP"); + //or use this for auto generated name ESP + ChipID + //wm.autoConnect(); + + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); +} + +void loop() { + // put your main code here, to run repeatedly: + +}