fix wifi reconnection event

This commit is contained in:
Nicu Hodos 2021-12-09 12:29:21 +01:00
parent 07c2e67a8f
commit be01072986
6 changed files with 14 additions and 21 deletions

View File

@ -60,7 +60,6 @@ namespace Display {
if (currentHour != hour()) {
currentHour = hour();
Display::adjustBrightness();
hourChanged.signal();
if (currentHour == 8) Wifi::reconnect();
}
if (currentMin != minute()) {

View File

@ -49,7 +49,7 @@ namespace Ir {
{
case 0x9F:
avrOn = false;
tCheckWifi.enable();
tCheckWifi.enableDelayed(5*TASK_SECOND);
Display::displayText("Off");
break;
case 0x12:

View File

@ -7,7 +7,7 @@
namespace Mqtt {
void publishCommand();
Task tPublish(TASK_SECOND, TASK_FOREVER, Mqtt::publishCommand, &ts);
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
AsyncMqttClient client;
@ -23,9 +23,6 @@ namespace Mqtt {
void pop() {
queue.pop();
}
uint8_t front() {
return queue.front();
}
} commands;
void publishCommand() {

View File

@ -27,5 +27,6 @@ namespace Ntp {
void setup() {
timeClient.begin();
Serial.println("NTP setup");
}
}

View File

@ -10,9 +10,9 @@ namespace Wifi {
String currentPsk;
void setup() {
stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& e) {
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
Serial.println("Reconnected to network.");
wifiConnected.signal();
tWifiConnected.restart();
});
WiFi.hostname("esp-clock");
@ -25,7 +25,6 @@ namespace Wifi {
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
}
Serial.println("Connected to network.");
currentSSID = WiFi.SSID();
currentPsk = WiFi.psk();
}

View File

@ -3,13 +3,11 @@
void checkWifiCallback();
void onWifiConnected();
#define _TASK_STATUS_REQUEST
// #define _TASK_STATUS_REQUEST
#include <TaskScheduler.h>
Scheduler ts;
StatusRequest hourChanged;
StatusRequest wifiConnected;
Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts);
Task tWifiConnected(onWifiConnected, &ts);
Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
#include "wifi.h"
#include "display.h"
@ -19,9 +17,10 @@ Task tWifiConnected(onWifiConnected, &ts);
#include "ota.h"
#include "ir.h"
#define STAY_CONNECTED_AFTER_BOOT 30*60
#define STAY_CONNECTED_AFTER_BOOT 15
void setup() {
Serial.begin(9600);
Display::setup();
@ -31,10 +30,6 @@ void setup() {
Mqtt::setup();
Bmp::setup();
hourChanged.setWaiting();
wifiConnected.setWaiting();
tWifiConnected.waitFor(&wifiConnected);
Wifi::setup();
}
@ -43,18 +38,20 @@ void loop() {
}
void onWifiConnected() {
Serial.println("Wifi connected event");
Wifi::printStatus();
Ota::tLoop.enable();
if (!Ir::avrOn) tCheckWifi.enable();
Mqtt::client.connect();
if (time_t newTime = Ntp::updateTime()) {
Serial.println(asctime(localtime(&newTime)));
Ntp::lastConnectedTime = newTime;
}
Ota::tLoop.enable();
if (!Ir::avrOn) tCheckWifi.enable();
Mqtt::client.connect();
}
void checkWifiCallback() {
if (difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) {
Serial.println("Wifi connection timed out");
Mqtt::client.disconnect();
Wifi::disconnect();
Ota::tLoop.disable();