Files
smart-rgb-esp32/src/main.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

2025-07-29 14:58:45 +02:00
#include <Arduino.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-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-03 00:34:05 +02:00
Task *updateTask = nullptr;
Task *mqttTickTask = nullptr;
Task *mqttCheckConnectionTask = nullptr;
2025-08-27 10:40:39 +02:00
Pin *pinR = new Pin(16, true, true, 5000, 0); // Example pin numbers, adjust as needed
Pin *pinG = new Pin(17, true, true, 5000, 1);
Pin *pinB = new Pin(18, true, true, 5000, 2);
Pin *pinCW = new Pin(19, true, true, 5000, 3);
Pin *pinWW = new Pin(21, true, true, 5000, 4);
2025-08-03 00:34:05 +02:00
Scheduler *scheduler;
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:
Serial.begin(115200);
Serial.println("Starting Smart RGB ESP32...");
2025-08-27 10:40:39 +02:00
pinR->setLedLevel(0);
pinG->setLedLevel(0);
pinB->setLedLevel(0);
pinCW->setLedLevel(0);
pinWW->setLedLevel(0);
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
}