From 696255c1f70a02ab396d9ffa86943f9311a0fef8 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 12:36:16 -0800 Subject: [PATCH 01/12] #557 - Endpoint to restart device over http(s) --- src/meshwifi/meshhttp.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 2ac81b1c9..32491d00d 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -56,6 +56,7 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res); void handleStaticBrowse(HTTPRequest *req, HTTPResponse *res); void handleStaticPost(HTTPRequest *req, HTTPResponse *res); void handleStatic(HTTPRequest *req, HTTPResponse *res); +void handleRestart(HTTPRequest *req, HTTPResponse *res); void handle404(HTTPRequest *req, HTTPResponse *res); void handleFormUpload(HTTPRequest *req, HTTPResponse *res); @@ -70,10 +71,11 @@ uint32_t timeSpeedUp = 0; // We need to specify some content-type mapping, so the resources get delivered with the // right content type and are displayed correctly in the browser -char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"}, {".js", "text/javascript"}, - {".png", "image/png"}, {".jpg", "image/jpg"}, {".gz", "application/gzip"}, - {".gif", "image/gif"}, {".json", "application/json"}, {".css", "text/css"}, - {"", ""}}; +char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"}, + {".js", "text/javascript"}, {".png", "image/png"}, + {".jpg", "image/jpg"}, {".gz", "application/gzip"}, + {".gif", "image/gif"}, {".json", "application/json"}, + {".css", "text/css"}, {"", ""}}; void handleWebResponse() { @@ -233,6 +235,7 @@ void initWebServer() ResourceNode *nodeStaticBrowse = new ResourceNode("/static", "GET", &handleStaticBrowse); ResourceNode *nodeStaticPOST = new ResourceNode("/static", "POST", &handleStaticPost); ResourceNode *nodeStatic = new ResourceNode("/static/*", "GET", &handleStatic); + ResourceNode *nodeRestart = new ResourceNode("/restart", "GET", &handleRestart); ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); @@ -246,6 +249,7 @@ void initWebServer() secureServer->registerNode(nodeStaticBrowse); secureServer->registerNode(nodeStaticPOST); secureServer->registerNode(nodeStatic); + secureServer->registerNode(nodeRestart); secureServer->setDefaultNode(node404); secureServer->setDefaultNode(nodeFormUpload); @@ -261,6 +265,7 @@ void initWebServer() insecureServer->registerNode(nodeStaticBrowse); insecureServer->registerNode(nodeStaticPOST); insecureServer->registerNode(nodeStatic); + insecureServer->registerNode(nodeRestart); insecureServer->setDefaultNode(node404); insecureServer->setDefaultNode(nodeFormUpload); @@ -512,7 +517,6 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res) // Get access to the parameters ResourceParameters *params = req->getParams(); - std::string parameter1; // Print the first parameter value if (params->getPathParameter(0, parameter1)) { @@ -560,7 +564,7 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res) cTypeIdx += 1; } while (strlen(contentTypes[cTypeIdx][0]) > 0); - if(!has_set_content_type) { + if (!has_set_content_type) { // Set a default content type res->setHeader("Content-Type", "application/octet-stream"); } @@ -845,9 +849,9 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) "mt_session=" + httpsserver::intToString(random(1, 9999999)) + "; Expires=Wed, 20 Apr 2049 4:20:00 PST"); std::string cookie = req->getHeader("Cookie"); - //String cookieString = cookie.c_str(); - //uint8_t nameIndex = cookieString.indexOf("mt_session"); - //DEBUG_MSG(cookie.c_str()); + // String cookieString = cookie.c_str(); + // uint8_t nameIndex = cookieString.indexOf("mt_session"); + // DEBUG_MSG(cookie.c_str()); std::string filename = "/static/index.html"; std::string filenameGzip = "/static/index.html.gz"; @@ -860,7 +864,8 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) res->printf("

File not found: %s

\n", filename.c_str()); res->printf("

\n"); res->printf("

You have gotten this error because the filesystem for the web server has not been loaded.

\n"); - res->printf("

Please review the 'Common Problems' section of the web interface documentation.

\n"); + res->printf("

Please review the 'Common Problems' section of the web interface documentation.

\n"); return; } @@ -881,7 +886,6 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) } } - // Read the file from SPIFFS and write it to the HTTP response body size_t length = 0; do { @@ -892,6 +896,16 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) } while (length > 0); } +void handleRestart(HTTPRequest *req, HTTPResponse *res) +{ + res->setHeader("Content-Type", "text/html"); + + DEBUG_MSG("***** Restarted on HTTP(s) Request *****\n"); + res->println("Restarting"); + + ESP.restart(); +} + void handleFavicon(HTTPRequest *req, HTTPResponse *res) { // Set Content-Type @@ -900,7 +914,6 @@ void handleFavicon(HTTPRequest *req, HTTPResponse *res) res->write(FAVICON_DATA, FAVICON_LENGTH); } - void replaceAll(std::string &str, const std::string &from, const std::string &to) { if (from.empty()) @@ -911,4 +924,3 @@ void replaceAll(std::string &str, const std::string &from, const std::string &to start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' } } - From 26c9585c9d35dfdd504d2b7645eb219062de5911 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 12:38:17 -0800 Subject: [PATCH 02/12] #557 - Switch from GET to POST --- src/meshwifi/meshhttp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 32491d00d..2c8f0e8bf 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -235,7 +235,7 @@ void initWebServer() ResourceNode *nodeStaticBrowse = new ResourceNode("/static", "GET", &handleStaticBrowse); ResourceNode *nodeStaticPOST = new ResourceNode("/static", "POST", &handleStaticPost); ResourceNode *nodeStatic = new ResourceNode("/static/*", "GET", &handleStatic); - ResourceNode *nodeRestart = new ResourceNode("/restart", "GET", &handleRestart); + ResourceNode *nodeRestart = new ResourceNode("/restart", "POST", &handleRestart); ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); From 02ce12607c0cebc9a36a2205e742f4769fb6b6be Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 13:54:14 -0800 Subject: [PATCH 03/12] #560 - Force SoftAP if the user button is held down during startup. #560 - Force SoftAP if the user button is held down during startup. --- src/main.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 59773e2bd..951d27a7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -241,7 +241,7 @@ class ButtonThread : public OSThread } #endif } else { - //DEBUG_MSG("Long press %u\n", (millis() - longPressTime)); + // DEBUG_MSG("Long press %u\n", (millis() - longPressTime)); } } @@ -298,6 +298,21 @@ void setup() digitalWrite(RESET_OLED, 1); #endif + // If BUTTON_PIN is held down during the startup process, + // force the device to go into a SoftAP mode. + bool forceSoftAP = 0; +#ifdef BUTTON_PIN +#ifndef NO_ESP32 + pinMode(BUTTON_PIN, INPUT); + + // BUTTON_PIN is pulled high by a 12k resistor. + if (!digitalRead(BUTTON_PIN)) { + forceSoftAP = 1; + } + +#endif +#endif + OSThread::setup(); ledPeriodic = new Periodic("Blink", ledBlinker); @@ -466,6 +481,13 @@ void setup() } #endif + if (forceSoftAP == false) { + strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); + strcpy(radioConfig.preferences.wifi_password, "12345678"); + radioConfig.preferences.wifi_ap_mode = true; + DEBUG_MSG("Forcing SoftAP\n"); + } + // Initialize Wifi initWifi(); From c0d94ae4abb2494fe711ae1ed5c912ba34e6274a Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 14:04:52 -0800 Subject: [PATCH 04/12] Update main.cpp --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 951d27a7e..f23d79b6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -481,7 +481,7 @@ void setup() } #endif - if (forceSoftAP == false) { + if (forceSoftAP) { strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); strcpy(radioConfig.preferences.wifi_password, "12345678"); radioConfig.preferences.wifi_ap_mode = true; From d82aaaa806f7e8814db7928f97007792553ac39f Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 18:33:52 -0800 Subject: [PATCH 05/12] #560 - Partial work for Charles. --- src/main.cpp | 10 ++------ src/meshwifi/meshwifi.cpp | 44 +++++++++++++++++++++++++++++++----- src/meshwifi/meshwifi.h | 2 +- src/nimble/BluetoothUtil.cpp | 2 +- src/nrf52/wifi-stubs.cpp | 2 +- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f23d79b6a..3f028613b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -308,6 +308,7 @@ void setup() // BUTTON_PIN is pulled high by a 12k resistor. if (!digitalRead(BUTTON_PIN)) { forceSoftAP = 1; + DEBUG_MSG("-------------------- Setting forceSoftAP = 1\n"); } #endif @@ -481,15 +482,8 @@ void setup() } #endif - if (forceSoftAP) { - strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); - strcpy(radioConfig.preferences.wifi_password, "12345678"); - radioConfig.preferences.wifi_ap_mode = true; - DEBUG_MSG("Forcing SoftAP\n"); - } - // Initialize Wifi - initWifi(); + initWifi(forceSoftAP); if (!rIf) recordCriticalError(ErrNoRadio); diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 71c4d2277..4b267399d 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -21,8 +21,16 @@ uint8_t wifiDisconnectReason = 0; // Stores our hostname char ourHost[16]; +bool forcedSoftAP = 0; + + bool isWifiAvailable() { + // If wifi status is connected, return true regardless of the radio configuration. + if (forcedSoftAP) { + return 1; + } + const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; @@ -58,20 +66,44 @@ void deinitWifi() } // Startup WiFi -void initWifi() +void initWifi(bool forceSoftAP) { - if (isWifiAvailable() == 0) { - return; + + if (forceSoftAP) { + // do nothing + DEBUG_MSG("----- Forcing SoftAP\n"); + } else { + if (isWifiAvailable() == 0) { + return; + } } + forcedSoftAP = forceSoftAP; + createSSLCert(); - if (radioConfig.has_preferences) { + if (radioConfig.has_preferences || forceSoftAP) { const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; - if (*wifiName && *wifiPsw) { - if (radioConfig.preferences.wifi_ap_mode) { + if ((*wifiName && *wifiPsw) || forceSoftAP) { + if (forceSoftAP) { + + DEBUG_MSG("----- Forcing SoftAP\n"); + + const char *softAPssid = ""; + const char *softAPpasswd = ""; + + IPAddress apIP(192, 168, 42, 1); + WiFi.onEvent(WiFiEvent); + + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); + DEBUG_MSG("STARTING WIFI AP: ssid=%s, ok=%d\n", softAPssid, WiFi.softAP(softAPssid, softAPpasswd)); + DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.softAPIP().toString().c_str()); + + dnsServer.start(53, "*", apIP); + + } else if (radioConfig.preferences.wifi_ap_mode) { IPAddress apIP(192, 168, 42, 1); WiFi.onEvent(WiFiEvent); diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index ac03e29f5..d56a44943 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -9,7 +9,7 @@ #include #endif -void initWifi(); +void initWifi(bool forceSoftAP); void deinitWifi(); bool isWifiAvailable(); diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 8a0dd779b..daba69372 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -545,7 +545,7 @@ void setBluetoothEnable(bool on) if (firstTime) { firstTime = 0; } else { - initWifi(); + initWifi(0); } } else { diff --git a/src/nrf52/wifi-stubs.cpp b/src/nrf52/wifi-stubs.cpp index 30321e5e4..1c1009862 100644 --- a/src/nrf52/wifi-stubs.cpp +++ b/src/nrf52/wifi-stubs.cpp @@ -1,7 +1,7 @@ #include "meshwifi/meshhttp.h" #include "meshwifi/meshwifi.h" -void initWifi() {} +void initWifi(bool forceSoftAP) {} void deinitWifi() {} From e10b82c118ad53ce0fe66000538a12221d267046 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 19:09:58 -0800 Subject: [PATCH 06/12] #560 Partial changes --- src/graphics/Screen.cpp | 25 ++++++++++++++++--------- src/meshwifi/meshwifi.cpp | 10 +++++++--- src/meshwifi/meshwifi.h | 2 ++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 320c0f1e2..71cad3908 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -984,7 +984,7 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i // The coordinates define the left starting point of the text display->setTextAlignment(TEXT_ALIGN_LEFT); - if (radioConfig.preferences.wifi_ap_mode) { + if (radioConfig.preferences.wifi_ap_mode || isSoftAPForced()) { display->drawString(x, y, String("WiFi: Software AP")); } else if (WiFi.status() != WL_CONNECTED) { display->drawString(x, y, String("WiFi: Not Connected")); @@ -1007,9 +1007,8 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i - WL_NO_SHIELD: assigned when no WiFi shield is present; */ - - if (WiFi.status() == WL_CONNECTED) { - if (radioConfig.preferences.wifi_ap_mode) { + if (WiFi.status() == WL_CONNECTED || isSoftAPForced()) { + if (radioConfig.preferences.wifi_ap_mode || isSoftAPForced()) { display->drawString(x, y + FONT_HEIGHT_SMALL * 1, "IP: " + String(WiFi.softAPIP().toString().c_str())); } else { display->drawString(x, y + FONT_HEIGHT_SMALL * 1, "IP: " + String(WiFi.localIP().toString().c_str())); @@ -1088,10 +1087,19 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i } } - if ((millis() / 10000) % 2) { - display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "SSID: " + String(wifiName)); + if (isSoftAPForced()) { + if ((millis() / 10000) % 2) { + display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "SSID: meshtasticAdmin"); + } else { + display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "PWD: 12345678"); + } + } else { - display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "PWD: " + String(wifiPsw)); + if ((millis() / 10000) % 2) { + display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "SSID: " + String(wifiName)); + } else { + display->drawString(x, y + FONT_HEIGHT_SMALL * 2, "PWD: " + String(wifiPsw)); + } } display->drawString(x, y + FONT_HEIGHT_SMALL * 3, "http://meshtastic.local"); @@ -1185,8 +1193,7 @@ int Screen::handleStatusUpdate(const meshtastic::Status *arg) // DEBUG_MSG("Screen got status update %d\n", arg->getStatusType()); switch (arg->getStatusType()) { case STATUS_TYPE_NODE: - if (showingNormalScreen && - nodeStatus->getLastNumTotal() != nodeStatus->getNumTotal()) { + if (showingNormalScreen && nodeStatus->getLastNumTotal() != nodeStatus->getNumTotal()) { setFrames(); // Regen the list of screens } nodeDB.updateGUI = false; diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 4b267399d..008d82118 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -24,10 +24,14 @@ char ourHost[16]; bool forcedSoftAP = 0; +bool isSoftAPForced() { + return forcedSoftAP; +} + bool isWifiAvailable() { // If wifi status is connected, return true regardless of the radio configuration. - if (forcedSoftAP) { + if (isSoftAPForced()) { return 1; } @@ -91,8 +95,8 @@ void initWifi(bool forceSoftAP) DEBUG_MSG("----- Forcing SoftAP\n"); - const char *softAPssid = ""; - const char *softAPpasswd = ""; + const char *softAPssid = "meshtasticAdmin"; + const char *softAPpasswd = "12345678"; IPAddress apIP(192, 168, 42, 1); WiFi.onEvent(WiFiEvent); diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index d56a44943..9e2f8ad78 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -18,5 +18,7 @@ void handleDNSResponse(); void reconnectWiFi(); +bool isSoftAPForced(); + uint8_t getWifiDisconnectReason(); From 9f9573d2eb6f5a9152072728aaee055940188b2c Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 19:18:51 -0800 Subject: [PATCH 07/12] #560 - Add note that we're in admin mode on network screen --- src/graphics/Screen.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 71cad3908..bc43ae853 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -984,7 +984,9 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i // The coordinates define the left starting point of the text display->setTextAlignment(TEXT_ALIGN_LEFT); - if (radioConfig.preferences.wifi_ap_mode || isSoftAPForced()) { + if (isSoftAPForced()) { + display->drawString(x, y, String("WiFi: Software AP (Admin)")); + } else if (radioConfig.preferences.wifi_ap_mode) { display->drawString(x, y, String("WiFi: Software AP")); } else if (WiFi.status() != WL_CONNECTED) { display->drawString(x, y, String("WiFi: Not Connected")); From 17297db2b136c5ccb10e150ecae817c9f91e7cf1 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 20:50:41 -0800 Subject: [PATCH 08/12] #561 - Add a scan for SSID --- src/meshwifi/meshhttp.cpp | 45 +++++++++++++++++++++++++++++++++++++++ src/meshwifi/meshwifi.cpp | 6 ++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 2c8f0e8bf..6243d8420 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -59,6 +59,7 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res); void handleRestart(HTTPRequest *req, HTTPResponse *res); void handle404(HTTPRequest *req, HTTPResponse *res); void handleFormUpload(HTTPRequest *req, HTTPResponse *res); +void handleScanNetworks(HTTPRequest *req, HTTPResponse *res); void middlewareSpeedUp240(HTTPRequest *req, HTTPResponse *res, std::function next); void middlewareSpeedUp160(HTTPRequest *req, HTTPResponse *res, std::function next); @@ -238,6 +239,7 @@ void initWebServer() ResourceNode *nodeRestart = new ResourceNode("/restart", "POST", &handleRestart); ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); + ResourceNode *nodeJsonScanNetworks = new ResourceNode("/json/scanNetworks", "GET", &handleScanNetworks); // Secure nodes secureServer->registerNode(nodeAPIv1ToRadioOptions); @@ -252,6 +254,7 @@ void initWebServer() secureServer->registerNode(nodeRestart); secureServer->setDefaultNode(node404); secureServer->setDefaultNode(nodeFormUpload); + secureServer->setDefaultNode(nodeJsonScanNetworks); secureServer->addMiddleware(&middlewareSpeedUp240); @@ -268,6 +271,7 @@ void initWebServer() insecureServer->registerNode(nodeRestart); insecureServer->setDefaultNode(node404); insecureServer->setDefaultNode(nodeFormUpload); + insecureServer->setDefaultNode(nodeJsonScanNetworks); insecureServer->addMiddleware(&middlewareSpeedUp160); @@ -906,6 +910,47 @@ void handleRestart(HTTPRequest *req, HTTPResponse *res) ESP.restart(); } +void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) +{ + // res->setHeader("Content-Type", "application/json"); + res->setHeader("Content-Type", "text/html"); + + int n = WiFi.scanNetworks(); + res->println("{"); + res->println("\"data\": {"); + if (n == 0) { + // No networks found. + res->println("\"networks\": []"); + + } else { + res->println("\"networks\": ["); + + for (int i = 0; i < n; ++i) { + char ssidArray[50]; + String(WiFi.SSID(i)).toCharArray(ssidArray, WiFi.SSID(i).length()); + + if (WiFi.encryptionType(i) != WIFI_AUTH_OPEN) { + //res->println("{\"ssid\": \"%s\",\"rssi\": -75}, ", String(WiFi.SSID(i).c_str() ); + + res->printf("{\"ssid\": \"%s\",\"rssi\": %d}", ssidArray, WiFi.RSSI(i) ) ; + //WiFi.RSSI(i) + if (i != n-1) { + res->printf(","); + } + } + // Yield some cpu cycles to IP stack. + // This is important in case the list is large and it takes us tome to return + // to the main loop. + yield(); + } + res->println("]"); + } + res->println("},"); + res->println("\"status\": \"ok\""); + res->println("}"); + +} + void handleFavicon(HTTPRequest *req, HTTPResponse *res) { // Set Content-Type diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 008d82118..0f28c1fad 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -40,6 +40,8 @@ bool isWifiAvailable() // strcpy(radioConfig.preferences.wifi_ssid, ""); // strcpy(radioConfig.preferences.wifi_password, ""); + strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); + strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); if (*wifiName && *wifiPsw) { return 1; @@ -75,7 +77,7 @@ void initWifi(bool forceSoftAP) if (forceSoftAP) { // do nothing - DEBUG_MSG("----- Forcing SoftAP\n"); + // DEBUG_MSG("----- Forcing SoftAP\n"); } else { if (isWifiAvailable() == 0) { return; @@ -93,7 +95,7 @@ void initWifi(bool forceSoftAP) if ((*wifiName && *wifiPsw) || forceSoftAP) { if (forceSoftAP) { - DEBUG_MSG("----- Forcing SoftAP\n"); + DEBUG_MSG("Forcing SoftAP\n"); const char *softAPssid = "meshtasticAdmin"; const char *softAPpasswd = "12345678"; From 5249608dcef988e75a7e68e174891933674eb7bf Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 21:35:21 -0800 Subject: [PATCH 09/12] #561 Add escapes to strings --- src/meshwifi/meshhttp.cpp | 8 ++++++-- src/meshwifi/meshwifi.cpp | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 6243d8420..0c8540344 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -927,7 +927,11 @@ void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) for (int i = 0; i < n; ++i) { char ssidArray[50]; - String(WiFi.SSID(i)).toCharArray(ssidArray, WiFi.SSID(i).length()); + String ssidString = String(WiFi.SSID(i)); + //String ssidString = String(WiFi.SSID(i)).toCharArray(ssidArray, WiFi.SSID(i).length()); + ssidString.replace("\"", "\\\""); + ssidString.toCharArray(ssidArray, 50); + if (WiFi.encryptionType(i) != WIFI_AUTH_OPEN) { //res->println("{\"ssid\": \"%s\",\"rssi\": -75}, ", String(WiFi.SSID(i).c_str() ); @@ -939,7 +943,7 @@ void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) } } // Yield some cpu cycles to IP stack. - // This is important in case the list is large and it takes us tome to return + // This is important in case the list is large and it takes us time to return // to the main loop. yield(); } diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 0f28c1fad..3d747b711 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -40,8 +40,8 @@ bool isWifiAvailable() // strcpy(radioConfig.preferences.wifi_ssid, ""); // strcpy(radioConfig.preferences.wifi_password, ""); - strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); - strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); + // strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); + // strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); if (*wifiName && *wifiPsw) { return 1; From bb9abf2dcaa54c4e3c53ecac5a97f1e2802f67d2 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 21:42:32 -0800 Subject: [PATCH 10/12] #560 Forgot to switch back to application/json --- src/meshwifi/meshhttp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 0c8540344..d0940d06b 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -912,8 +912,8 @@ void handleRestart(HTTPRequest *req, HTTPResponse *res) void handleScanNetworks(HTTPRequest *req, HTTPResponse *res) { - // res->setHeader("Content-Type", "application/json"); - res->setHeader("Content-Type", "text/html"); + res->setHeader("Content-Type", "application/json"); + // res->setHeader("Content-Type", "text/html"); int n = WiFi.scanNetworks(); res->println("{"); From 46781357dfd400d2853c25f18254607f05dc64d1 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 21:43:01 -0800 Subject: [PATCH 11/12] remove hard coded network --- src/meshwifi/meshwifi.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 3d747b711..da3af6a54 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -23,8 +23,8 @@ char ourHost[16]; bool forcedSoftAP = 0; - -bool isSoftAPForced() { +bool isSoftAPForced() +{ return forcedSoftAP; } @@ -40,8 +40,6 @@ bool isWifiAvailable() // strcpy(radioConfig.preferences.wifi_ssid, ""); // strcpy(radioConfig.preferences.wifi_password, ""); - // strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); - // strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); if (*wifiName && *wifiPsw) { return 1; From 86952c5456e2ae5f111b7a7f171ad41508232ebe Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 22:37:07 -0800 Subject: [PATCH 12/12] Fixed #541 - Work around to bug in espressif softap event handler --- src/graphics/Screen.cpp | 5 ++++- src/meshwifi/meshhttp.cpp | 7 ++++--- src/meshwifi/meshwifi.cpp | 19 ++++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index bc43ae853..c112a2957 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -1009,12 +1009,15 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i - WL_NO_SHIELD: assigned when no WiFi shield is present; */ - if (WiFi.status() == WL_CONNECTED || isSoftAPForced()) { + if (WiFi.status() == WL_CONNECTED || isSoftAPForced() || radioConfig.preferences.wifi_ap_mode) { if (radioConfig.preferences.wifi_ap_mode || isSoftAPForced()) { display->drawString(x, y + FONT_HEIGHT_SMALL * 1, "IP: " + String(WiFi.softAPIP().toString().c_str())); } else { display->drawString(x, y + FONT_HEIGHT_SMALL * 1, "IP: " + String(WiFi.localIP().toString().c_str())); } + display->drawString(x + SCREEN_WIDTH - display->getStringWidth("(" + String(WiFi.softAPgetStationNum()) + "/4)"), + y + FONT_HEIGHT_SMALL * 1, "(" + String(WiFi.softAPgetStationNum()) + "/4)"); + } else if (WiFi.status() == WL_NO_SSID_AVAIL) { display->drawString(x, y + FONT_HEIGHT_SMALL * 1, "SSID Not Found"); } else if (WiFi.status() == WL_CONNECTION_LOST) { diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index d0940d06b..c09e6ac46 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -178,9 +178,10 @@ void createSSLCert() NULL); /* Task handle. */ DEBUG_MSG("Waiting for SSL Cert to be generated.\n"); - if (isCertReady) { - DEBUG_MSG(".\n"); - delayMicroseconds(1000); + while (!isCertReady) { + DEBUG_MSG("."); + delay(1000); + yield(); } DEBUG_MSG("SSL Cert Ready!\n"); } diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index da3af6a54..16ae61850 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -23,6 +23,8 @@ char ourHost[16]; bool forcedSoftAP = 0; +bool APStartupComplete = 0; + bool isSoftAPForced() { return forcedSoftAP; @@ -41,6 +43,10 @@ bool isWifiAvailable() // strcpy(radioConfig.preferences.wifi_ssid, ""); // strcpy(radioConfig.preferences.wifi_password, ""); + // strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); + // strcpy(radioConfig.preferences.wifi_password, "12345678"); + // radioConfig.preferences.wifi_ap_mode = true; + if (*wifiName && *wifiPsw) { return 1; } else { @@ -239,9 +245,16 @@ static void WiFiEvent(WiFiEvent_t event) DEBUG_MSG("WiFi access point started\n"); Serial.println(WiFi.softAPIP()); - // Start web server - initWebServer(); - initApiServer(); + if (!APStartupComplete) { + // Start web server + DEBUG_MSG("... Starting network services\n"); + initWebServer(); + initApiServer(); + + APStartupComplete = true; + } else { + DEBUG_MSG("... Not starting network services\n"); + } break; case SYSTEM_EVENT_AP_STOP: