#include void checkWifiCallback(); void onButtonPressed(); void onLed(); void onButtonCallback(); #define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_PORT 1883 #define MAIN_DEVICE_ID "esp-clock" #include Scheduler ts; Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts); void turnLed(bool on = true) { on ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); } #include "bme.h" #include "ntp_time.h" #include "wifi.h" #include "devices.h" #include "mqtt.h" #include "ota.h" #include "webserver.h" #define BUTTON D3 Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() { if (Display::displayTask.activate(Display::tDisplaySensor)) Bme::data.readAll(); }, &ts); Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { Devices::ledMqtt->updateState(!digitalRead(LED_BUILTIN)); }, &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; Devices::publishBme280(); if (abs(lastTemp - Bme::data.temp) > 0.2) { lastTemp = Bme::data.temp; Display::tDisplaySensor.setIterations((DISPLAY_SENSOR_ITERATIONS - 1) * 2 + 1); Display::displayTask.activate(Display::tDisplaySensor); } }, &ts); void setup() { Serial.begin(9600); Serial.println(); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); Display::setup(); Ota::setup( [] { Display::displayText("load"); Mqtt::publishCleanupConfig(); delay(2000); Mqtt::disconnect(); WebServer::stop(); }); Ntp::setup(); Bme::setup(); Mqtt::setup(&ts, [] {turnLed(false);}, [] {turnLed();} ); Devices::setup(); WebServer::setup(); Wifi::setup(ts, [] { Ota::tLoop.enable(); Mqtt::tReConnect.enable(); Ntp::tUpdateTime.enable(); WebServer::start(); }, [] { Ota::tLoop.disable(); Mqtt::tReConnect.disable(); WebServer::stop(); }); pinMode(BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); attachInterrupt(digitalPinToInterrupt(LED_BUILTIN), onLed, CHANGE); tReadBme.enableDelayed(); tButton.restart(); } void loop() { ts.execute(); } 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(); }