mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-04 10:58:13 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
614516596b | ||
|
|
e09c5803b2 | ||
|
|
a95000e91a | ||
|
|
070448db4e | ||
|
|
659df9cb27 | ||
|
|
c03570faba | ||
|
|
3d550ab78f | ||
|
|
ec23ed45b0 | ||
|
|
faed661d25 | ||
|
|
0ead57671c |
@@ -1,43 +1,61 @@
|
||||
# WiFiManager
|
||||
ESP8266 WiFi Connection manager with save to EEPROM and web config portal
|
||||
ESP8266 WiFi Connection manager with fallback web configuration portal
|
||||
|
||||
Captive portal should also work, I have tested it only on iOS.
|
||||
The configuration portal is of the captive variety, so on various devices it will present the configuration dialogue as soon as you connect to the created access point.
|
||||
|
||||
First attempt at a library. Lots more changes and fixes to do. Adding examples also needs doing.
|
||||
First attempt at a library. Lots more changes and fixes to do. Contributions are welcome.
|
||||
|
||||
## How It works
|
||||
- when your ESP starts up, it sets it up in Station mode and tries to connect to a previously saved Access Point
|
||||
- if this is unsuccessful (or no previous network saved) it moves the ESP into Access Point mode and spins up a DNS and WebServer (default ip 192.168.4.1)
|
||||
- using any wifi enabled device with a browser (computer, phone, tablet) connect to the newly created Access Point
|
||||
- because of the Captive Portal and the DNS server you will either get a 'Join to network' type of popup or get any domain you try to access redirected to the configuration portal
|
||||
- choose one of the access points scanned, enter password, click save
|
||||
- ESP will try to connect. If successful, it relinquishes control back to your app. If not, reconnect to AP and reconfigure.
|
||||
|
||||
## Still left to do
|
||||
- ~~remove dependency on EEPROM library~~
|
||||
- move HTML Strings to PROGMEM
|
||||
- cleanup and streamline code
|
||||
- if timeout is set, extend it when a page is fetched in AP mode
|
||||
- add ability to configure more parameters than ssid/password
|
||||
- maybe allow setting ip of ESP after reboot
|
||||
|
||||
This works with the ESP8266 Arduino platform https://github.com/esp8266/Arduino
|
||||
|
||||
## Releases
|
||||
> v0.3 removed the need for EEPROM and works with the 2.0.0 and above stable release of the ESP8266 for Arduino IDE package
|
||||
|
||||
> **v0.2 and newer need the latest staging version (or at least a recent release of the staging version) to work**
|
||||
|
||||
> v0.1 works with the staging release ver. 1.6.5-1044-g170995a, built on Aug 10, 2015 of the ESP8266 Arduino library.
|
||||
> latest comit should work with the latest staging version
|
||||
|
||||
|
||||
## Quick Start
|
||||
- Make sure you have a recent package of the ESP8266 for Arduino IDE
|
||||
|
||||
- Checkout library to your Arduino libraries folder
|
||||
|
||||
- Include in your sketch
|
||||
|
||||
```Arduino
|
||||
#include <EEPROM.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
```cpp
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h>
|
||||
```
|
||||
|
||||
- Initialise library, in your setup function add
|
||||
```
|
||||
//parameter is eeprom start
|
||||
WiFiManager wifi;
|
||||
```cpp
|
||||
WiFiManager wifiManager;
|
||||
```
|
||||
|
||||
- Also in the setup function add
|
||||
```
|
||||
```cpp
|
||||
//parameter is name of access point
|
||||
wifi.autoConnect("AP-NAME");
|
||||
wifiManager.autoConnect("AP-NAME");
|
||||
```
|
||||
or if you want to use and auto generated name from 'ESP' and the esp's Chip ID use
|
||||
```
|
||||
wifi.autoConnect();
|
||||
|
||||
```cpp
|
||||
wifiManager.autoConnect();
|
||||
```
|
||||
|
||||
After you write your sketch and start the ESP, it will try to connect to WiFi. If it fails it starts in Access Point mode.
|
||||
@@ -46,27 +64,27 @@ 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);
|
||||
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
|
||||
```cpp
|
||||
wifiManager.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);
|
||||
```cpp
|
||||
wifiManager.setDebugOutput(false);
|
||||
```
|
||||
|
||||
### Custom IP Configuration
|
||||
This will set your captive portal to a specific IP should you need/want such a feature. Add the following snippet before `autoConnect()`
|
||||
```
|
||||
```cpp
|
||||
//set custom ip for portal
|
||||
wifi.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
```
|
||||
|
||||
####Inspiration
|
||||
#### Inspiration
|
||||
- http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
|
||||
To be continued...
|
||||
|
||||
+39
-24
@@ -96,41 +96,51 @@ boolean WiFiManager::autoConnect(char const *apName) {
|
||||
connect = false;
|
||||
begin(apName);
|
||||
|
||||
while(1) {
|
||||
bool looping = true;
|
||||
//while(1) {
|
||||
while(timeout == 0 || millis() < start + timeout) {
|
||||
//DNS
|
||||
dnsServer.processNextRequest();
|
||||
//HTTP
|
||||
server.handleClient();
|
||||
|
||||
if(connect) {
|
||||
/*if(connect) {
|
||||
delay(5000);
|
||||
ESP.reset();
|
||||
delay(1000);
|
||||
}
|
||||
/*
|
||||
}*/
|
||||
|
||||
if(connect) {
|
||||
delay(2000);
|
||||
DEBUG_PRINT("Connecting to new AP");
|
||||
connect = false;
|
||||
ssid = getSSID();
|
||||
pass = getPassword();
|
||||
connectWifi(ssid, pass);
|
||||
//ssid = getSSID();
|
||||
//pass = getPassword();
|
||||
connectWifi(_ssid, _pass);
|
||||
int s = WiFi.status();
|
||||
if (s == WL_CONNECTED) {
|
||||
//connected
|
||||
if (s != WL_CONNECTED) {
|
||||
DEBUG_PRINT("Failed to connect.");
|
||||
//not connected, should retry everything
|
||||
//ESP.reset();
|
||||
//delay(1000);
|
||||
//return false;
|
||||
} else {
|
||||
//connected
|
||||
WiFi.mode(WIFI_STA);
|
||||
return true;
|
||||
}
|
||||
|
||||
}*/
|
||||
}
|
||||
yield();
|
||||
}
|
||||
|
||||
return false;
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
|
||||
void WiFiManager::connectWifi(String ssid, String pass) {
|
||||
DEBUG_PRINT("Connecting as wifi client...");
|
||||
WiFi.disconnect();
|
||||
//WiFi.disconnect();
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
int connRes = WiFi.waitForConnectResult();
|
||||
DEBUG_PRINT ("Connection result: ");
|
||||
@@ -141,7 +151,7 @@ void WiFiManager::connectWifi(String ssid, String pass) {
|
||||
String WiFiManager::getSSID() {
|
||||
if (_ssid == "") {
|
||||
DEBUG_PRINT("Reading EEPROM SSID");
|
||||
_ssid = getEEPROMString(0, 32);
|
||||
_ssid = WiFi.SSID();//getEEPROMString(0, 32);
|
||||
DEBUG_PRINT("SSID: ");
|
||||
DEBUG_PRINT(_ssid);
|
||||
}
|
||||
@@ -151,14 +161,14 @@ String WiFiManager::getSSID() {
|
||||
String WiFiManager::getPassword() {
|
||||
if (_pass == "") {
|
||||
DEBUG_PRINT("Reading EEPROM Password");
|
||||
_pass = getEEPROMString(32, 64);
|
||||
_pass = WiFi.psk();//getEEPROMString(32, 64);
|
||||
DEBUG_PRINT("Password: " + _pass);
|
||||
//DEBUG_PRINT(_pass);
|
||||
}
|
||||
return _pass;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void WiFiManager::setSSID(String s) {
|
||||
_ssid = s;
|
||||
setEEPROMString(0, 32, _ssid);
|
||||
@@ -169,8 +179,8 @@ void WiFiManager::setPassword(String p) {
|
||||
DEBUG_PRINT(p);
|
||||
_pass = p;
|
||||
setEEPROMString(32, 64, _pass);
|
||||
}
|
||||
|
||||
}*/
|
||||
/*
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
@@ -182,7 +192,8 @@ String WiFiManager::getEEPROMString(int start, int len) {
|
||||
EEPROM.end();
|
||||
return string;
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
@@ -201,7 +212,7 @@ void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
}
|
||||
EEPROM.end();
|
||||
DEBUG_PRINT("Wrote " + string);
|
||||
}
|
||||
}*/
|
||||
|
||||
String WiFiManager::urldecode(const char *src)
|
||||
{
|
||||
@@ -241,12 +252,12 @@ String WiFiManager::urldecode(const char *src)
|
||||
|
||||
void WiFiManager::resetSettings() {
|
||||
//need to call it only after lib has been started with autoConnect or begin
|
||||
setEEPROMString(0, 32, "-");
|
||||
setEEPROMString(32, 64, "-");
|
||||
//setEEPROMString(0, 32, "-");
|
||||
//setEEPROMString(32, 64, "-");
|
||||
|
||||
DEBUG_PRINT("settings invalidated");
|
||||
delay(200);
|
||||
WiFi.disconnect();
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void WiFiManager::setTimeout(unsigned long seconds) {
|
||||
@@ -342,8 +353,12 @@ void WiFiManager::handleWifiSave() {
|
||||
DEBUG_PRINT("WiFi save");
|
||||
//server.arg("s").toCharArray(ssid, sizeof(ssid) - 1);
|
||||
//server.arg("p").toCharArray(password, sizeof(password) - 1);
|
||||
setSSID(urldecode(server.arg("s").c_str()));
|
||||
setPassword(urldecode(server.arg("p").c_str()));
|
||||
|
||||
//SAVE/connect here
|
||||
//setSSID(urldecode(server.arg("s").c_str()));
|
||||
//setPassword(urldecode(server.arg("p").c_str()));
|
||||
_ssid = urldecode(server.arg("s").c_str());
|
||||
_pass = urldecode(server.arg("p").c_str());
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
|
||||
+3
-3
@@ -14,8 +14,8 @@
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include <EEPROM.h>
|
||||
#include <WiFiClient.h>
|
||||
//#include <EEPROM.h>
|
||||
//#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <DNSServer.h>
|
||||
|
||||
@@ -70,7 +70,7 @@ private:
|
||||
const String HTTP_HEAD_END = "</head><body>";
|
||||
const String HTTP_ITEM = "<div><a href='#' onclick='c(this)'>{v}</a></div>";
|
||||
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/><button type='submit'>save</button></form>";
|
||||
const String HTTP_SAVED = "<div>Credentials Saved<br />Node will reboot in 5 seconds.</div>";
|
||||
const String HTTP_SAVED = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
|
||||
const String HTTP_END = "</body></html>";
|
||||
//const char HTTP_END[] PROGMEM = R"=====(
|
||||
//</body></html>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <EEPROM.h>
|
||||
#include <WiFiClient.h>
|
||||
//#include <EEPROM.h>
|
||||
//#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <DNSServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
@@ -20,7 +20,7 @@ void setup() {
|
||||
WiFiManager wifi;
|
||||
|
||||
//reset settings - for testing
|
||||
//wifi.resetSettings();
|
||||
wifi.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
@@ -33,7 +33,7 @@ void setup() {
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if(!wifi.autoConnect("AutoConnectAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(5000);
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
|
||||
Reference in New Issue
Block a user