From ce8c9fea61b8c97c56cefd313a36d7ca6e56e609 Mon Sep 17 00:00:00 2001 From: Alex Tudorache Date: Fri, 9 Oct 2015 18:33:41 +0300 Subject: [PATCH] added initialisation to setup so wifi is no longer global and it gets cleaned dnsServer and webServer are also no longer global readme updated --- README.md | 17 +++++++++++++++-- WiFiManager.cpp | 14 +++++++++----- WiFiManager.h | 6 ++++++ examples/AutoConnect/AutoConnect.ino | 10 +++++----- .../AutoConnectWithTimeout.ino | 9 +++++---- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7e3c3dc..7cdaa63 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,13 @@ This works with the ESP8266 Arduino platform https://github.com/esp8266/Arduino #include ``` -- Initialise library, the paramater is the Acces Point Name +- Initialise library, in your setup function add ``` //parameter is eeprom start WiFiManager wifi; ``` -- In your setup function add +- Also in the setup function add ``` //parameter is name of access point wifi.autoConnect("AP-NAME"); @@ -40,6 +40,19 @@ While in AP mode, connect to it then open a browser to the gateway IP, default 1 Also see examples. +###Timeout +If you need to set a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure, you can add +``` +wifi.setTimeout(180); +``` +which will wait 3 minutes (180 seconds). When the time passes, the autoConnect function will return, no matter the outcome. +Check for connection and if it's still not established do whatever is needed (on some modules I restart them to retry, on others I enter deep sleep) + +###Debug +Debug is enabled by default on Serial. To disable add before autoConnect +``` +wifi.setDebugOutput(false); +``` ####Inspiration - http://www.esp8266.com/viewtopic.php?f=29&t=2520 diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 895566e..2400ebd 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -14,12 +14,12 @@ #include "WiFiManager.h" -DNSServer dnsServer; +//DNSServer dnsServer; // Web server -ESP8266WebServer server(80); +//ESP8266WebServer server(80); -WiFiManager::WiFiManager() { +WiFiManager::WiFiManager() : server(80) { } void WiFiManager::begin() { @@ -27,6 +27,7 @@ void WiFiManager::begin() { } void WiFiManager::begin(char const *apName) { + DEBUG_PRINT(""); _apName = apName; start = millis(); @@ -64,7 +65,10 @@ boolean WiFiManager::autoConnect(char const *apName) { // read eeprom for ssid and pass String ssid = getSSID(); String pass = getPassword(); - + //use SDK functions to get SSID and pass + //String ssid = WiFi.SSID(); + //String pass = WiFi.psk(); + WiFi.mode(WIFI_STA); connectWifi(ssid, pass); int s = WiFi.status(); @@ -325,7 +329,7 @@ void WiFiManager::handleWifiSave() { server.sendHeader("Expires", "-1"); server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. - String head = HTTP_HEAD; + String head = HTTP_HEAD; head.replace("{v}", "Credentials Saved"); server.sendContent(head); server.sendContent(HTTP_SCRIPT); diff --git a/WiFiManager.h b/WiFiManager.h index 8cb4f26..e2dbb0d 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -53,6 +53,9 @@ public: int serverLoop(); private: + DNSServer dnsServer; + ESP8266WebServer server; + const int WM_DONE = 0; const int WM_WAIT = 10; @@ -66,6 +69,9 @@ private: const String HTTP_FORM = "

"; const String HTTP_SAVED = "
Credentials Saved
Node will reboot in 5 seconds.
"; const String HTTP_END = ""; + //const char HTTP_END[] PROGMEM = R"=====( + // + //)====="; int _eepromStart; const char* _apName = "no-net"; diff --git a/examples/AutoConnect/AutoConnect.ino b/examples/AutoConnect/AutoConnect.ino index bcb574b..66d658f 100644 --- a/examples/AutoConnect/AutoConnect.ino +++ b/examples/AutoConnect/AutoConnect.ino @@ -6,15 +6,15 @@ #include #include //https://github.com/tzapu/WiFiManager -//this intitilises the library, parameter is eeprom start -WiFiManager wifi; 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 wifi; + //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" @@ -28,4 +28,4 @@ void setup() { void loop() { // put your main code here, to run repeatedly: -} \ No newline at end of file +} diff --git a/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino b/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino index d055261..220a88e 100644 --- a/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino +++ b/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino @@ -1,26 +1,27 @@ #include //https://github.com/esp8266/Arduino //needed for library -#include //https://github.com/tzapu/WiFiManager - #include #include #include #include +#include //https://github.com/tzapu/WiFiManager + //this intitilises the library, parameter is eeprom start -WiFiManager wifi; 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 wifi; //reset settings - for testing //wifi.resetSettings(); - //WiFiManager //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds