adding menu control, in progress

This commit is contained in:
Shawn A
2018-02-21 17:11:57 -06:00
parent 4e57d74e87
commit c2bdbfafb9
3 changed files with 88 additions and 18 deletions
+47 -9
View File
@@ -145,7 +145,7 @@ WiFiManager::WiFiManager():_debugPort(Serial) {
//parameters
// @todo belongs to wifimanagerparameter
_max_params = WIFI_MANAGER_MAX_PARAMS;
_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
}
// destructor
@@ -180,6 +180,8 @@ boolean WiFiManager::autoConnect() {
boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
DEBUG_WM(F("AutoConnect"));
Serial.println(getMenuOut());
// attempt to connect using saved settings, on fail fallback to AP config portal
WiFi.enableSTA(true);
_usermode = WIFI_STA;
@@ -343,10 +345,11 @@ void WiFiManager::setupConfigPortal() {
server->on((String)F("/wifi"), std::bind(&WiFiManager::handleWifi, this, true));
server->on((String)F("/0wifi"), std::bind(&WiFiManager::handleWifi, this, false));
server->on((String)F("/wifisave"), std::bind(&WiFiManager::handleWifiSave, this));
server->on((String)F("/i"), std::bind(&WiFiManager::handleInfo, this));
server->on((String)F("/r"), std::bind(&WiFiManager::handleReset, this));
server->on((String)F("/info"), std::bind(&WiFiManager::handleInfo, this));
server->on((String)F("/param"), std::bind(&WiFiManager::handleParam, this));
server->on((String)F("/restart"), std::bind(&WiFiManager::handleReset, this));
server->on((String)F("/exit"), std::bind(&WiFiManager::handleExit, this));
// server->on((String)F("/exit"), std::bind(&WiFiManager::stopCaptivePortal, this));
server->on((String)F("/close"), std::bind(&WiFiManager::stopCaptivePortal, this));
server->on((String)F("/erase"), std::bind(&WiFiManager::handleErase, this));
server->on((String)F("/status"), std::bind(&WiFiManager::handleWiFiStatus, this));
//server->on("/fwlink", std::bind(&WiFiManager::handleRoot, this)); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
@@ -423,7 +426,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
break;
}
state = handleConfigPortal();
state = processConfigPortal();
// status change, break
if(state != WL_IDLE_STATUS){
@@ -445,14 +448,14 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
*/
boolean WiFiManager::process(){
if(webPortalActive || (configPortalActive && !_configPortalIsBlocking)){
uint8_t state = handleConfigPortal();
uint8_t state = processConfigPortal();
return state == WL_CONNECTED;
}
return false;
}
//using esp enums returns for now, should be fine
uint8_t WiFiManager::handleConfigPortal(){
uint8_t WiFiManager::processConfigPortal(){
//DNS handler
dnsServer->processNextRequest();
//HTTP handler
@@ -672,6 +675,7 @@ void WiFiManager::handleRoot() {
str.replace(FPSTR(T_v),configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading
page += str;
page += FPSTR(HTTP_PORTAL_OPTIONS);
page += getMenuOut();
reportStatus(page);
page += FPSTR(HTTP_END);
@@ -695,8 +699,7 @@ void WiFiManager::handleWifi(boolean scan) {
page += pitem;
page += getStaticOut();
page += getParamOut();
if(_paramsInWifi) page += getParamOut();
page += FPSTR(HTTP_FORM_END);
page += FPSTR(HTTP_SCAN_LINK);
reportStatus(page);
@@ -711,6 +714,38 @@ void WiFiManager::handleWifi(boolean scan) {
DEBUG_WM(F("Sent config page"));
}
/**
* HTTPD CALLBACK Wifi param page handler
*/
void WiFiManager::handleParam(){
DEBUG_WM(F("<- HTTP Param"));
handleRequest();
String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi
page += getParamOut();
page += FPSTR(HTTP_FORM_END);
reportStatus(page);
page += FPSTR(HTTP_END);
server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
server->send(200, FPSTR(HTTP_HEAD_CT), page);
DEBUG_WM(F("Sent param page"));
}
String WiFiManager::getMenuOut(){
String page;
int i;
for(i=0;i<sizeof(_menuIds);i++){
if(i == MENU_PARAM){
if(_paramsCount == 0) continue;
}
page += HTTP_PORTAL_MENU[_menuIds[i]];
}
return page;
}
String WiFiManager::getScanItemOut(){
String page;
@@ -1665,6 +1700,9 @@ bool WiFiManager::setHostname(const char * hostname){
return true;
}
void WiFiManager::setMenu(uint8_t menu[]){
// _menuIds = menu;
}
// GETTERS
+23 -4
View File
@@ -102,6 +102,18 @@ class WiFiManager
WiFiManager();
~WiFiManager();
typedef enum {
MENU_WIFI = 0,
MENU_WIFINOSCAN = 1,
MENU_INFO = 2,
MENU_PARAM = 3,
MENU_CLOSE = 4,
MENU_RESTART = 5,
MENU_EXIT = 6,
MENU_ERASE = 7,
MENU_SEP = 8
} menu_page_t;
// auto connect to saved wifi, or custom, and start config portal on failures
boolean autoConnect();
boolean autoConnect(char const *apName, char const *apPassword = NULL);
@@ -173,6 +185,8 @@ class WiFiManager
void setScanDispPerc(boolean enabled);
// set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266
bool setHostname(const char * hostname);
// set custom menu
void setMenu(uint8_t menu[]);
// get last connection result, includes autoconnect and wifisave
uint8_t getLastConxResult();
@@ -195,6 +209,9 @@ class WiFiManager
#endif
std::unique_ptr<WM_WebServer> server;
// std:vector<uint8_t> _menuids;
uint8_t _menuIds[7] = {MENU_WIFI,MENU_INFO,MENU_PARAM,MENU_SEP,MENU_CLOSE,MENU_ERASE,MENU_EXIT};
// ip configs
IPAddress _ap_static_ip;
IPAddress _ap_static_gw;
@@ -217,14 +234,14 @@ class WiFiManager
unsigned long _webPortalAccessed = 0; // ms last web access time
WiFiMode_t _usermode = WIFI_OFF;
String _wifissidprefix = FPSTR(S_ssidpre); // auto apname prefix prefix+chipid
uint8_t _lastconxresult = WL_IDLE_STATUS;
uint8_t _lastconxresult = WL_IDLE_STATUS;
#ifdef ESP32
static uint8_t _lastconxresulttmp; // tmp var for esp32 callback
#endif
#ifndef WL_STATION_WRONG_PASSWORD
uint8_t WL_STATION_WRONG_PASSWORD = 7;
uint8_t WL_STATION_WRONG_PASSWORD = 7; // @kludge define a WL status for wrong password
#endif
// option parameters
@@ -240,6 +257,7 @@ class WiFiManager
boolean _cpClientCheck = false; // keep cp alive if cp have station
boolean _webClientCheck = true; // keep cp alive if web have client
boolean _scanDispOptions = false; // show percentage in scans not icons
boolean _paramsInWifi = true; // show custom parameters on wifi page
const char * _hostname = "";
const char* _customHeadElement = ""; // store custom head element html from user
@@ -261,13 +279,14 @@ class WiFiManager
void handleNotFound();
void handleExit();
void handleErase();
void handleParam();
void handleWiFiStatus();
void handleRequest();
boolean captivePortal();
boolean configPortalHasTimeout();
boolean stopConfigPortal();
uint8_t handleConfigPortal();
uint8_t processConfigPortal();
void stopCaptivePortal();
// wifi platform abstractions
@@ -291,7 +310,7 @@ class WiFiManager
String getScanItemOut();
String getStaticOut();
String getHTTPHead(String title);
String getMenuOut();
//helpers
int getRSSIasQuality(int RSSI);
boolean isIp(String str);
+18 -5
View File
@@ -22,8 +22,20 @@ const char HTTP_SCRIPT[] PROGMEM = "<script>function c(l){document.get
const char HTTP_HEAD_END[] PROGMEM = "</head><body><div class='wrap'>";
const char HTTP_ROOT_MAIN[] PROGMEM = "<h1>{v}</h1><h3>WiFiManager</h3>";
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><br/><form action='/i' method='GET'><button>Info</button></form><br/><form action='/r' method='GET'><button>Restart</button></form><br/><form action='/exit' method='GET'><button>Quit</button></form>";
const char * const HTTP_PORTAL_MENU[] PROGMEM = {
"<form action='/wifi' method='GET'><button>Configure WiFi</button></form><br/>\n", // MENU_WIFI
"<form action='/0wifi' method='GET'><button>Configure WiFi (No Scan)</button></form><br/>\n", // MENU_WIFINOSCAN
"<form action='/info' method='GET'><button>Info</button></form><br/>\n", // MENU_INFO
"<form action='/param' method='GET'><button>Setup</button></form><br/>\n",//MENU_PARAM
"<form action='/close' method='GET'><button>Close</button></form><br/>\n", // MENU_CLOSE
"<form action='/restart' method='GET'><button>Restart</button></form><br/>\n",// MENU_RESTART
"<form action='/exit' method='GET'><button>Quit</button></form>", // MENU_EXIT
"<form action='/erase' method='GET'><button>Erase</button></form>", // MENU_ERASE
"<hr>" // MENU_SEP
};
// const char HTTP_PORTAL_OPTIONS[] PROGMEM = strcat(HTTP_PORTAL_MENU[0] , HTTP_PORTAL_MENU[3] , HTTP_PORTAL_MENU[7]);
const char HTTP_PORTAL_OPTIONS[] PROGMEM = "";
const char HTTP_ITEM_QI[] PROGMEM = "<div role='img' aria-label='{r}%' title='{r}%' class='q q-{q} {i} {h}'></div>"; // rssi icons
const char HTTP_ITEM_QP[] PROGMEM = "<div class='q {h}'>{r}%</div>"; // rssi percentage
const char HTTP_ITEM[] PROGMEM = "<div><a href='#p' onclick='c(this)'>{v}</a>{qi}{qp}</div>"; // {q} = HTTP_ITEM_QI, {r} = HTTP_ITEM_QP
@@ -80,13 +92,13 @@ const char HTTP_HELP[] PROGMEM =
"<td>Show WiFi scan results and enter WiFi configuration.(/0wifi noscan)</td></tr>"
"<tr><td><a href='/wifisave'>/wifisave</a></td>"
"<td>Save WiFi configuration information and configure device. Needs variables supplied.</td></tr>"
"<tr><td><a href='/close'>/close</a></td>"
"<td>Close the configuration server and configuration WiFi network.</td></tr>"
"<tr><td><a href='/i'>/i</a></td>"
"<tr><td><a href='/info'>/i</a></td>"
"<td>Information page</td></tr>"
"<tr><td><a href='/close'>/close</a></td>"
"<td>Close the captiveportal popup,configportal will remain active</td></tr>"
"<tr><td><a href='/exit'>/exit</a></td>"
"<td>Exit Config Portal, configportal will close</td></tr>"
"<tr><td><a href='/r'>/r</a></td>"
"<tr><td><a href='/restart'>/r</a></td>"
"<td>Reboot the device</td></tr>"
"<tr><td><a href='/erase'>/erase</a></td>"
"<td>Erase WiFi configuration and reboot Device. Device will not reconnect to a network until new WiFi configuration data is entered.</td></tr>"
@@ -162,6 +174,7 @@ const char S_NA[] PROGMEM = "Unknown";
const char S_titlewifisaved[] PROGMEM = "Credentials Saved";
const char S_titlewifi[] PROGMEM = "Config ESP";
const char S_titleinfo[] PROGMEM = "Info";
const char S_titleparam[] PROGMEM = "Setup";
const char S_titleexit[] PROGMEM = "Exit";
const char S_titlereset[] PROGMEM = "Reset";
const char S_titleerase[] PROGMEM = "Erase";