mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-25 23:06:11 +02:00
improv does not open complete menu every time
This commit is contained in:
parent
ba3f84d684
commit
efe3bc4d20
8 changed files with 64 additions and 12 deletions
|
@ -84,7 +84,7 @@ void app::setup() {
|
||||||
|
|
||||||
mPubSerial.setup(mConfig, &mSys, &mTimestamp);
|
mPubSerial.setup(mConfig, &mSys, &mTimestamp);
|
||||||
|
|
||||||
mImprov.setup(mConfig->sys.deviceName, mVersion);
|
mImprov.setup(this, mConfig->sys.deviceName, mVersion);
|
||||||
|
|
||||||
regularTickers();
|
regularTickers();
|
||||||
|
|
||||||
|
|
|
@ -101,8 +101,8 @@ class app : public IApp, public ah::Scheduler {
|
||||||
mWifi.scanAvailNetworks();
|
mWifi.scanAvailNetworks();
|
||||||
}
|
}
|
||||||
|
|
||||||
void getAvailNetworks(JsonObject obj) {
|
bool getAvailNetworks(JsonObject obj) {
|
||||||
mWifi.getAvailNetworks(obj);
|
return mWifi.getAvailNetworks(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOnUpdate() {
|
void setOnUpdate() {
|
||||||
|
|
|
@ -25,7 +25,7 @@ class IApp {
|
||||||
virtual const char *getVersion() = 0;
|
virtual const char *getVersion() = 0;
|
||||||
virtual statistics_t *getStatistics() = 0;
|
virtual statistics_t *getStatistics() = 0;
|
||||||
virtual void scanAvailNetworks() = 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 getUptime() = 0;
|
||||||
virtual uint32_t getTimestamp() = 0;
|
virtual uint32_t getTimestamp() = 0;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
#include "dbg.h"
|
#include "dbg.h"
|
||||||
|
|
||||||
DBG_CB mCb = NULL;
|
DBG_CB mCb = NULL;
|
||||||
|
bool mDebugEn = true;
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
#ifdef ARDUINO
|
#ifdef ARDUINO
|
||||||
#define DBG_CB std::function<void(String)>
|
#define DBG_CB std::function<void(String)>
|
||||||
extern DBG_CB mCb;
|
extern DBG_CB mCb;
|
||||||
|
extern bool mDebugEn;
|
||||||
|
|
||||||
inline void registerDebugCb(DBG_CB cb) {
|
inline void registerDebugCb(DBG_CB cb) {
|
||||||
mCb = cb;
|
mCb = cb;
|
||||||
|
@ -48,12 +49,16 @@
|
||||||
#define DSERIAL Serial
|
#define DSERIAL Serial
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
inline void setDebugEn(bool en) {
|
||||||
|
mDebugEn = en;
|
||||||
|
}
|
||||||
|
|
||||||
//template <class T>
|
//template <class T>
|
||||||
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 <class T>
|
//template <class T>
|
||||||
inline void DBGPRINTLN(String str, bool ser = true) { DBGPRINT(str); DBGPRINT(F("\r\n")); }
|
inline void DBGPRINTLN(String str, bool ser = true) { DBGPRINT(str); DBGPRINT(F("\r\n")); }
|
||||||
inline void DHEX(uint8_t b, bool ser = true) {
|
inline void DHEX(uint8_t b, bool ser = true) {
|
||||||
if(ser) {
|
if(ser && mDebugEn) {
|
||||||
if( b<0x10 ) DSERIAL.print(F("0"));
|
if( b<0x10 ) DSERIAL.print(F("0"));
|
||||||
DSERIAL.print(b,HEX);
|
DSERIAL.print(b,HEX);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "dbg.h"
|
#include "dbg.h"
|
||||||
|
#include "AsyncJson.h"
|
||||||
|
|
||||||
// https://www.improv-wifi.com/serial/
|
// https://www.improv-wifi.com/serial/
|
||||||
// https://github.com/jnthas/improv-wifi-demo/blob/main/src/esp32-wifiimprov/esp32-wifiimprov.ino
|
// https://github.com/jnthas/improv-wifi-demo/blob/main/src/esp32-wifiimprov/esp32-wifiimprov.ino
|
||||||
|
@ -16,12 +17,18 @@
|
||||||
// configure ESP through Serial interface
|
// configure ESP through Serial interface
|
||||||
class Improv {
|
class Improv {
|
||||||
public:
|
public:
|
||||||
void setup(const char *devName, const char *version) {
|
void setup(IApp *app, const char *devName, const char *version) {
|
||||||
|
mApp = app;
|
||||||
mDevName = devName;
|
mDevName = devName;
|
||||||
mVersion = version;
|
mVersion = version;
|
||||||
|
|
||||||
|
mScanRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tickSerial(void) {
|
void tickSerial(void) {
|
||||||
|
if(mScanRunning)
|
||||||
|
getNetworks();
|
||||||
|
|
||||||
if(Serial.available() == 0)
|
if(Serial.available() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -114,7 +121,7 @@ class Improv {
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendDevInfo(void) {
|
void sendDevInfo(void) {
|
||||||
uint8_t buf[100];
|
uint8_t buf[50];
|
||||||
buf[7] = TYPE_RPC_RESPONSE;
|
buf[7] = TYPE_RPC_RESPONSE;
|
||||||
buf[9] = GET_DEVICE_INFO; // repsonse to cmd
|
buf[9] = GET_DEVICE_INFO; // repsonse to cmd
|
||||||
uint8_t p = 11;
|
uint8_t p = 11;
|
||||||
|
@ -137,6 +144,37 @@ class Improv {
|
||||||
sendPaket(buf, p);
|
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) {
|
void setState(uint8_t state) {
|
||||||
uint8_t buf[20];
|
uint8_t buf[20];
|
||||||
buf[7] = TYPE_CURRENT_STATE;
|
buf[7] = TYPE_CURRENT_STATE;
|
||||||
|
@ -162,14 +200,20 @@ class Improv {
|
||||||
|
|
||||||
void parsePayload(uint8_t type, uint8_t buf[], uint8_t len) {
|
void parsePayload(uint8_t type, uint8_t buf[], uint8_t len) {
|
||||||
if(TYPE_RPC == type) {
|
if(TYPE_RPC == type) {
|
||||||
if(GET_CURRENT_STATE == buf[0])
|
if(GET_CURRENT_STATE == buf[0]) {
|
||||||
|
setDebugEn(false);
|
||||||
setState(STATE_AUTHORIZED);
|
setState(STATE_AUTHORIZED);
|
||||||
|
}
|
||||||
else if(GET_DEVICE_INFO == buf[0])
|
else if(GET_DEVICE_INFO == buf[0])
|
||||||
sendDevInfo();
|
sendDevInfo();
|
||||||
|
else if(GET_WIFI_NETWORKS == buf[0])
|
||||||
|
getNetworks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IApp *mApp;
|
||||||
const char *mDevName, *mVersion;
|
const char *mDevName, *mVersion;
|
||||||
|
bool mScanRunning;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*__IMPROV_H__*/
|
#endif /*__IMPROV_H__*/
|
||||||
|
|
|
@ -274,12 +274,12 @@ void ahoywifi::scanAvailNetworks(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::getAvailNetworks(JsonObject obj) {
|
bool ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
JsonArray nets = obj.createNestedArray("networks");
|
JsonArray nets = obj.createNestedArray("networks");
|
||||||
|
|
||||||
int n = WiFi.scanComplete();
|
int n = WiFi.scanComplete();
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return;
|
return false;
|
||||||
if(n > 0) {
|
if(n > 0) {
|
||||||
int sort[n];
|
int sort[n];
|
||||||
sortRSSI(&sort[0], n);
|
sortRSSI(&sort[0], n);
|
||||||
|
@ -292,6 +292,8 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
WiFi.scanDelete();
|
WiFi.scanDelete();
|
||||||
if(mStaConn == IN_AP_MODE)
|
if(mStaConn == IN_AP_MODE)
|
||||||
WiFi.mode(WIFI_AP);
|
WiFi.mode(WIFI_AP);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
@ -27,7 +27,7 @@ class ahoywifi {
|
||||||
void tickWifiLoop(void);
|
void tickWifiLoop(void);
|
||||||
bool getNtpTime(void);
|
bool getNtpTime(void);
|
||||||
void scanAvailNetworks(void);
|
void scanAvailNetworks(void);
|
||||||
void getAvailNetworks(JsonObject obj);
|
bool getAvailNetworks(JsonObject obj);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef enum WiFiStatus {
|
typedef enum WiFiStatus {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue