#include #include "appcontext.hpp" #include "config.h" #include "light.h" #include "mqtt.h" #include "network.h" #include "ota.h" #include "pin.h" #include "statemachine.hpp" #include "states.hpp" #include "TaskScheduler.h" #include "wifimanager.h" Network* network = nullptr; OTAHandler* otaHandler = nullptr; Mqtt* mqttClient = nullptr; Light *light = nullptr; Task *updateTask = nullptr; Task *mqttTickTask = nullptr; Task *mqttCheckConnectionTask = nullptr; Pin *pinR = new Pin(ledPinR, true, true, 5000, 0); Pin *pinG = new Pin(ledPinG, true, true, 5000, 1); Pin *pinB = new Pin(ledPinB, true, true, 5000, 2); Pin *pinCW = new Pin(ledPinCW, true, true, 5000, 3); Pin *pinWW = new Pin(ledPinWW, true, true, 5000, 4); AppContext *appContext = new AppContext(); StartState *startState = new StartState(appContext); StateMachine *stateMachine = nullptr; Scheduler *scheduler = nullptr; void initializeScheduler(); void setup() { // put your setup code here, to run once: // Serial.begin(115200); // Serial.println("Starting Smart RGB ESP32..."); stateMachine = new StateMachine(); appContext->pinR = pinR; appContext->pinG = pinG; appContext->pinB = pinB; appContext->pinCW = pinCW; appContext->pinWW = pinWW; stateMachine->addStateRaw(startState); stateMachine->setInitialState(StateId::StartState); network = new Network("smart-rgb"); otaHandler = new OTAHandler("smart-rgb-ota"); network->registerMDNS(); Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt"); delay(1000); // Wait for MQTT connection to stabilize light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light"); initializeScheduler(); } void loop() { scheduler->execute(); // Execute the scheduler to run tasks yield(); // Yield to allow other tasks to run } void initializeScheduler() { scheduler = new Scheduler(); updateTask = new Task(TASK_SECOND, TASK_FOREVER, []() { otaHandler->poll(); // Poll for OTA updates }, scheduler, true, nullptr, nullptr); mqttTickTask = new Task(TASK_MILLISECOND * 100, TASK_FOREVER, []() { Mqtt::poll(); // Poll MQTT client for messages }, scheduler, true, nullptr, nullptr); mqttCheckConnectionTask = new Task(TASK_SECOND * 30, TASK_FOREVER, []() { Mqtt::checkConnection(); // Check MQTT connection status }, scheduler, true, nullptr, nullptr); }