From dbbfe25a444c182b8adfc1576736dc1b4b9ac259 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 22 Oct 2022 02:59:01 +0200 Subject: [PATCH] added wifi scan to setup fixed issues on index.html --- tools/esp8266/ahoywifi.cpp | 20 ++++++++++++++++++++ tools/esp8266/ahoywifi.h | 1 + tools/esp8266/app.cpp | 6 ++++++ tools/esp8266/app.h | 2 +- tools/esp8266/defines.h | 2 +- tools/esp8266/html/api.js | 8 ++++++++ tools/esp8266/html/index.html | 14 +++++++++----- tools/esp8266/html/setup.html | 27 +++++++++++++++++++++++++++ tools/esp8266/webApi.cpp | 27 +++++++++++++++++---------- tools/esp8266/webApi.h | 1 + 10 files changed, 91 insertions(+), 17 deletions(-) diff --git a/tools/esp8266/ahoywifi.cpp b/tools/esp8266/ahoywifi.cpp index 436fd660..a3ad7236 100644 --- a/tools/esp8266/ahoywifi.cpp +++ b/tools/esp8266/ahoywifi.cpp @@ -209,6 +209,26 @@ time_t ahoywifi::getNtpTime(void) { } +//----------------------------------------------------------------------------- +void ahoywifi::getAvailNetworks(JsonObject obj) { + JsonArray nets = obj.createNestedArray("networks"); + + int n = WiFi.scanComplete(); + if(n == -2) { + WiFi.scanNetworks(true); + } else if(n) { + for (int i = 0; i < n; ++i) { + nets[i]["ssid"] = WiFi.SSID(i); + nets[i]["rssi"] = WiFi.RSSI(i); + nets[i]["hidden"] = WiFi.isHidden(i) ? true : false; + } + WiFi.scanDelete(); + if(WiFi.scanComplete() == -2) + WiFi.scanNetworks(true); + } +} + + //----------------------------------------------------------------------------- void ahoywifi::sendNTPpacket(IPAddress& address) { //DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket")); diff --git a/tools/esp8266/ahoywifi.h b/tools/esp8266/ahoywifi.h index ba02c577..9a3239fd 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 getAvailNetworks(JsonObject obj); private: void sendNTPpacket(IPAddress& address); diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 7dae17ec..aa7b9bd0 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -604,6 +604,12 @@ bool app::getWifiApActive(void) { } +//----------------------------------------------------------------------------- +void app::getAvailNetworks(JsonObject obj) { + mWifi->getAvailNetworks(obj); +} + + //----------------------------------------------------------------------------- void app::sendMqttDiscoveryConfig(void) { DPRINTLN(DBG_VERBOSE, F("app::sendMqttDiscoveryConfig")); diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index df43bcd4..18b00263 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 getAvailNetworks(JsonObject obj); uint8_t getIrqPin(void) { return mConfig.pinIrq; @@ -171,7 +172,6 @@ class app { inline bool getSettingsValid(void) { return mSettingsValid; } inline bool getRebootRequestState(void) { return mShowRebootRequest; } - HmSystemType *mSys; bool mShouldReboot; bool mFlagSendDiscoveryConfig; diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 5c7ccff8..fa6ee3f4 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 21 +#define VERSION_PATCH 22 //------------------------------------- diff --git a/tools/esp8266/html/api.js b/tools/esp8266/html/api.js index 9742a967..d3288c8b 100644 --- a/tools/esp8266/html/api.js +++ b/tools/esp8266/html/api.js @@ -68,6 +68,14 @@ function sel(name, opt, selId) { return e; } +function opt(val, html) { + o = document.createElement('option'); + o.value = val; + o.innerHTML = html; + e.appendChild(o); + return o; +} + function div(cl) { e = document.createElement('div'); e.classList.add(...cl); diff --git a/tools/esp8266/html/index.html b/tools/esp8266/html/index.html index d8537e11..cc7f5f08 100644 --- a/tools/esp8266/html/index.html +++ b/tools/esp8266/html/index.html @@ -104,22 +104,26 @@ + ("0"+hrs).substr(-2) + ":" + ("0"+min).substr(-2) + ":" + ("0"+sec).substr(-2); - var dSpan = document.getElementById("date") + var dSpan = document.getElementById("date"); if(0 != obj["ts_now"]) dSpan.innerHTML = date.toLocaleString('de-DE'); else { + dSpan.innerHTML = ""; var e = inp("set", "sync from browser", 0, ["btn"], "set", "button"); dSpan.appendChild(span("NTP timeserver unreachable. ")); dSpan.appendChild(e); dSpan.appendChild(span("", ["span"], "apiResult")); e.addEventListener("click", setTime); } - document.getElementById("sunrise").innerHTML = sunrise.toLocaleString('de-DE'); - document.getElementById("sunset").innerHTML = sunset.toLocaleString('de-DE'); if(!obj["ts_sun_upd"]) { - var elem = document.getElementById("sun"); - elem.parentNode.removeChild(elem); + var e = document.getElementById("sun"); + if(null != e) + e.parentNode.removeChild(e); + } + else { + document.getElementById("sunrise").innerHTML = sunrise.toLocaleString('de-DE'); + document.getElementById("sunset").innerHTML = sunset.toLocaleString('de-DE'); } } diff --git a/tools/esp8266/html/setup.html b/tools/esp8266/html/setup.html index 51d1917a..b73d4ebc 100644 --- a/tools/esp8266/html/setup.html +++ b/tools/esp8266/html/setup.html @@ -36,6 +36,10 @@
WiFi

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

