esp-clock/src/esp_clock.cpp

96 lines
2.0 KiB
C++

#include <Arduino.h>
void checkWifiCallback();
void onWifiConnected();
void onButtonPressed();
void onLed();
void onButtonCallback();
#include <TaskScheduler.h>
Scheduler ts;
Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts);
Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
#include "wifi.h"
#include "mqtt.h"
#include "display.h"
#include "bme.h"
#include "ntp_time.h"
#include "ota.h"
#define BUTTON D3
Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() {
if (Display::tDisplaySensor.isEnabled()) return;
Bme::data.readAll();
Display::tDisplaySensor.restart();
}, &ts);
Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() {
Mqtt::ledMqtt->publishState();
}, &ts);
Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() {
static float lastTemp = 0;
float temp = Bme::data.temp;
float humidity = Bme::data.humidity;
Bme::data.readAll();
if ((temp == Bme::data.temp) && (humidity == Bme::data.humidity)) return;
Mqtt::publishBme280();
if (abs(lastTemp - Bme::data.temp) > 0.2) {
lastTemp = Bme::data.temp;
Display::tDisplaySensor.restart();
}
}, &ts);
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Display::setup();
Ota::setup();
Ntp::setup();
Mqtt::setup();
Bme::setup();
Wifi::setup();
pinMode(BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING);
attachInterrupt(digitalPinToInterrupt(LED_BUILTIN), onLed, CHANGE);
tReadBme.enable();
}
void loop() {
ts.execute();
}
void onWifiConnected() {
Serial.println("Wifi connected event");
Wifi::printStatus();
Ota::tLoop.enable();
Mqtt::connect();
if (time_t newTime = Ntp::updateTime()) {
Serial.println(asctime(localtime(&newTime)));
}
}
void checkWifiCallback() {
#if !WIFI_ALWAYS_ON
Serial.println("Wifi connection timed out");
Mqtt::disconnect();
Ota::tLoop.disable();
Wifi::disconnect();
#endif
}
ICACHE_RAM_ATTR void onButtonPressed() {
tButton.restart();
}
ICACHE_RAM_ATTR void onLed() {
tLed.restart();
}