mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-13 14:51:21 +10:00
modified custom parameters feature so user can provide their own storage mechanism
added FS json storage of parameters example
This commit is contained in:
+131
-111
@@ -1,58 +1,69 @@
|
||||
/**************************************************************
|
||||
* WiFiManager is a library for the ESP8266/Arduino platform
|
||||
* (https://github.com/esp8266/Arduino) to enable easy
|
||||
* configuration and reconfiguration of WiFi credentials and
|
||||
* store them in EEPROM.
|
||||
* inspired by:
|
||||
* http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
* https://github.com/chriscook8/esp-arduino-apboot
|
||||
* https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced
|
||||
* Built by AlexT https://github.com/tzapu
|
||||
* Licensed under MIT license
|
||||
WiFiManager is a library for the ESP8266/Arduino platform
|
||||
(https://github.com/esp8266/Arduino) to enable easy
|
||||
configuration and reconfiguration of WiFi credentials and
|
||||
store them in EEPROM.
|
||||
inspired by:
|
||||
http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
https://github.com/chriscook8/esp-arduino-apboot
|
||||
https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced
|
||||
Built by AlexT https://github.com/tzapu
|
||||
Licensed under MIT license
|
||||
**************************************************************/
|
||||
|
||||
#include "WiFiManager.h"
|
||||
|
||||
WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length){
|
||||
_id = id;
|
||||
_placeholder = placeholder;
|
||||
_length = length;
|
||||
_value = new char[length];
|
||||
for(int i=0; i<length; i++)
|
||||
_value[i] = 0;
|
||||
if(defaultValue!=NULL)
|
||||
strncpy(_value, defaultValue, length);
|
||||
WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length) {
|
||||
_id = id;
|
||||
_placeholder = placeholder;
|
||||
_length = length;
|
||||
_value = new char[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
_value[i] = 0;
|
||||
}
|
||||
if (defaultValue != NULL) {
|
||||
strncpy(_value, defaultValue, length);
|
||||
}
|
||||
}
|
||||
|
||||
const char* WiFiManagerParameter::getValue(){
|
||||
return _value;
|
||||
const char* WiFiManagerParameter::getValue() {
|
||||
return _value;
|
||||
}
|
||||
const char* WiFiManagerParameter::getID(){
|
||||
return _id;
|
||||
const char* WiFiManagerParameter::getID() {
|
||||
return _id;
|
||||
}
|
||||
const char* WiFiManagerParameter::getPlaceholder(){
|
||||
return _placeholder;
|
||||
const char* WiFiManagerParameter::getPlaceholder() {
|
||||
return _placeholder;
|
||||
}
|
||||
int WiFiManagerParameter::getValueLength(){
|
||||
return _length;
|
||||
int WiFiManagerParameter::getValueLength() {
|
||||
return _length;
|
||||
}
|
||||
|
||||
WiFiManager::WiFiManager() {
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++)
|
||||
_params[i] = NULL;
|
||||
_eepromLength = 0;
|
||||
//for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++)
|
||||
// _params[i] = NULL;
|
||||
}
|
||||
|
||||
void WiFiManager::addParameter(WiFiManagerParameter *p){
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL){
|
||||
_params[i] = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void WiFiManager::addParameter(WiFiManagerParameter *p) {
|
||||
/*for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL){
|
||||
_params[i] = p;
|
||||
_paramsCount++;
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
//if(_params[_paramsCount] == NULL){
|
||||
_params[_paramsCount] = p;
|
||||
_paramsCount++;
|
||||
DEBUG_PRINT("Adding parameter");
|
||||
DEBUG_PRINT(p->getID());
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
void WiFiManager::loadParameters(){
|
||||
/*
|
||||
void WiFiManager::loadParameters() {
|
||||
DEBUG_PRINT("Load parameters - not needed");
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
@@ -78,30 +89,33 @@ void WiFiManager::loadParameters(){
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(_params[i]->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
void WiFiManager::saveParameters() {
|
||||
DEBUG_PRINT("save params callback");
|
||||
/*
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
_eepromLength+= _params[i]->getValueLength();
|
||||
}
|
||||
DEBUG_PRINT(F("EEPROM initialized with length "));
|
||||
DEBUG_PRINT(_eepromLength);
|
||||
|
||||
void WiFiManager::saveParameters(){
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
_eepromLength+= _params[i]->getValueLength();
|
||||
}
|
||||
DEBUG_PRINT(F("EEPROM initialized with length "));
|
||||
DEBUG_PRINT(_eepromLength);
|
||||
|
||||
EEPROM.begin(_eepromLength);
|
||||
}
|
||||
int pos = 0;
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
int l = _params[i]->getValueLength();
|
||||
for(int j=0; j<l; j++)
|
||||
EEPROM.write(pos++, _params[i]->_value[j]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
EEPROM.begin(_eepromLength);
|
||||
}
|
||||
int pos = 0;
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
int l = _params[i]->getValueLength();
|
||||
for(int j=0; j<l; j++)
|
||||
EEPROM.write(pos++, _params[i]->_value[j]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
*/
|
||||
}
|
||||
|
||||
void WiFiManager::begin() {
|
||||
@@ -109,13 +123,13 @@ void WiFiManager::begin() {
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName) {
|
||||
begin(apName,NULL);
|
||||
begin(apName, NULL);
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
dnsServer.reset(new DNSServer());
|
||||
server.reset(new ESP8266WebServer(80));
|
||||
|
||||
|
||||
DEBUG_PRINT(F(""));
|
||||
_apName = apName;
|
||||
_apPasswd = apPasswd;
|
||||
@@ -124,9 +138,9 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F("Configuring access point... "));
|
||||
DEBUG_PRINT(_apName);
|
||||
if (_apPasswd != NULL) {
|
||||
if(strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) {
|
||||
// fail passphrase to short or long!
|
||||
DEBUG_PRINT(F("Invalid AccessPoint password"));
|
||||
if (strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) {
|
||||
// fail passphrase to short or long!
|
||||
DEBUG_PRINT(F("Invalid AccessPoint password"));
|
||||
}
|
||||
DEBUG_PRINT(_apPasswd);
|
||||
}
|
||||
@@ -147,7 +161,7 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F("AP IP address: "));
|
||||
DEBUG_PRINT(WiFi.softAPIP());
|
||||
|
||||
/* Setup the DNS server redirecting all the domains to the apIP */
|
||||
/* Setup the DNS server redirecting all the domains to the apIP */
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
|
||||
|
||||
@@ -165,24 +179,24 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
|
||||
boolean WiFiManager::autoConnect() {
|
||||
String ssid = "ESP" + String(ESP.getChipId());
|
||||
return autoConnect(ssid.c_str(),NULL);
|
||||
return autoConnect(ssid.c_str(), NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName) {
|
||||
return autoConnect(apName,NULL);
|
||||
return autoConnect(apName, NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F(""));
|
||||
DEBUG_PRINT(F("AutoConnect"));
|
||||
|
||||
|
||||
// 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();
|
||||
@@ -192,26 +206,26 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
//connected
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//not connected
|
||||
//setup AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
//notify we entered AP mode
|
||||
if( _apcallback != NULL) {
|
||||
if ( _apcallback != NULL) {
|
||||
_apcallback();
|
||||
}
|
||||
connect = false;
|
||||
begin(apName,apPasswd);
|
||||
begin(apName, apPasswd);
|
||||
|
||||
bool looping = true;
|
||||
while(timeout == 0 || millis() < start + timeout) {
|
||||
while (timeout == 0 || millis() < start + timeout) {
|
||||
//DNS
|
||||
dnsServer->processNextRequest();
|
||||
//HTTP
|
||||
server->handleClient();
|
||||
|
||||
|
||||
if(connect) {
|
||||
|
||||
if (connect) {
|
||||
delay(2000);
|
||||
DEBUG_PRINT(F("Connecting to new AP"));
|
||||
connect = false;
|
||||
@@ -226,13 +240,13 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
//delay(1000);
|
||||
//return false;
|
||||
} else {
|
||||
//connected
|
||||
//connected
|
||||
WiFi.mode(WIFI_STA);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
yield();
|
||||
yield();
|
||||
}
|
||||
|
||||
server.reset();
|
||||
@@ -272,7 +286,7 @@ String WiFiManager::getPassword() {
|
||||
}
|
||||
|
||||
/*
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
String string = "";
|
||||
@@ -282,10 +296,10 @@ String WiFiManager::getEEPROMString(int start, int len) {
|
||||
}
|
||||
EEPROM.end();
|
||||
return string;
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
int si = 0;
|
||||
@@ -303,7 +317,7 @@ void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
}
|
||||
EEPROM.end();
|
||||
DEBUG_PRINT(F("Wrote " + string);
|
||||
}*/
|
||||
}*/
|
||||
|
||||
String WiFiManager::urldecode(const char *src)
|
||||
{
|
||||
@@ -348,7 +362,7 @@ void WiFiManager::resetSettings() {
|
||||
//delay(200);
|
||||
|
||||
//overwrite the current parameter values (with defaults)
|
||||
saveParameters();
|
||||
//saveParameters();
|
||||
}
|
||||
|
||||
void WiFiManager::setTimeout(unsigned long seconds) {
|
||||
@@ -392,7 +406,7 @@ void WiFiManager::handleRoot() {
|
||||
server->sendContent(title);
|
||||
server->sendContent(F("<h3>WiFiManager</h3>"));
|
||||
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_PORTAL_OPTIONS);
|
||||
server->sendContent_P(HTTP_END);
|
||||
|
||||
@@ -431,7 +445,7 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
rssiQ += getRSSIasQuality(WiFi.RSSI(i));
|
||||
item.replace("{v}", WiFi.SSID(i));
|
||||
item.replace("{r}", rssiQ);
|
||||
if(WiFi.encryptionType(i) != ENC_TYPE_NONE) {
|
||||
if (WiFi.encryptionType(i) != ENC_TYPE_NONE) {
|
||||
item.replace("{i}", FPSTR(HTTP_ITEM_PADLOCK));
|
||||
} else {
|
||||
item.replace("{i}", "");
|
||||
@@ -443,13 +457,15 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
server->sendContent("<br/>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_FORM_START);
|
||||
char parLength[2];
|
||||
// add the extra parameters to the form
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
for (int i = 0; i < _paramsCount; i++) {
|
||||
if (_params[i] == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
String pitem = FPSTR(HTTP_FORM_PARAM);
|
||||
pitem.replace("{i}", _params[i]->getID());
|
||||
pitem.replace("{n}", _params[i]->getID());
|
||||
@@ -460,14 +476,15 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
|
||||
server->sendContent(pitem);
|
||||
}
|
||||
if(_params[0]!=NULL)
|
||||
if (_params[0] != NULL) {
|
||||
server->sendContent_P("<br/>");
|
||||
}
|
||||
|
||||
server->sendContent_P(HTTP_FORM_END);
|
||||
server->sendContent_P(HTTP_END);
|
||||
server->client().stop();
|
||||
|
||||
DEBUG_PRINT(F("Sent config page"));
|
||||
|
||||
DEBUG_PRINT(F("Sent config page"));
|
||||
}
|
||||
|
||||
/** Handle the WLAN save form and redirect to WLAN config page again */
|
||||
@@ -478,15 +495,18 @@ void WiFiManager::handleWifiSave() {
|
||||
_ssid = urldecode(server->arg("s").c_str());
|
||||
_pass = urldecode(server->arg("p").c_str());
|
||||
|
||||
//parameter
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
//parameters
|
||||
for (int i = 0; i < _paramsCount; i++) {
|
||||
if (_params[i] == NULL) {
|
||||
break;
|
||||
}
|
||||
//read parameter
|
||||
String value = urldecode(server->arg(_params[i]->getID()).c_str());
|
||||
//store it in array
|
||||
value.toCharArray(_params[i]->_value, _params[i]->_length);
|
||||
DEBUG_PRINT(F("Parameter"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(value);
|
||||
DEBUG_PRINT(F("Parameter"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(value);
|
||||
}
|
||||
saveParameters();
|
||||
|
||||
@@ -501,19 +521,19 @@ void WiFiManager::handleWifiSave() {
|
||||
server->sendContent_P(HTTP_SCRIPT);
|
||||
server->sendContent_P(HTTP_STYLE);
|
||||
server->sendContent_P(HTTP_HEAD_END);
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_SAVED);
|
||||
|
||||
server->sendContent_P(HTTP_END);
|
||||
server->client().stop();
|
||||
|
||||
DEBUG_PRINT(F("Sent wifi save page"));
|
||||
|
||||
|
||||
DEBUG_PRINT(F("Sent wifi save page"));
|
||||
|
||||
connect = true; //signal ready to connect/reset
|
||||
}
|
||||
|
||||
void WiFiManager::handle204() {
|
||||
DEBUG_PRINT(F("204 No Response"));
|
||||
DEBUG_PRINT(F("204 No Response"));
|
||||
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server->sendHeader("Pragma", "no-cache");
|
||||
server->sendHeader("Expires", "-1");
|
||||
@@ -563,9 +583,9 @@ void WiFiManager::setAPCallback( void (*func)(void) ) {
|
||||
|
||||
template <typename Generic>
|
||||
void WiFiManager::DEBUG_PRINT(Generic text) {
|
||||
if(_debug) {
|
||||
if (_debug) {
|
||||
Serial.print("*WM: ");
|
||||
Serial.println(text);
|
||||
Serial.println(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,12 +593,12 @@ void WiFiManager::DEBUG_PRINT(Generic text) {
|
||||
int WiFiManager::getRSSIasQuality(int RSSI) {
|
||||
int quality = 0;
|
||||
|
||||
if(RSSI <= -100){
|
||||
quality = 0;
|
||||
}else if(RSSI >= -50){
|
||||
quality = 100;
|
||||
if (RSSI <= -100) {
|
||||
quality = 0;
|
||||
} else if (RSSI >= -50) {
|
||||
quality = 100;
|
||||
} else {
|
||||
quality = 2 * (RSSI + 100);
|
||||
quality = 2 * (RSSI + 100);
|
||||
}
|
||||
return quality;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user