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()) { if (currentHour != hour()) {
currentHour = hour(); currentHour = hour();
Display::adjustBrightness(); Display::adjustBrightness();
hourChanged.signal();
if (currentHour == 8) Wifi::reconnect(); if (currentHour == 8) Wifi::reconnect();
} }
if (currentMin != minute()) { if (currentMin != minute()) {

View File

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

View File

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

View File

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

View File

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

View File

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