2025-07-29 14:58:45 +02:00
|
|
|
#include <Arduino.h>
|
2025-08-28 14:58:55 +02:00
|
|
|
#include "appcontext.hpp"
|
|
|
|
|
#include "config.h"
|
2025-08-03 00:34:05 +02:00
|
|
|
#include "light.h"
|
|
|
|
|
#include "mqtt.h"
|
2025-07-29 17:41:21 +02:00
|
|
|
#include "network.h"
|
|
|
|
|
#include "ota.h"
|
2025-08-03 00:34:05 +02:00
|
|
|
#include "pin.h"
|
2025-08-28 14:13:20 +02:00
|
|
|
#include "statemachine.hpp"
|
2025-08-28 14:58:55 +02:00
|
|
|
#include "states.hpp"
|
2025-08-03 00:34:05 +02:00
|
|
|
#include "TaskScheduler.h"
|
2025-07-29 17:41:21 +02:00
|
|
|
#include "wifimanager.h"
|
|
|
|
|
|
|
|
|
|
Network* network = nullptr;
|
|
|
|
|
OTAHandler* otaHandler = nullptr;
|
2025-08-03 00:34:05 +02:00
|
|
|
Mqtt* mqttClient = nullptr;
|
|
|
|
|
Light *light = nullptr;
|
2025-07-29 14:58:45 +02:00
|
|
|
|
2025-08-28 14:58:55 +02:00
|
|
|
|
2025-08-03 00:34:05 +02:00
|
|
|
Task *updateTask = nullptr;
|
|
|
|
|
Task *mqttTickTask = nullptr;
|
|
|
|
|
Task *mqttCheckConnectionTask = nullptr;
|
|
|
|
|
|
2025-08-28 14:58:55 +02:00
|
|
|
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<maxNumberOfStates> *stateMachine = nullptr;
|
2025-08-03 00:34:05 +02:00
|
|
|
|
2025-08-28 14:58:55 +02:00
|
|
|
Scheduler *scheduler = nullptr;
|
2025-08-03 00:34:05 +02:00
|
|
|
|
|
|
|
|
void initializeScheduler();
|
2025-07-29 14:58:45 +02:00
|
|
|
|
|
|
|
|
void setup() {
|
2025-07-29 17:41:21 +02:00
|
|
|
// put your setup code here, to run once:
|
2025-08-28 14:58:55 +02:00
|
|
|
// Serial.begin(115200);
|
|
|
|
|
// Serial.println("Starting Smart RGB ESP32...");
|
|
|
|
|
stateMachine = new StateMachine<maxNumberOfStates>();
|
|
|
|
|
appContext->pinR = pinR;
|
|
|
|
|
appContext->pinG = pinG;
|
|
|
|
|
appContext->pinB = pinB;
|
|
|
|
|
appContext->pinCW = pinCW;
|
|
|
|
|
appContext->pinWW = pinWW;
|
|
|
|
|
stateMachine->addStateRaw(startState);
|
|
|
|
|
stateMachine->setInitialState(StateId::StartState);
|
2025-07-29 17:41:21 +02:00
|
|
|
network = new Network("smart-rgb");
|
|
|
|
|
otaHandler = new OTAHandler("smart-rgb-ota");
|
|
|
|
|
network->registerMDNS();
|
2025-08-03 00:34:05 +02:00
|
|
|
Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
|
|
|
|
delay(1000); // Wait for MQTT connection to stabilize
|
2025-08-27 10:40:39 +02:00
|
|
|
light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
2025-08-03 00:34:05 +02:00
|
|
|
initializeScheduler();
|
2025-07-29 14:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
2025-08-03 00:34:05 +02:00
|
|
|
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);
|
2025-07-29 14:58:45 +02:00
|
|
|
}
|