+ + @@ -377,12 +381,35 @@ parseRadio(root["radio"]); parseSerial(root["serial"]); } + getAjax('/api/setup/networks', listNetworks); + window.setInterval("getAjax('/api/setup/networks', listNetworks)", 7000); + } + + function listNetworks(root) { + 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); + } + + for(i = 0; i < root["networks"].length; i++) { + s.appendChild(opt(root["networks"][i]["ssid"], root["networks"][i]["ssid"] + " (" + root["networks"][i]["rssi"] + " dBm)")); + } + } + } + + function selNet() { + var s = document.getElementById("networks"); + var e = document.getElementsByName("ssid")[0]; + e.value = s.value; } hiddenInput = document.getElementById("disclaimer") hiddenInput.value = sessionStorage.getItem("gDisclaimer"); getAjax("/api/setup", parse); + diff --git a/tools/esp8266/webApi.cpp b/tools/esp8266/webApi.cpp index 7a2d265d..71d73dc3 100644 --- a/tools/esp8266/webApi.cpp +++ b/tools/esp8266/webApi.cpp @@ -45,16 +45,17 @@ void webApi::onApi(AsyncWebServerRequest *request) { Inverter<> *iv = mApp->mSys->getInverterByPos(0, false); String path = request->url().substring(5); - if(path == "system") getSystem(root); - else if(path == "statistics") getStatistics(root); - else if(path == "inverter/list") getInverterList(root); - else if(path == "index") getIndex(root); - else if(path == "setup") getSetup(root); - else if(path == "live") getLive(root); - else if(path == "record/info") getRecord(root, iv->getRecordStruct(InverterDevInform_All)); - else if(path == "record/alarm") getRecord(root, iv->getRecordStruct(AlarmData)); - else if(path == "record/config") getRecord(root, iv->getRecordStruct(SystemConfigPara)); - else if(path == "record/live") getRecord(root, iv->getRecordStruct(RealTimeRunData_Debug)); + if(path == "system") getSystem(root); + else if(path == "statistics") getStatistics(root); + else if(path == "inverter/list") getInverterList(root); + else if(path == "index") getIndex(root); + else if(path == "setup") getSetup(root); + else if(path == "setup/networks") getNetworks(root); + else if(path == "live") getLive(root); + else if(path == "record/info") getRecord(root, iv->getRecordStruct(InverterDevInform_All)); + else if(path == "record/alarm") getRecord(root, iv->getRecordStruct(AlarmData)); + else if(path == "record/config") getRecord(root, iv->getRecordStruct(SystemConfigPara)); + else if(path == "record/live") getRecord(root, iv->getRecordStruct(RealTimeRunData_Debug)); else getNotFound(root, F("http://") + request->host() + F("/api/")); @@ -291,6 +292,12 @@ void webApi::getSetup(JsonObject obj) { } +//----------------------------------------------------------------------------- +void webApi::getNetworks(JsonObject obj) { + mApp->getAvailNetworks(obj); +} + + //----------------------------------------------------------------------------- void webApi::getLive(JsonObject obj) { getSystem(obj.createNestedObject(F("system"))); diff --git a/tools/esp8266/webApi.h b/tools/esp8266/webApi.h index 783c5782..9be0cf4c 100644 --- a/tools/esp8266/webApi.h +++ b/tools/esp8266/webApi.h @@ -44,6 +44,7 @@ class webApi { void getIndex(JsonObject obj); void getSetup(JsonObject obj); + void getNetworks(JsonObject obj); void getLive(JsonObject obj); void getRecord(JsonObject obj, record_t<> *rec);