use events for Wifi
This commit is contained in:
parent
f807698cdd
commit
25dcda2dfa
@ -9,13 +9,10 @@
|
||||
|
||||
namespace Display {
|
||||
|
||||
void displayColon();
|
||||
|
||||
uint8_t brightness = BRIGHTNESS;
|
||||
int currentHour = -1;
|
||||
int currentMin = -1;
|
||||
|
||||
Task blinkColon(500, TASK_FOREVER, displayColon, &ts, true);
|
||||
|
||||
// Create display object
|
||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||
|
||||
@ -44,7 +41,6 @@ namespace Display {
|
||||
}
|
||||
|
||||
void adjustBrightness() {
|
||||
int currentHour = hour();
|
||||
if (currentHour > 8 && currentHour < 17) {
|
||||
brightness = 11;
|
||||
} else {
|
||||
@ -56,9 +52,17 @@ namespace Display {
|
||||
void displayColon() {
|
||||
static bool colonOn = false;
|
||||
|
||||
if (colonOn && currentMin != minute()) {
|
||||
currentMin = minute();
|
||||
displayTime();
|
||||
if (colonOn) {
|
||||
if (currentHour != hour()) {
|
||||
currentHour = hour();
|
||||
Display::adjustBrightness();
|
||||
hourChanged.signal();
|
||||
if (currentHour == 8) dayChanged.signal();
|
||||
}
|
||||
if (currentMin != minute()) {
|
||||
currentMin = minute();
|
||||
displayTime();
|
||||
}
|
||||
}
|
||||
clockDisplay.drawColon(colonOn);
|
||||
clockDisplay.writeDisplay();
|
||||
|
||||
@ -70,14 +70,14 @@ namespace Ir {
|
||||
{
|
||||
case 0x9F:
|
||||
avrOn = false;
|
||||
Ntp::timeAtStartup = now();
|
||||
Ntp::lastConnectedTime = now();
|
||||
break;
|
||||
case 0xC4:
|
||||
case 0xD0:
|
||||
case 0xC0:
|
||||
avrOn = true;
|
||||
if (WiFi.status() == WL_DISCONNECTED) {
|
||||
wifi.reconnect();
|
||||
Wifi::reconnect();
|
||||
// connect on wifi connected
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ namespace Ntp {
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
|
||||
time_t timeAtStartup;
|
||||
time_t lastConnectedTime;
|
||||
|
||||
// Central European Time (Frankfurt, Paris)
|
||||
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
|
||||
@ -27,6 +27,5 @@ namespace Ntp {
|
||||
|
||||
void setup() {
|
||||
timeClient.begin();
|
||||
timeAtStartup = updateTime();
|
||||
}
|
||||
}
|
||||
@ -2,19 +2,24 @@
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
|
||||
class Wifi {
|
||||
namespace Wifi {
|
||||
|
||||
WiFiEventHandler stationConnectedHandler;
|
||||
|
||||
private:
|
||||
String currentSSID;
|
||||
String currentPsk;
|
||||
|
||||
public:
|
||||
void setup() {
|
||||
stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& e) {
|
||||
Serial.println("Reconnected to network.");
|
||||
wifiConnected.signal();
|
||||
});
|
||||
|
||||
WiFi.hostname("esp-clock");
|
||||
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
wifiMulti.addAP("OpenWrt", "***REMOVED***");
|
||||
// wifiMulti.addAP("IoT", "***REMOVED***");
|
||||
wifiMulti.addAP("Miracle", "***REMOVED***");
|
||||
WiFi.hostname("esp-clock");
|
||||
|
||||
Serial.println("Connecting to WiFi netowrk.");
|
||||
while (wifiMulti.run() != WL_CONNECTED) {
|
||||
@ -30,9 +35,6 @@ public:
|
||||
WiFi.forceSleepWake();
|
||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||
Serial.println("Reconnecting to WiFi netowrk...");
|
||||
for (int i = 0; i < 4; i++) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,4 +60,4 @@ public:
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
} wifi;
|
||||
}
|
||||
|
||||
@ -1,51 +1,67 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define _TASK_STATUS_REQUEST
|
||||
#include <TaskScheduler.h>
|
||||
Scheduler ts;
|
||||
StatusRequest hourChanged;
|
||||
StatusRequest dayChanged;
|
||||
StatusRequest wifiConnected;
|
||||
|
||||
#include "wifi.h"
|
||||
#include "ota.h"
|
||||
#include "ntp_time.h"
|
||||
#include "ota.h"
|
||||
#include "display.h"
|
||||
#include "ir.h"
|
||||
|
||||
#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() {
|
||||
Display::setup();
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
wifi.setup();
|
||||
Display::setup();
|
||||
|
||||
Ota::setup();
|
||||
|
||||
Ntp::setup();
|
||||
Display::adjustBrightness();
|
||||
|
||||
Ir::setup();
|
||||
|
||||
hourChanged.setWaiting();
|
||||
dayChanged.setWaiting();
|
||||
wifiConnected.setWaiting();
|
||||
tWifiReconnect.waitFor(&dayChanged);
|
||||
tWifiConnected.waitFor(&wifiConnected);
|
||||
|
||||
Wifi::setup();
|
||||
}
|
||||
|
||||
void 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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user