diff --git a/tools/esp8266/ahoywifi.cpp b/tools/esp8266/ahoywifi.cpp index 9823ea23..54ada30a 100644 --- a/tools/esp8266/ahoywifi.cpp +++ b/tools/esp8266/ahoywifi.cpp @@ -209,24 +209,25 @@ time_t ahoywifi::getNtpTime(void) { } +//----------------------------------------------------------------------------- +void ahoywifi::scanAvailNetworks(void) { + int n = WiFi.scanComplete(); + if(n == -2) + WiFi.scanNetworks(true); +} + + //----------------------------------------------------------------------------- void ahoywifi::getAvailNetworks(JsonObject obj) { JsonArray nets = obj.createNestedArray("networks"); int n = WiFi.scanComplete(); - if(n == -2) { - WiFi.scanNetworks(true); - } else if(n) { + if(n > 0) { for (int i = 0; i < n; ++i) { nets[i]["ssid"] = WiFi.SSID(i); nets[i]["rssi"] = WiFi.RSSI(i); - // TODO: does github workflow use another version of this library? - // ahoywifi.cpp:223:38: error: 'class WiFiClass' has no member named 'isHidden' - //nets[i]["hidden"] = WiFi.isHidden(i) ? true : false; } WiFi.scanDelete(); - if(WiFi.scanComplete() == -2) - WiFi.scanNetworks(true); } } diff --git a/tools/esp8266/ahoywifi.h b/tools/esp8266/ahoywifi.h index 9a3239fd..cf3c779d 100644 --- a/tools/esp8266/ahoywifi.h +++ b/tools/esp8266/ahoywifi.h @@ -30,6 +30,7 @@ class ahoywifi { bool setupStation(uint32_t timeout); bool getApActive(void); time_t getNtpTime(void); + void scanAvailNetworks(void); void getAvailNetworks(JsonObject obj); private: diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index befe017c..6f403fec 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -609,6 +609,12 @@ bool app::getWifiApActive(void) { } +//----------------------------------------------------------------------------- +void app::scanAvailNetworks(void) { + mWifi->scanAvailNetworks(); +} + + //----------------------------------------------------------------------------- void app::getAvailNetworks(JsonObject obj) { mWifi->getAvailNetworks(obj); diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index e2e1adcc..fdb8ba93 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -68,6 +68,7 @@ class app { void saveValues(void); void resetPayload(Inverter<>* iv); bool getWifiApActive(void); + void scanAvailNetworks(void); void getAvailNetworks(JsonObject obj); uint8_t getIrqPin(void) { diff --git a/tools/esp8266/html/api.js b/tools/esp8266/html/api.js index a959ed74..eb2791ee 100644 --- a/tools/esp8266/html/api.js +++ b/tools/esp8266/html/api.js @@ -77,6 +77,13 @@ function sel(name, opt, selId) { return e; } +function selDelAllOpt(sel) { + var i, l = sel.options.length - 1; + for(i = l; i >= 0; i--) { + sel.remove(i); + } +} + function opt(val, html) { o = document.createElement('option'); o.value = val; diff --git a/tools/esp8266/html/setup.html b/tools/esp8266/html/setup.html index e88af4e1..c2985886 100644 --- a/tools/esp8266/html/setup.html +++ b/tools/esp8266/html/setup.html @@ -44,9 +44,11 @@
WiFi

Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.

+ +
@@ -162,6 +164,15 @@ ivHtml(JSON.parse('{"name":"","serial":"","channels":4,"ch_max_power":[0,0,0,0],"ch_name":["","","",""]}'), highestId + 1); }); + function apiCbWifi(obj) { + var e = document.getElementById("networks"); + selDelAllOpt(e); + if(obj["success"]) + e.appendChild(opt("-1", "scanning ...")) + else + e.appendChild(opt("-1", "Error: " + obj["error"])); + } + function apiCbNtp(obj) { var e = document.getElementById("apiResultNtp"); if(obj["success"]) @@ -186,6 +197,13 @@ getAjax("/api/setup", apiCbNtp, "POST", JSON.stringify(obj)); } + function scan() { + var obj = new Object(); + obj.cmd = "scan_wifi"; + getAjax("/api/setup", apiCbWifi, "POST", JSON.stringify(obj)); + setTimeout(function() {getAjax('/api/setup/networks', listNetworks)}, 7000); + } + function syncTime() { var obj = new Object(); obj.cmd = "sync_ntp"; @@ -402,27 +420,26 @@ parseRadio(root["radio"]); parseSerial(root["serial"]); } - window.setInterval("getAjax('/api/setup/networks', listNetworks)", 7000); } function listNetworks(root) { + var s = document.getElementById("networks"); + selDelAllOpt(s); if(root["networks"].length > 0) { - var s = document.getElementById("networks"); - var i, l = s.options.length - 1; - for(i = l; i >= 0; i--) { - s.remove(i); - } - + s.appendChild(opt("-1", "please select network")); for(i = 0; i < root["networks"].length; i++) { s.appendChild(opt(root["networks"][i]["ssid"], root["networks"][i]["ssid"] + " (" + root["networks"][i]["rssi"] + " dBm)")); } } + else + s.appendChild(opt("-1", "no network found")); } function selNet() { var s = document.getElementById("networks"); var e = document.getElementsByName("ssid")[0]; - e.value = s.value; + if(-1 != s.value) + e.value = s.value; } hiddenInput = document.getElementById("disclaimer") diff --git a/tools/esp8266/webApi.cpp b/tools/esp8266/webApi.cpp index 124a2f2e..0c69102a 100644 --- a/tools/esp8266/webApi.cpp +++ b/tools/esp8266/webApi.cpp @@ -456,7 +456,9 @@ bool webApi::setCtrl(DynamicJsonDocument jsonIn, JsonObject jsonOut) { //----------------------------------------------------------------------------- bool webApi::setSetup(DynamicJsonDocument jsonIn, JsonObject jsonOut) { - if(F("set_time") == jsonIn[F("cmd")]) + if(F("scan_wifi")) + mApp->scanAvailNetworks(); + else if(F("set_time") == jsonIn[F("cmd")]) mApp->setTimestamp(jsonIn[F("ts")]); else if(F("sync_ntp") == jsonIn[F("cmd")]) mApp->setTimestamp(0); // 0: update ntp flag