Compare commits

..
2 Commits
Author SHA1 Message Date
Alex Tudorache 0f75d745fe 0.9 fixed encoding problems 2016-02-04 14:38:48 +02:00
Alex Tudorache ba1aa5496d added more examples,
updated readme
made it compile on older versions
2016-02-01 12:34:29 +02:00
6 changed files with 168 additions and 87 deletions
+20 -5
View File
@@ -48,7 +48,7 @@ First attempt at a library. Lots more changes and fixes to do. Contributions are
- ~~add ability to configure more parameters than ssid/password~~
- ~~maybe allow setting ip of ESP after reboot~~
- ~~add to Arduino Boards Manager~~
- add to PlatformIO
- ~~add to PlatformIO~~
- add multiple sets of network credentials
- allow users to customize CSS
@@ -58,7 +58,7 @@ First attempt at a library. Lots more changes and fixes to do. Contributions are
You can either install through the Arduino Boards Manager or checkout the latest changes or a release from github
#### Install through Boards Manager
__Currently version 0.6 works with release 2.0.0 or newer of the [ESP8266 core for Arduino](https://github.com/esp8266/Arduino)__
__Currently version 0.8 works with release 2.0.0 or newer of the [ESP8266 core for Arduino](https://github.com/esp8266/Arduino)__
- in Arduino IDE got to Sketch/Include Library/Manage Libraries
![Manage Libraries](http://i.imgur.com/9BkEBkR.png)
@@ -68,7 +68,7 @@ __Currently version 0.6 works with release 2.0.0 or newer of the [ESP8266 core f
- click Install and start [using it](#using)
#### Checkout from github
__Github version works with the HEAD (as of 18 jan 2016) or newer of the [ESP8266 core for Arduino](https://github.com/esp8266/Arduino)__
__Github version works with release 2.0.0 or newer of the [ESP8266 core for Arduino](https://github.com/esp8266/Arduino)__
- Checkout library to your Arduino libraries folder
### Using
@@ -131,7 +131,7 @@ void configModeCallback (WiFiManager *myWiFiManager) {
```
##### Save settings
This gets called when custom parameters have been set **AND** a connection has been established. Use it to set a flag, so when all the configuration finishes, you can save the extra parameters somewhere.
This gets called when custom parameters have been set **AND** a connection has been established. Use it to set a flag, so when all the configuration finishes, you can save the extra parameters somewhere.
See [AutoConnectWithFSParameters Example](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters).
```cpp
@@ -229,6 +229,8 @@ wifiManager.setDebugOutput(false);
## Troubleshooting
If you get compilation errors, more often than not, you may need to install a newer version of the ESP8266 core for Arduino.
Changes added on 0.8 should make the latest trunk work without compilation errors. Tested down to ESP8266 core 2.0.0. **Please update to version 0.8**
I am trying to keep releases working with release versions of the core, so they can be installed through boards manager, but if you checkout the latest version directly from github, sometimes, the library will only work if you update the ESP8266 core to the latest version because I am using some newly added function.
@@ -236,7 +238,20 @@ If you connect to the created configuration Access Point but the configuration p
## Releases
#### 0.6
#### 0.9
- fixed support for encoded characters in ssid/pass
##### 0.8
- made it compile on older versions of ESP8266 core as well, tested down to 2.0.0
- added simple example for Custom IP
##### 0.7
- added static IP in station mode
- added example of persisting custom IP to FS config.json
- more option on portal homepage
- added on PlatformIO
##### 0.6
- custom parameters
- prettier
- on demand config portal
+45 -63
View File
@@ -16,7 +16,7 @@ WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placehold
_id = id;
_placeholder = placeholder;
_length = length;
_value = new char[length+1];
_value = new char[length + 1];
for (int i = 0; i < length; i++) {
_value[i] = 0;
}
@@ -98,6 +98,7 @@ void WiFiManager::setupConfigPortal() {
server->onNotFound (std::bind(&WiFiManager::handleNotFound, this));
server->begin(); // Web server start
DEBUG_WM(F("HTTP server started"));
}
boolean WiFiManager::autoConnect() {
@@ -129,6 +130,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
//setup AP
WiFi.mode(WIFI_AP);
DEBUG_WM("SET AP");
_apName = apName;
_apPassword = apPassword;
@@ -152,7 +154,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
connect = false;
delay(2000);
DEBUG_WM(F("Connecting to new AP"));
// using user-provided _ssid, _pass in place of system-stored ssid and pass
if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
DEBUG_WM(F("Failed to connect."));
@@ -166,8 +168,8 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
}
break;
}
if(_shouldBreakAfterConfig) {
if (_shouldBreakAfterConfig) {
//flag set to exit after config after trying to connect
//notify that configuration has changed and any optional parameters should be saved
if ( _savecallback != NULL) {
@@ -197,20 +199,32 @@ int WiFiManager::connectWifi(String ssid, String pass) {
DEBUG_WM(WiFi.localIP());
}
if(ssid !="" && pass != "") {
//check if we have ssid and pass and force those, if not, try with last saved values
if (ssid != "") {
WiFi.begin(ssid.c_str(), pass.c_str());
} else {
DEBUG_WM("Using last saved values, should be faster");
WiFi.begin();
}
int connRes = WiFi.waitForConnectResult();
DEBUG_WM ("Connection result: ");
DEBUG_WM ( connRes );
//not connected, WPS enabled, no pass - first attempt
if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
startWPS();
//should be connected at the end of WPS
connRes = WiFi.waitForConnectResult();
}
return connRes;
}
void WiFiManager::startWPS() {
DEBUG_WM("START WPS");
WiFi.beginWPSConfig();
DEBUG_WM("END WPS");
}
/*
String WiFiManager::getSSID() {
if (_ssid == "") {
DEBUG_WM(F("Reading SSID"));
@@ -230,50 +244,14 @@ String WiFiManager::getPassword() {
}
return _pass;
}
*/
String WiFiManager::getConfigPortalSSID() {
return _apName;
}
String WiFiManager::urldecode(const char *src)
{
String decoded = "";
char a, b;
while (*src) {
if ((*src == '%') &&
((a = src[1]) && (b = src[2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a' - 'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a' - 'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
decoded += char(16 * a + b);
src += 3;
} else if (*src == '+') {
decoded += ' ';
*src++;
} else {
decoded += *src;
*src++;
}
}
decoded += '\0';
return decoded;
}
void WiFiManager::resetSettings() {
DEBUG_WM(F("settings invalidated"));
DEBUG_WM(F("THIS MAY CAUSE AP NOT TO STRT UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
WiFi.disconnect(true);
//delay(200);
}
@@ -324,7 +302,7 @@ void WiFiManager::handleRoot() {
page += F("<h3>WiFiManager</h3>");
page += FPSTR(HTTP_PORTAL_OPTIONS);
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
}
@@ -404,8 +382,8 @@ void WiFiManager::handleWifi(boolean scan) {
server->sendContent_P("<br/>");
}
if(_sta_static_ip) {
if (_sta_static_ip) {
String item = FPSTR(HTTP_FORM_PARAM);
item.replace("{i}", "ip");
item.replace("{n}", "ip");
@@ -413,8 +391,8 @@ void WiFiManager::handleWifi(boolean scan) {
item.replace("{l}", "15");
item.replace("{v}", _sta_static_ip.toString());
server->sendContent(item);
server->sendContent(item);
item = FPSTR(HTTP_FORM_PARAM);
item.replace("{i}", "gw");
item.replace("{n}", "gw");
@@ -423,7 +401,7 @@ void WiFiManager::handleWifi(boolean scan) {
item.replace("{v}", _sta_static_gw.toString());
server->sendContent(item);
item = FPSTR(HTTP_FORM_PARAM);
item.replace("{i}", "sn");
item.replace("{n}", "sn");
@@ -431,7 +409,7 @@ void WiFiManager::handleWifi(boolean scan) {
item.replace("{l}", "15");
item.replace("{v}", _sta_static_sn.toString());
server->sendContent(item);
server->sendContent(item);
server->sendContent_P("<br/>");
}
@@ -449,8 +427,8 @@ void WiFiManager::handleWifiSave() {
DEBUG_WM(F("WiFi save"));
//SAVE/connect here
_ssid = urldecode(server->arg("s").c_str());
_pass = urldecode(server->arg("p").c_str());
_ssid = server->arg("s").c_str();
_pass = server->arg("p").c_str();
//parameters
for (int i = 0; i < _paramsCount; i++) {
@@ -458,7 +436,7 @@ void WiFiManager::handleWifiSave() {
break;
}
//read parameter
String value = urldecode(server->arg(_params[i]->getID()).c_str());
String value = server->arg(_params[i]->getID()).c_str();
//store it in array
value.toCharArray(_params[i]->_value, _params[i]->_length);
DEBUG_WM(F("Parameter"));
@@ -466,20 +444,24 @@ void WiFiManager::handleWifiSave() {
DEBUG_WM(value);
}
if(server->arg("ip") != "") {
if (server->arg("ip") != "") {
DEBUG_WM(F("static ip"));
DEBUG_WM(server->arg("ip"));
_sta_static_ip.fromString(server->arg("ip"));
//_sta_static_ip.fromString(server->arg("ip"));
String ip = server->arg("ip");
optionalIPFromString(&_sta_static_ip, ip.c_str());
}
if(server->arg("gw") != "") {
if (server->arg("gw") != "") {
DEBUG_WM(F("static gateway"));
DEBUG_WM(server->arg("gw"));
_sta_static_gw.fromString(server->arg("gw"));
String gw = server->arg("gw");
optionalIPFromString(&_sta_static_gw, gw.c_str());
}
if(server->arg("sn") != "") {
if (server->arg("sn") != "") {
DEBUG_WM(F("static netmask"));
DEBUG_WM(server->arg("sn"));
_sta_static_sn.fromString(server->arg("sn"));
String sn = server->arg("sn");
optionalIPFromString(&_sta_static_sn, sn.c_str());
}
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
@@ -509,7 +491,7 @@ void WiFiManager::handleWifiSave() {
void WiFiManager::handleInfo() {
DEBUG_WM(F("Info"));
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
@@ -535,7 +517,7 @@ String page = FPSTR(HTTP_HEAD);
page += F("</dd>");
page += F("</dl>");
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
DEBUG_WM(F("Sent info page"));
+26 -17
View File
@@ -66,15 +66,13 @@ class WiFiManager
//if you want to always start the config portal, without trying to connect first
boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
String getSSID();
String getPassword();
//String getSSID();
//String getPassword();
// get the AP name of the config portal, so it can be used in the callback
String getConfigPortalSSID();
void resetSettings();
//for convenience
String urldecode(const char*);
//sets timeout before webserver loop ends and exits even if there has been no setup.
//usefully for devices that failed to connect at some point and got stuck in a webserver loop
@@ -95,34 +93,38 @@ class WiFiManager
void addParameter(WiFiManagerParameter *p);
//if this is set, it will exit after config, even if connection is unsucessful.
void setBreakAfterConfig(boolean shouldBreak);
//if this is set, try WPS setup when starting (this will delay config portal for up to 2 mins)
private:
std::unique_ptr<DNSServer> dnsServer;
std::unique_ptr<ESP8266WebServer> server;
const int WM_DONE = 0;
const int WM_WAIT = 10;
//const int WM_DONE = 0;
//const int WM_WAIT = 10;
//const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
void setupConfigPortal();
void startWPS();
int _eepromStart;
const char* _apName = "no-net";
const char* _apPassword = NULL;
String _ssid = "";
String _pass = "";
unsigned long timeout = 0;
unsigned long start = 0;
const char* _apName = "no-net";
const char* _apPassword = NULL;
String _ssid = "";
String _pass = "";
unsigned long timeout = 0;
unsigned long start = 0;
IPAddress _ap_static_ip;
IPAddress _ap_static_gw;
IPAddress _ap_static_sn;
IPAddress _sta_static_ip;
IPAddress _sta_static_gw;
IPAddress _sta_static_sn;
int _paramsCount = 0;
int _minimumQuality = -1;
int _paramsCount = 0;
int _minimumQuality = -1;
boolean _shouldBreakAfterConfig = false;
boolean _tryWPS = false;
//String getEEPROMString(int start, int len);
//void setEEPROMString(int start, int len, String string);
@@ -157,8 +159,15 @@ class WiFiManager
template <typename Generic>
void DEBUG_WM(Generic text);
template <class T>
auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) {
return obj->fromString(s);
}
auto optionalIPFromString(...) -> bool {
DEBUG_WM("NO fromString METHOD ON IPAddress, you need ESP8266 core 2.1.0 or newer for Custom IP configuration to work.");
return false;
}
};
#endif
@@ -0,0 +1,75 @@
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
/**************************************************************************************
* this example shows how to set a static IP configuration for the ESP
* although the IP shows in the config portal, the changes will revert
* to the IP set in the source file.
* if you want the ability to configure and persist the new IP configuration
* look at the FS examples, which save the config to file
*************************************************************************************/
//default custom static IP
//char static_ip[16] = "10.0.1.59";
//char static_gw[16] = "10.0.1.1";
//char static_sn[16] = "255.255.255.0";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//set static ip
//the commented bit only works for ESP8266 core 2.1.0 or newer
/*IPAddress _ip,_gw,_sn;
_ip.fromString(static_ip);
_gw.fromString(static_gw);
_sn.fromString(static_sn);
*/
IPAddress _ip = IPAddress(10, 0, 1, 78);
IPAddress _gw = IPAddress(10, 0, 1, 1);
IPAddress _sn = IPAddress(255, 255, 255, 0);
wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
//tries to connect to last known settings
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP" with password "password"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
Serial.println("local ip");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}
+1 -1
View File
@@ -9,5 +9,5 @@
},
"frameworks": "arduino",
"platforms": "espressif",
"version": "0.6"
"version": "0.9"
}
+1 -1
View File
@@ -1,5 +1,5 @@
name=WiFiManager
version=0.7
version=0.9
author=tzapu
maintainer=tzapu
sentence=ESP8266 WiFi Connection manager with fallback web configuration portal