diff --git a/src/app.cpp b/src/app.cpp index 7f2fefd7..001b18f6 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -84,7 +84,7 @@ void app::setup() { mPubSerial.setup(mConfig, &mSys, &mTimestamp); - mImprov.setup(mConfig->sys.deviceName, mVersion); + mImprov.setup(this, mConfig->sys.deviceName, mVersion); regularTickers(); diff --git a/src/app.h b/src/app.h index 010758c4..9a73ddcd 100644 --- a/src/app.h +++ b/src/app.h @@ -101,8 +101,8 @@ class app : public IApp, public ah::Scheduler { mWifi.scanAvailNetworks(); } - void getAvailNetworks(JsonObject obj) { - mWifi.getAvailNetworks(obj); + bool getAvailNetworks(JsonObject obj) { + return mWifi.getAvailNetworks(obj); } void setOnUpdate() { diff --git a/src/appInterface.h b/src/appInterface.h index 44491d91..a3240189 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -25,7 +25,7 @@ class IApp { virtual const char *getVersion() = 0; virtual statistics_t *getStatistics() = 0; virtual void scanAvailNetworks() = 0; - virtual void getAvailNetworks(JsonObject obj) = 0; + virtual bool getAvailNetworks(JsonObject obj) = 0; virtual uint32_t getUptime() = 0; virtual uint32_t getTimestamp() = 0; diff --git a/src/utils/dbg.cpp b/src/utils/dbg.cpp index 9f7c9fd3..e7ae4581 100644 --- a/src/utils/dbg.cpp +++ b/src/utils/dbg.cpp @@ -1,3 +1,4 @@ #include "dbg.h" DBG_CB mCb = NULL; +bool mDebugEn = true; diff --git a/src/utils/dbg.h b/src/utils/dbg.h index 53c9f79e..8ce23db9 100644 --- a/src/utils/dbg.h +++ b/src/utils/dbg.h @@ -39,6 +39,7 @@ #ifdef ARDUINO #define DBG_CB std::function extern DBG_CB mCb; + extern bool mDebugEn; inline void registerDebugCb(DBG_CB cb) { mCb = cb; @@ -48,12 +49,16 @@ #define DSERIAL Serial #endif + inline void setDebugEn(bool en) { + mDebugEn = en; + } + //template - inline void DBGPRINT(String str, bool ser = true) { if(ser) DSERIAL.print(str); if(NULL != mCb) mCb(str); } + inline void DBGPRINT(String str, bool ser = true) { if(ser && mDebugEn) DSERIAL.print(str); if(NULL != mCb) mCb(str); } //template inline void DBGPRINTLN(String str, bool ser = true) { DBGPRINT(str); DBGPRINT(F("\r\n")); } inline void DHEX(uint8_t b, bool ser = true) { - if(ser) { + if(ser && mDebugEn) { if( b<0x10 ) DSERIAL.print(F("0")); DSERIAL.print(b,HEX); } diff --git a/src/utils/improv.h b/src/utils/improv.h index c3b8c87c..ade1291e 100644 --- a/src/utils/improv.h +++ b/src/utils/improv.h @@ -9,6 +9,7 @@ #include #include #include "dbg.h" +#include "AsyncJson.h" // https://www.improv-wifi.com/serial/ // https://github.com/jnthas/improv-wifi-demo/blob/main/src/esp32-wifiimprov/esp32-wifiimprov.ino @@ -16,12 +17,18 @@ // configure ESP through Serial interface class Improv { public: - void setup(const char *devName, const char *version) { + void setup(IApp *app, const char *devName, const char *version) { + mApp = app; mDevName = devName; mVersion = version; + + mScanRunning = false; } void tickSerial(void) { + if(mScanRunning) + getNetworks(); + if(Serial.available() == 0) return; @@ -114,7 +121,7 @@ class Improv { } void sendDevInfo(void) { - uint8_t buf[100]; + uint8_t buf[50]; buf[7] = TYPE_RPC_RESPONSE; buf[9] = GET_DEVICE_INFO; // repsonse to cmd uint8_t p = 11; @@ -137,6 +144,37 @@ class Improv { sendPaket(buf, p); } + void getNetworks(void) { + if(!mScanRunning) + mApp->scanAvailNetworks(); + + JsonObject obj; + if(!mApp->getAvailNetworks(obj)) + return; + + mScanRunning = false; + + uint8_t buf[50]; + buf[7] = TYPE_RPC_RESPONSE; + buf[9] = GET_WIFI_NETWORKS; // repsonse to cmd + uint8_t p = 11; + + JsonArray arr = obj[F("networks")]; + for(uint8_t i = 0; i < arr.size(); i++) { + buf[p++] = strlen(arr[i][F("ssid")]); + // ssid + p += char2Improv(arr[i][F("ssid")], &buf[p]); + buf[p++] = String(arr[i][F("rssi")]).length(); + // rssi + p += char2Improv(String(arr[i][F("rssi")]).c_str(), &buf[p]); + + buf[10] = p - 11; // sub length + buf[8] = p - 9; // paket length + + sendPaket(buf, p); + } + } + void setState(uint8_t state) { uint8_t buf[20]; buf[7] = TYPE_CURRENT_STATE; @@ -162,14 +200,20 @@ class Improv { void parsePayload(uint8_t type, uint8_t buf[], uint8_t len) { if(TYPE_RPC == type) { - if(GET_CURRENT_STATE == buf[0]) + if(GET_CURRENT_STATE == buf[0]) { + setDebugEn(false); setState(STATE_AUTHORIZED); + } else if(GET_DEVICE_INFO == buf[0]) sendDevInfo(); + else if(GET_WIFI_NETWORKS == buf[0]) + getNetworks(); } } + IApp *mApp; const char *mDevName, *mVersion; + bool mScanRunning; }; #endif /*__IMPROV_H__*/ diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index 08c19070..f590f67e 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -274,12 +274,12 @@ void ahoywifi::scanAvailNetworks(void) { } //----------------------------------------------------------------------------- -void ahoywifi::getAvailNetworks(JsonObject obj) { +bool ahoywifi::getAvailNetworks(JsonObject obj) { JsonArray nets = obj.createNestedArray("networks"); int n = WiFi.scanComplete(); if (n < 0) - return; + return false; if(n > 0) { int sort[n]; sortRSSI(&sort[0], n); @@ -292,6 +292,8 @@ void ahoywifi::getAvailNetworks(JsonObject obj) { WiFi.scanDelete(); if(mStaConn == IN_AP_MODE) WiFi.mode(WIFI_AP); + + return true; } //----------------------------------------------------------------------------- diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index 3a3a35a8..3fde2ab3 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -27,7 +27,7 @@ class ahoywifi { void tickWifiLoop(void); bool getNtpTime(void); void scanAvailNetworks(void); - void getAvailNetworks(JsonObject obj); + bool getAvailNetworks(JsonObject obj); private: typedef enum WiFiStatus {