mirror of
https://github.com/lumapu/ahoy.git
synced 2025-06-06 12:41:37 +02:00
fix night communication enable
improved different WiFi connection scenarios (STA WiFi not found, reconnect #509, redirect for AP to configuration)
This commit is contained in:
parent
07bf947ff7
commit
de4c572ee2
10 changed files with 171 additions and 164 deletions
12
src/.vscode/settings.json
vendored
12
src/.vscode/settings.json
vendored
|
@ -73,6 +73,16 @@
|
||||||
"sstream": "cpp",
|
"sstream": "cpp",
|
||||||
"stdexcept": "cpp",
|
"stdexcept": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"cinttypes": "cpp"
|
"cinttypes": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"mutex": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"stop_token": "cpp",
|
||||||
|
"thread": "cpp"
|
||||||
},
|
},
|
||||||
}
|
}
|
|
@ -1,17 +1,21 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.5.59
|
||||||
|
* fix night communication enable
|
||||||
|
* improved different WiFi connection scenarios (STA WiFi not found, reconnect #509, redirect for AP to configuration)
|
||||||
|
|
||||||
## 0.5.58
|
## 0.5.58
|
||||||
* improved stability
|
* improved stability
|
||||||
* improved wifi initial connection - especially if station wifi is not available
|
* improved WiFi initial connection - especially if station WiFi is not available
|
||||||
* removed new operators from web.h (reduce dynamic allocation)
|
* removed new operators from web.h (reduce dynamic allocation)
|
||||||
* improved sun calculation #515, #505
|
* improved sun calculation #515, #505
|
||||||
* fixed wifi auto reconnect #509
|
* fixed WiFi auto reconnect #509
|
||||||
* added disable night communication flag to MQTT #505
|
* added disable night communication flag to MQTT #505
|
||||||
* changed MQTT publish of `available` and `available_text` to sunset #468
|
* changed MQTT publish of `available` and `available_text` to sunset #468
|
||||||
|
|
||||||
## 0.5.57
|
## 0.5.57
|
||||||
* improved stability
|
* improved stability
|
||||||
* added icons to index.html, added wifi-strength symbol on each page
|
* added icons to index.html, added WiFi-strength symbol on each page
|
||||||
* moved packet stats and sun to system.html
|
* moved packet stats and sun to system.html
|
||||||
* refactored communication offset (adjustable in minutes now)
|
* refactored communication offset (adjustable in minutes now)
|
||||||
|
|
||||||
|
@ -32,7 +36,7 @@
|
||||||
|
|
||||||
## 0.5.53
|
## 0.5.53
|
||||||
* Mono-Display: show values in offline mode #498
|
* Mono-Display: show values in offline mode #498
|
||||||
* improved wifi class #483
|
* improved WiFi class #483
|
||||||
* added communication enable / disable (to test mutliple DTUs with the same inverter)
|
* added communication enable / disable (to test mutliple DTUs with the same inverter)
|
||||||
* fix factory reset #495
|
* fix factory reset #495
|
||||||
|
|
||||||
|
|
|
@ -266,6 +266,7 @@ void app::resetSystem(void) {
|
||||||
|
|
||||||
mSendLastIvId = 0;
|
mSendLastIvId = 0;
|
||||||
mShowRebootRequest = false;
|
mShowRebootRequest = false;
|
||||||
|
mIVCommunicationOn = true;
|
||||||
|
|
||||||
memset(&mStat, 0, sizeof(statistics_t));
|
memset(&mStat, 0, sizeof(statistics_t));
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,10 @@ class app : public IApp, public ah::Scheduler {
|
||||||
return mApi.getTimezoneOffset();
|
return mApi.getTimezoneOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getSchedulerInfo(uint16_t *everyMax, uint16_t *atMax) {
|
||||||
|
return getStat(everyMax, atMax);
|
||||||
|
}
|
||||||
|
|
||||||
void setTimestamp(uint32_t newTime) {
|
void setTimestamp(uint32_t newTime) {
|
||||||
DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime));
|
DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime));
|
||||||
if(0 == newTime)
|
if(0 == newTime)
|
||||||
|
|
|
@ -28,6 +28,7 @@ class IApp {
|
||||||
virtual void setTimestamp(uint32_t newTime) = 0;
|
virtual void setTimestamp(uint32_t newTime) = 0;
|
||||||
virtual String getTimeStr(uint32_t offset) = 0;
|
virtual String getTimeStr(uint32_t offset) = 0;
|
||||||
virtual uint32_t getTimezoneOffset() = 0;
|
virtual uint32_t getTimezoneOffset() = 0;
|
||||||
|
virtual void getSchedulerInfo(uint16_t *everyMax, uint16_t *atMax);
|
||||||
|
|
||||||
virtual bool getRebootRequestState() = 0;
|
virtual bool getRebootRequestState() = 0;
|
||||||
virtual bool getSettingsValid() = 0;
|
virtual bool getSettingsValid() = 0;
|
||||||
|
|
|
@ -93,9 +93,9 @@ namespace ah {
|
||||||
return mTimestamp;
|
return mTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stat() {
|
void getStat(uint16_t *everyMax, uint16_t *atMax) {
|
||||||
DPRINTLN(DBG_INFO, "max fill every: " + String(mStack.getMaxFill()));
|
*everyMax = mStack.getMaxFill();
|
||||||
DPRINTLN(DBG_INFO, "max fill at: " + String(mStackAt.getMaxFill()));
|
*atMax = mStackAt.getMaxFill();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -199,6 +199,11 @@ class RestApi {
|
||||||
#endif
|
#endif
|
||||||
//obj[F("littlefs_total")] = LittleFS.totalBytes();
|
//obj[F("littlefs_total")] = LittleFS.totalBytes();
|
||||||
//obj[F("littlefs_used")] = LittleFS.usedBytes();
|
//obj[F("littlefs_used")] = LittleFS.usedBytes();
|
||||||
|
|
||||||
|
uint16_t evry, at;
|
||||||
|
mApp->getSchedulerInfo(&evry, &at);
|
||||||
|
obj[F("schEvryMax")] = evry;
|
||||||
|
obj[F("schAtMax")] = at;
|
||||||
}
|
}
|
||||||
|
|
||||||
void getHtmlSystem(JsonObject obj) {
|
void getHtmlSystem(JsonObject obj) {
|
||||||
|
|
|
@ -285,20 +285,10 @@ class Web {
|
||||||
}
|
}
|
||||||
|
|
||||||
void showNotFound(AsyncWebServerRequest *request) {
|
void showNotFound(AsyncWebServerRequest *request) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("showNotFound - ") + request->url());
|
if(mProtected)
|
||||||
String msg = F("File Not Found\n\nURL: ");
|
request->redirect("/login");
|
||||||
msg += request->url();
|
else
|
||||||
msg += F("\nMethod: ");
|
request->redirect("/setup");
|
||||||
msg += ( request->method() == HTTP_GET ) ? "GET" : "POST";
|
|
||||||
msg += F("\nArguments: ");
|
|
||||||
msg += request->args();
|
|
||||||
msg += "\n";
|
|
||||||
|
|
||||||
for(uint8_t i = 0; i < request->args(); i++ ) {
|
|
||||||
msg += " " + request->argName(i) + ": " + request->arg(i) + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
request->send(404, F("text/plain"), msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onReboot(AsyncWebServerRequest *request) {
|
void onReboot(AsyncWebServerRequest *request) {
|
||||||
|
|
|
@ -12,39 +12,50 @@
|
||||||
// NTP CONFIG
|
// NTP CONFIG
|
||||||
#define NTP_PACKET_SIZE 48
|
#define NTP_PACKET_SIZE 48
|
||||||
|
|
||||||
enum {WIFI_NOT_FOUND = 0, WIFI_FOUND, WIFI_NOT_COMPLETE};
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
ahoywifi::ahoywifi() : mApIp(192, 168, 4, 1), mApMask(255, 255, 255, 0) {
|
ahoywifi::ahoywifi() : mApIp(192, 168, 4, 1) {}
|
||||||
mDnsActive = false;
|
|
||||||
mClientCnt = 0;
|
|
||||||
mLoopCnt = 250;
|
|
||||||
mExtScan = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
|
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
|
||||||
mCnt = 0;
|
|
||||||
mConnected = false;
|
|
||||||
mReconnect = false;
|
|
||||||
mConfig = config;
|
mConfig = config;
|
||||||
mUtcTimestamp = utcTimestamp;
|
mUtcTimestamp = utcTimestamp;
|
||||||
|
|
||||||
if(String(mConfig->sys.deviceName) != "")
|
mCnt = 0;
|
||||||
WiFi.hostname(mConfig->sys.deviceName);
|
mConnected = false;
|
||||||
|
mReconnect = false;
|
||||||
#if !defined(FB_WIFI_OVERRIDDEN)
|
mScanActive = false;
|
||||||
setupAp();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
||||||
|
wifiGotIPHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onGotIP, this, std::placeholders::_1));
|
||||||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1));
|
wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1));
|
||||||
#else
|
#else
|
||||||
WiFi.onEvent(std::bind(&ahoywifi::onWiFiEvent, this, std::placeholders::_1));
|
WiFi.onEvent(std::bind(&ahoywifi::onWiFiEvent, this, std::placeholders::_1));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
setupWifi();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void ahoywifi::setupWifi(void) {
|
||||||
|
#if !defined(FB_WIFI_OVERRIDDEN)
|
||||||
|
//if(strncmp(mConfig->sys.stationSsid, FB_WIFI_SSID, 14) == 0)
|
||||||
|
setupAp();
|
||||||
|
delay(1000);
|
||||||
|
#endif
|
||||||
|
#if !defined(AP_ONLY)
|
||||||
|
if(mConfig->valid) {
|
||||||
|
#if !defined(FB_WIFI_OVERRIDDEN)
|
||||||
|
if(strncmp(mConfig->sys.stationSsid, FB_WIFI_SSID, 14) != 0)
|
||||||
|
setupStation();
|
||||||
|
#else
|
||||||
|
setupStation();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,38 +64,29 @@ void ahoywifi::loop() {
|
||||||
#if !defined(AP_ONLY)
|
#if !defined(AP_ONLY)
|
||||||
if(mReconnect) {
|
if(mReconnect) {
|
||||||
delay(100);
|
delay(100);
|
||||||
mCnt++;
|
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
|
||||||
if((mCnt % 50) == 0)
|
mDns.processNextRequest();
|
||||||
WiFi.disconnect();
|
if((WIFI_AP_STA == WiFi.getMode()) && !mScanActive) {
|
||||||
else if((mCnt % 60) == 0) {
|
DBGPRINTLN(F("AP client connected"));
|
||||||
DPRINTLN(DBG_INFO, F("[WiFi] reconnect"));
|
welcome(mApIp.toString());
|
||||||
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd);
|
WiFi.mode(WIFI_AP);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if(WIFI_AP == WiFi.getMode()) {
|
||||||
mCnt = 0;
|
mCnt = 0;
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
}
|
}
|
||||||
}
|
mCnt++;
|
||||||
yield();
|
|
||||||
if(mDnsActive) {
|
|
||||||
mDns.processNextRequest();
|
|
||||||
uint8_t cnt = WiFi.softAPgetStationNum();
|
|
||||||
if(cnt != mClientCnt) {
|
|
||||||
mClientCnt = cnt;
|
|
||||||
DPRINTLN(DBG_INFO, String(cnt) + F(" client(s) connected"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!mExtScan && (mLoopCnt == 240)) {
|
if ((mCnt % 10) == 0) {
|
||||||
if(scanStationNetwork()) {
|
DBGPRINT(F("reconnect in "));
|
||||||
setupStation();
|
DBGPRINT(String((100-mCnt)/10));
|
||||||
mLoopCnt = 0;
|
DBGPRINTLN(F(" seconds"));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if((mCnt % 100) == 0) { // try to reconnect after 10 sec without connection
|
||||||
if(0 != mLoopCnt) {
|
WiFi.reconnect();
|
||||||
if(++mLoopCnt > 250) {
|
mCnt = 0;
|
||||||
mLoopCnt = 1;
|
|
||||||
if(!mExtScan)
|
|
||||||
scanAvailNetworks(false);
|
|
||||||
}
|
|
||||||
delay(25);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -93,7 +95,7 @@ void ahoywifi::loop() {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::setupAp(void) {
|
void ahoywifi::setupAp(void) {
|
||||||
DPRINTLN(DBG_INFO, F("wifi::setupAp"));
|
DPRINTLN(DBG_VERBOSE, F("wifi::setupAp"));
|
||||||
|
|
||||||
DBGPRINTLN(F("\n---------\nAhoyDTU Info:"));
|
DBGPRINTLN(F("\n---------\nAhoyDTU Info:"));
|
||||||
DBGPRINT(F("Version: "));
|
DBGPRINT(F("Version: "));
|
||||||
|
@ -109,18 +111,16 @@ void ahoywifi::setupAp(void) {
|
||||||
DBGPRINTLN(F("---------\n"));
|
DBGPRINTLN(F("---------\n"));
|
||||||
|
|
||||||
WiFi.mode(WIFI_AP_STA);
|
WiFi.mode(WIFI_AP_STA);
|
||||||
WiFi.softAPConfig(mApIp, mApIp, mApMask);
|
WiFi.softAPConfig(mApIp, mApIp, IPAddress(255, 255, 255, 0));
|
||||||
WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD);
|
WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD);
|
||||||
|
|
||||||
mDns.setErrorReplyCode(DNSReplyCode::NoError);
|
mDns.start(53, "*", mApIp);
|
||||||
mDns.start(53, "*", WiFi.softAPIP());
|
|
||||||
mDnsActive = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::setupStation(void) {
|
void ahoywifi::setupStation(void) {
|
||||||
DPRINTLN(DBG_INFO, F("wifi::setupStation"));
|
DPRINTLN(DBG_VERBOSE, F("wifi::setupStation"));
|
||||||
if(mConfig->sys.ip.ip[0] != 0) {
|
if(mConfig->sys.ip.ip[0] != 0) {
|
||||||
IPAddress ip(mConfig->sys.ip.ip);
|
IPAddress ip(mConfig->sys.ip.ip);
|
||||||
IPAddress mask(mConfig->sys.ip.mask);
|
IPAddress mask(mConfig->sys.ip.mask);
|
||||||
|
@ -130,34 +130,18 @@ void ahoywifi::setupStation(void) {
|
||||||
if(!WiFi.config(ip, gateway, mask, dns1, dns2))
|
if(!WiFi.config(ip, gateway, mask, dns1, dns2))
|
||||||
DPRINTLN(DBG_ERROR, F("failed to set static IP!"));
|
DPRINTLN(DBG_ERROR, F("failed to set static IP!"));
|
||||||
}
|
}
|
||||||
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd);
|
mReconnect = (WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd) != WL_CONNECTED);
|
||||||
|
if(String(mConfig->sys.deviceName) != "")
|
||||||
|
WiFi.hostname(mConfig->sys.deviceName);
|
||||||
|
|
||||||
DBGPRINT(F("connect to network '"));
|
DBGPRINT(F("connect to network '"));
|
||||||
DBGPRINT(mConfig->sys.stationSsid);
|
DBGPRINT(mConfig->sys.stationSsid);
|
||||||
}
|
DBGPRINTLN(F("' ..."));
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
bool ahoywifi::scanStationNetwork(void) {
|
|
||||||
bool found = false;
|
|
||||||
int n = WiFi.scanComplete();
|
|
||||||
if(n > 0) {
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
DPRINTLN(DBG_INFO, "found network: " + WiFi.SSID(i));
|
|
||||||
if(String(mConfig->sys.stationSsid) == WiFi.SSID(i)) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
WiFi.scanDelete();
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
bool ahoywifi::getNtpTime(void) {
|
bool ahoywifi::getNtpTime(void) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
|
|
||||||
if(!mConnected)
|
if(!mConnected)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -165,9 +149,10 @@ bool ahoywifi::getNtpTime(void) {
|
||||||
uint8_t buf[NTP_PACKET_SIZE];
|
uint8_t buf[NTP_PACKET_SIZE];
|
||||||
uint8_t retry = 0;
|
uint8_t retry = 0;
|
||||||
|
|
||||||
WiFi.hostByName(mConfig->ntp.addr, timeServer);
|
if (WiFi.hostByName(mConfig->ntp.addr, timeServer) != 1)
|
||||||
mUdp.begin(mConfig->ntp.port);
|
return false;
|
||||||
|
|
||||||
|
mUdp.begin(mConfig->ntp.port);
|
||||||
sendNTPpacket(timeServer);
|
sendNTPpacket(timeServer);
|
||||||
|
|
||||||
while(retry++ < 5) {
|
while(retry++ < 5) {
|
||||||
|
@ -194,38 +179,6 @@ bool ahoywifi::getNtpTime(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
void ahoywifi::scanAvailNetworks(bool externalCall) {
|
|
||||||
if(externalCall)
|
|
||||||
mExtScan = true;
|
|
||||||
if(-2 == WiFi.scanComplete())
|
|
||||||
WiFi.scanNetworks(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
void ahoywifi::getAvailNetworks(JsonObject obj) {
|
|
||||||
JsonArray nets = obj.createNestedArray("networks");
|
|
||||||
|
|
||||||
int n = WiFi.scanComplete();
|
|
||||||
if(n > 0) {
|
|
||||||
int sort[n];
|
|
||||||
for (int i = 0; i < n; i++)
|
|
||||||
sort[i] = i;
|
|
||||||
for (int i = 0; i < n; i++)
|
|
||||||
for (int j = i + 1; j < n; j++)
|
|
||||||
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i]))
|
|
||||||
std::swap(sort[i], sort[j]);
|
|
||||||
for (int i = 0; i < n; ++i) {
|
|
||||||
nets[i]["ssid"] = WiFi.SSID(sort[i]);
|
|
||||||
nets[i]["rssi"] = WiFi.RSSI(sort[i]);
|
|
||||||
}
|
|
||||||
WiFi.scanDelete();
|
|
||||||
}
|
|
||||||
mExtScan = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::sendNTPpacket(IPAddress& address) {
|
void ahoywifi::sendNTPpacket(IPAddress& address) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket"));
|
//DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket"));
|
||||||
|
@ -248,58 +201,96 @@ void ahoywifi::sendNTPpacket(IPAddress& address) {
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
#if defined(ESP8266)
|
void ahoywifi::scanAvailNetworks(void) {
|
||||||
void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) {
|
if(-2 == WiFi.scanComplete()) {
|
||||||
|
mScanActive = true;
|
||||||
|
if(WIFI_AP == WiFi.getMode())
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
|
WiFi.scanNetworks(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
|
JsonArray nets = obj.createNestedArray("networks");
|
||||||
|
|
||||||
|
int n = WiFi.scanComplete();
|
||||||
|
if(n > 0) {
|
||||||
|
int sort[n];
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
sort[i] = i;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
for (int j = i + 1; j < n; j++)
|
||||||
|
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i]))
|
||||||
|
std::swap(sort[i], sort[j]);
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
nets[i]["ssid"] = WiFi.SSID(sort[i]);
|
||||||
|
nets[i]["rssi"] = WiFi.RSSI(sort[i]);
|
||||||
|
}
|
||||||
|
mScanActive = false;
|
||||||
|
WiFi.scanDelete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void ahoywifi::connectionEvent(bool connected) {
|
||||||
|
if (connected) {
|
||||||
if(!mConnected) {
|
if(!mConnected) {
|
||||||
mConnected = true;
|
mConnected = true;
|
||||||
mReconnect = false;
|
mReconnect = false;
|
||||||
DBGPRINTLN(F("\n[WiFi] Connected"));
|
DBGPRINTLN(F("\n[WiFi] Connected"));
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
DBGPRINTLN(F("[WiFi] AP disabled"));
|
DBGPRINTLN(F("[WiFi] AP disabled"));
|
||||||
mDnsActive = false;
|
|
||||||
mDns.stop();
|
mDns.stop();
|
||||||
|
|
||||||
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
|
||||||
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
|
||||||
if(mConnected) {
|
if(mConnected) {
|
||||||
mConnected = false;
|
mConnected = false;
|
||||||
mReconnect = true;
|
mReconnect = true;
|
||||||
mCnt = 0;
|
mCnt = 50; // try to reconnect in 5 sec
|
||||||
|
setupWifi(); // reconnect with AP / Station setup
|
||||||
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
#if defined(ESP8266)
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
void ahoywifi::onConnect(const WiFiEventStationModeConnected& event) {
|
||||||
|
connectionEvent(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
void ahoywifi::onGotIP(const WiFiEventStationModeGotIP& event) {
|
||||||
|
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||||
|
connectionEvent(false);
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
void ahoywifi::onWiFiEvent(WiFiEvent_t event) {
|
void ahoywifi::onWiFiEvent(WiFiEvent_t event) {
|
||||||
|
DBGPRINT(F("Wifi event: "));
|
||||||
|
DBGPRINTLN(String(event));
|
||||||
|
|
||||||
switch(event) {
|
switch(event) {
|
||||||
|
case SYSTEM_EVENT_STA_CONNECTED:
|
||||||
|
connectionEvent(true);
|
||||||
|
break;
|
||||||
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
if(!mConnected) {
|
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
||||||
delay(1000);
|
|
||||||
mConnected = true;
|
|
||||||
DBGPRINTLN(F("\n[WiFi] Connected"));
|
|
||||||
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
WiFi.begin();
|
|
||||||
DBGPRINTLN(F("[WiFi] AP disabled"));
|
|
||||||
mDnsActive = false;
|
|
||||||
mDns.stop();
|
|
||||||
mReconnect = false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
if(mConnected) {
|
connectionEvent(false);
|
||||||
mConnected = false;
|
|
||||||
mReconnect = true;
|
|
||||||
mCnt = 0;
|
|
||||||
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -23,16 +23,18 @@ class ahoywifi {
|
||||||
void setup(settings_t *config, uint32_t *utcTimestamp);
|
void setup(settings_t *config, uint32_t *utcTimestamp);
|
||||||
void loop(void);
|
void loop(void);
|
||||||
bool getNtpTime(void);
|
bool getNtpTime(void);
|
||||||
void scanAvailNetworks(bool externalCall = true);
|
void scanAvailNetworks(void);
|
||||||
void getAvailNetworks(JsonObject obj);
|
void getAvailNetworks(JsonObject obj);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void setupWifi(void);
|
||||||
void setupAp(void);
|
void setupAp(void);
|
||||||
void setupStation(void);
|
void setupStation(void);
|
||||||
bool scanStationNetwork(void);
|
|
||||||
void sendNTPpacket(IPAddress& address);
|
void sendNTPpacket(IPAddress& address);
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
void onConnect(const WiFiEventStationModeGotIP& event);
|
void connectionEvent(bool connected);
|
||||||
|
void onConnect(const WiFiEventStationModeConnected& event);
|
||||||
|
void onGotIP(const WiFiEventStationModeGotIP& event);
|
||||||
void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||||
#else
|
#else
|
||||||
void onWiFiEvent(WiFiEvent_t event);
|
void onWiFiEvent(WiFiEvent_t event);
|
||||||
|
@ -43,11 +45,10 @@ class ahoywifi {
|
||||||
settings_t *mConfig;
|
settings_t *mConfig;
|
||||||
|
|
||||||
DNSServer mDns;
|
DNSServer mDns;
|
||||||
IPAddress mApIp, mApMask;
|
IPAddress mApIp;
|
||||||
WiFiUDP mUdp; // for time server
|
WiFiUDP mUdp; // for time server
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool mConnected, mReconnect, mDnsActive;
|
bool mConnected, mReconnect, mDnsActive;
|
||||||
|
@ -55,7 +56,7 @@ class ahoywifi {
|
||||||
uint32_t *mUtcTimestamp;
|
uint32_t *mUtcTimestamp;
|
||||||
|
|
||||||
uint8_t mLoopCnt;
|
uint8_t mLoopCnt;
|
||||||
bool mExtScan;
|
bool mScanActive;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*__AHOYWIFI_H__*/
|
#endif /*__AHOYWIFI_H__*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue