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";