added initialisation to setup so wifi is no longer global and it gets cleaned

dnsServer and webServer are also no longer global
readme updated
This commit is contained in:
Alex Tudorache
2015-10-09 18:33:41 +03:00
parent 97b74f2b19
commit ce8c9fea61
5 changed files with 40 additions and 16 deletions
+15 -2
View File
@@ -23,13 +23,13 @@ This works with the ESP8266 Arduino platform https://github.com/esp8266/Arduino
#include <WiFiManager.h>
```
- 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
+9 -5
View File
@@ -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);
+6
View File
@@ -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 = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><input id='p' name='p' length=64 placeholder='password'><br/><input type='submit'></form>";
const String HTTP_SAVED = "<div>Credentials Saved<br />Node will reboot in 5 seconds.</div>";
const String HTTP_END = "</body></html>";
//const char HTTP_END[] PROGMEM = R"=====(
//</body></html>
//)=====";
int _eepromStart;
const char* _apName = "no-net";
+5 -5
View File
@@ -6,15 +6,15 @@
#include <DNSServer.h>
#include <WiFiManager.h> //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:
}
}
@@ -1,26 +1,27 @@
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <EEPROM.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //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