mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-04 02:48:13 +10:00
Added support for additional parameters
This commit is contained in:
+123
-1
@@ -13,7 +13,95 @@
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
const char* WiFiManagerParameter::getValue(){
|
||||
return _value;
|
||||
}
|
||||
const char* WiFiManagerParameter::getID(){
|
||||
return _id;
|
||||
}
|
||||
const char* WiFiManagerParameter::getPlaceholder(){
|
||||
return _placeholder;
|
||||
}
|
||||
int WiFiManagerParameter::getValueLength(){
|
||||
return _length;
|
||||
}
|
||||
|
||||
WiFiManager::WiFiManager() {
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++)
|
||||
_params[i] = NULL;
|
||||
_eepromLength = 0;
|
||||
}
|
||||
|
||||
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::loadParameters(){
|
||||
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++)
|
||||
_params[i]->_value[j] = EEPROM.read(pos++);
|
||||
_params[i]->_value[l-1] = 0;
|
||||
DEBUG_PRINT(F("Parameter loaded"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(_params[i]->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void WiFiManager::begin() {
|
||||
@@ -258,6 +346,9 @@ void WiFiManager::resetSettings() {
|
||||
DEBUG_PRINT(F("THIS MAY CAUSE AP NOT TO STRT UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
|
||||
WiFi.disconnect(true);
|
||||
//delay(200);
|
||||
|
||||
//overwrite the current parameter values (with defaults)
|
||||
saveParameters();
|
||||
}
|
||||
|
||||
void WiFiManager::setTimeout(unsigned long seconds) {
|
||||
@@ -353,7 +444,26 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
}
|
||||
}
|
||||
|
||||
server->sendContent_P(HTTP_FORM);
|
||||
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)
|
||||
break;
|
||||
String pitem = FPSTR(HTTP_FORM_PARAM);
|
||||
pitem.replace("{i}", _params[i]->getID());
|
||||
pitem.replace("{n}", _params[i]->getID());
|
||||
pitem.replace("{p}", _params[i]->getPlaceholder());
|
||||
snprintf(parLength, 2, "%d", _params[i]->getValueLength());
|
||||
pitem.replace("{l}", parLength);
|
||||
pitem.replace("{v}", _params[i]->getValue());
|
||||
|
||||
server->sendContent(pitem);
|
||||
}
|
||||
if(_params[0]!=NULL)
|
||||
server->sendContent_P("<br/>");
|
||||
|
||||
server->sendContent_P(HTTP_FORM_END);
|
||||
server->sendContent_P(HTTP_END);
|
||||
server->client().stop();
|
||||
|
||||
@@ -368,6 +478,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)
|
||||
break;
|
||||
String value = urldecode(server->arg(_params[i]->getID()).c_str());
|
||||
value.toCharArray(_params[i]->_value, _params[i]->_length);
|
||||
DEBUG_PRINT(F("Parameter"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(value);
|
||||
}
|
||||
saveParameters();
|
||||
|
||||
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server->sendHeader("Pragma", "no-cache");
|
||||
server->sendHeader("Expires", "-1");
|
||||
|
||||
+31
-2
@@ -14,7 +14,7 @@
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
//#include <EEPROM.h>
|
||||
#include <EEPROM.h>
|
||||
//#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <DNSServer.h>
|
||||
@@ -29,10 +29,32 @@ const char HTTP_HEAD_END[] PROGMEM = "</head><body><div style='text-align: left;
|
||||
const char HTTP_PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form>";
|
||||
const char HTTP_ITEM[] PROGMEM = "<div><a href='#' onclick='c(this)'>{v}</a> {r}% {i}</div>";
|
||||
const char HTTP_ITEM_PADLOCK[] PROGMEM = "<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAKCAMAAAC+Ge+yAAAACVBMVEUAAAC7u7v///9etNz6AAAAI0lEQVQI133GsQ3AMAAEoXv2H9qNJacKFbEaWTQJ+uQB/nMdEaYAXeJkZ9AAAAAASUVORK5CYII='/>";
|
||||
const char HTTP_FORM[] PROGMEM = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><br/><input id='p' name='p' length=64 placeholder='password'><br/><br/><button type='submit'>save</button></form>";
|
||||
const char HTTP_FORM_START[] PROGMEM = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><br/><input id='p' name='p' length=64 placeholder='password'><br/>";
|
||||
const char HTTP_FORM_PARAM[] PROGMEM = "<br/><input id='{i}' name='{n}' length={l} placeholder='{p}' value='{v}'>";
|
||||
const char HTTP_FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></form>";
|
||||
|
||||
const char HTTP_SAVED[] PROGMEM = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
|
||||
const char HTTP_END[] PROGMEM = "</div></body></html>";
|
||||
|
||||
#define WIFI_MANAGER_MAX_PARAMS 6
|
||||
|
||||
class WiFiManagerParameter {
|
||||
public:
|
||||
WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length);
|
||||
|
||||
const char* getID();
|
||||
const char* getValue();
|
||||
const char* getPlaceholder();
|
||||
int getValueLength();
|
||||
private:
|
||||
const char* _id;
|
||||
const char* _placeholder;
|
||||
char* _value;
|
||||
int _length;
|
||||
|
||||
friend class WiFiManager;
|
||||
};
|
||||
|
||||
|
||||
class WiFiManager
|
||||
{
|
||||
@@ -60,6 +82,10 @@ public:
|
||||
void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
|
||||
void setAPCallback( void (*func)(void) );
|
||||
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
|
||||
void loadParameters();
|
||||
void saveParameters();
|
||||
private:
|
||||
std::unique_ptr<DNSServer> dnsServer;
|
||||
std::unique_ptr<ESP8266WebServer> server;
|
||||
@@ -123,6 +149,9 @@ private:
|
||||
|
||||
void (*_apcallback)(void) = NULL;
|
||||
|
||||
WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS];
|
||||
int _eepromLength;
|
||||
|
||||
template <typename Generic>
|
||||
void DEBUG_PRINT(Generic text);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user