use events for Wifi

This commit is contained in:
Nicu Hodos 2021-12-06 09:15:31 +01:00
parent f807698cdd
commit 25dcda2dfa
5 changed files with 66 additions and 45 deletions

View File

@ -9,13 +9,10 @@
namespace Display { namespace Display {
void displayColon();
uint8_t brightness = BRIGHTNESS; uint8_t brightness = BRIGHTNESS;
int currentHour = -1;
int currentMin = -1; int currentMin = -1;
Task blinkColon(500, TASK_FOREVER, displayColon, &ts, true);
// Create display object // Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment(); Adafruit_7segment clockDisplay = Adafruit_7segment();
@ -44,7 +41,6 @@ namespace Display {
} }
void adjustBrightness() { void adjustBrightness() {
int currentHour = hour();
if (currentHour > 8 && currentHour < 17) { if (currentHour > 8 && currentHour < 17) {
brightness = 11; brightness = 11;
} else { } else {
@ -56,13 +52,21 @@ namespace Display {
void displayColon() { void displayColon() {
static bool colonOn = false; static bool colonOn = false;
if (colonOn && currentMin != minute()) { if (colonOn) {
currentMin = minute(); if (currentHour != hour()) {
displayTime(); currentHour = hour();
Display::adjustBrightness();
hourChanged.signal();
if (currentHour == 8) dayChanged.signal();
}
if (currentMin != minute()) {
currentMin = minute();
displayTime();
}
} }
clockDisplay.drawColon(colonOn); clockDisplay.drawColon(colonOn);
clockDisplay.writeDisplay(); clockDisplay.writeDisplay();
colonOn = !colonOn; colonOn = !colonOn;
} }

View File

@ -70,14 +70,14 @@ namespace Ir {
{ {
case 0x9F: case 0x9F:
avrOn = false; avrOn = false;
Ntp::timeAtStartup = now(); Ntp::lastConnectedTime = now();
break; break;
case 0xC4: case 0xC4:
case 0xD0: case 0xD0:
case 0xC0: case 0xC0:
avrOn = true; avrOn = true;
if (WiFi.status() == WL_DISCONNECTED) { if (WiFi.status() == WL_DISCONNECTED) {
wifi.reconnect(); Wifi::reconnect();
// connect on wifi connected // connect on wifi connected
mqttClient.connect(); mqttClient.connect();
} }

View File

@ -8,7 +8,7 @@ namespace Ntp {
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
time_t timeAtStartup; time_t lastConnectedTime;
// Central European Time (Frankfurt, Paris) // Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
@ -27,6 +27,5 @@ namespace Ntp {
void setup() { void setup() {
timeClient.begin(); timeClient.begin();
timeAtStartup = updateTime();
} }
} }

View File

@ -2,20 +2,25 @@
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
class Wifi { namespace Wifi {
WiFiEventHandler stationConnectedHandler;
private:
String currentSSID; String currentSSID;
String currentPsk; String currentPsk;
public:
void setup() { void setup() {
stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& e) {
Serial.println("Reconnected to network.");
wifiConnected.signal();
});
WiFi.hostname("esp-clock");
ESP8266WiFiMulti wifiMulti; ESP8266WiFiMulti wifiMulti;
wifiMulti.addAP("OpenWrt", "***REMOVED***"); wifiMulti.addAP("OpenWrt", "***REMOVED***");
// wifiMulti.addAP("IoT", "***REMOVED***");
wifiMulti.addAP("Miracle", "***REMOVED***"); wifiMulti.addAP("Miracle", "***REMOVED***");
WiFi.hostname("esp-clock");
Serial.println("Connecting to WiFi netowrk."); Serial.println("Connecting to WiFi netowrk.");
while (wifiMulti.run() != WL_CONNECTED) { while (wifiMulti.run() != WL_CONNECTED) {
delay(500); delay(500);
@ -30,9 +35,6 @@ public:
WiFi.forceSleepWake(); WiFi.forceSleepWake();
WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
Serial.println("Reconnecting to WiFi netowrk..."); Serial.println("Reconnecting to WiFi netowrk...");
for (int i = 0; i < 4; i++) {
delay(1000);
}
} }
} }
@ -58,4 +60,4 @@ public:
Serial.print(rssi); Serial.print(rssi);
Serial.println(" dBm"); Serial.println(" dBm");
} }
} wifi; }

View File

@ -1,51 +1,67 @@
#include <Arduino.h> #include <Arduino.h>
#define _TASK_STATUS_REQUEST
#include <TaskScheduler.h> #include <TaskScheduler.h>
Scheduler ts; Scheduler ts;
StatusRequest hourChanged;
StatusRequest dayChanged;
StatusRequest wifiConnected;
#include "wifi.h" #include "wifi.h"
#include "ota.h"
#include "ntp_time.h" #include "ntp_time.h"
#include "ota.h"
#include "display.h" #include "display.h"
#include "ir.h" #include "ir.h"
#define STAY_CONNECTED_AFTER_BOOT 5*60 #define STAY_CONNECTED_AFTER_BOOT 5*60
int currentHour = -1; void wifiConnectedCallback();
void otaCallback();
Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true);
Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts);
Task tWifiReconnect(Wifi::reconnect, &ts);
Task tWifiConnected(wifiConnectedCallback, &ts);
void setup() { void setup() {
Display::setup();
Serial.begin(9600); Serial.begin(9600);
wifi.setup(); Display::setup();
Ota::setup(); Ota::setup();
Ntp::setup(); Ntp::setup();
Display::adjustBrightness();
Ir::setup(); Ir::setup();
hourChanged.setWaiting();
dayChanged.setWaiting();
wifiConnected.setWaiting();
tWifiReconnect.waitFor(&dayChanged);
tWifiConnected.waitFor(&wifiConnected);
Wifi::setup();
} }
void loop() { void loop() {
Ir::loop(); Ir::loop();
if ((currentHour != hour())) {
Display::adjustBrightness();
wifi.reconnect();
wifi.printStatus();
if (WiFi.status() == WL_CONNECTED) {
if (time_t newTime = Ntp::updateTime()) Serial.println(asctime(localtime(&newTime)));
}
currentHour = hour();
}
if (WiFi.status() == WL_CONNECTED) {
Ota::loop();
if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) {
wifi.disconnect();
}
}
ts.execute(); ts.execute();
} }
void wifiConnectedCallback() {
Wifi::printStatus();
if (time_t newTime = Ntp::updateTime()) {
Serial.println(asctime(localtime(&newTime)));
Ntp::lastConnectedTime = newTime;
}
tOta.enable();
}
void otaCallback() {
Ota::loop();
if ((difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) {
Wifi::disconnect();
tOta.disable();
}
}