Compare commits
1 Commits
eeb317c108
...
bugfix/lig
| Author | SHA1 | Date | |
|---|---|---|---|
| 214f4647a0 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
|||||||
[submodule "lib/state-machine"]
|
|
||||||
path = lib/state-machine
|
|
||||||
url = https://github.com/t-liu93/state-machine
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
inline constexpr uint8_t ledPinR = 16;
|
|
||||||
inline constexpr uint8_t ledPinG = 17;
|
|
||||||
inline constexpr uint8_t ledPinB = 18;
|
|
||||||
inline constexpr uint8_t ledPinCW = 19;
|
|
||||||
inline constexpr uint8_t ledPinWW = 21;
|
|
||||||
|
|
||||||
inline constexpr std::string_view hostName = "smart-rgb-dev";
|
|
||||||
inline constexpr std::string_view friendlyName = "Smart RGB Dev";
|
|
||||||
inline constexpr uint32_t maxNumberOfStates = 10;
|
|
||||||
|
|
||||||
inline constexpr std::string_view mqttBroker = "10.238.75.81";
|
|
||||||
@@ -14,8 +14,8 @@ const struct {
|
|||||||
} Availability;
|
} Availability;
|
||||||
|
|
||||||
|
|
||||||
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId)
|
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId)
|
||||||
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), mqttClient(mqttClient) {
|
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr) {
|
||||||
lightInfo.uniqueId = uniqueId;
|
lightInfo.uniqueId = uniqueId;
|
||||||
lightType = LightType::rgb;
|
lightType = LightType::rgb;
|
||||||
uint8_t bits = pinR->getLedResolutionBits();
|
uint8_t bits = pinR->getLedResolutionBits();
|
||||||
@@ -24,8 +24,8 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniq
|
|||||||
subscribeToMqttTopics();
|
subscribeToMqttTopics();
|
||||||
}
|
}
|
||||||
|
|
||||||
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId)
|
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId)
|
||||||
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) {
|
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW) {
|
||||||
lightInfo.uniqueId = uniqueId;
|
lightInfo.uniqueId = uniqueId;
|
||||||
lightType = LightType::rgbww;
|
lightType = LightType::rgbww;
|
||||||
uint8_t bits = pinR->getLedResolutionBits();
|
uint8_t bits = pinR->getLedResolutionBits();
|
||||||
@@ -76,7 +76,7 @@ void Light::publishInitialState() {
|
|||||||
|
|
||||||
std::string configJson;
|
std::string configJson;
|
||||||
serializeJson(configInfo, configJson);
|
serializeJson(configInfo, configJson);
|
||||||
mqttClient->publish(lightInfo.discoveryTopic, configJson);
|
Mqtt::publish(lightInfo.discoveryTopic, configJson);
|
||||||
|
|
||||||
std::string stateJson;
|
std::string stateJson;
|
||||||
JsonDocument stateInfo;
|
JsonDocument stateInfo;
|
||||||
@@ -95,8 +95,8 @@ void Light::publishInitialState() {
|
|||||||
JsonDocument availabilityInfoDoc;
|
JsonDocument availabilityInfoDoc;
|
||||||
availabilityInfoDoc["availability"] = Availability.available;
|
availabilityInfoDoc["availability"] = Availability.available;
|
||||||
serializeJson(availabilityInfoDoc, availabilityJson);
|
serializeJson(availabilityInfoDoc, availabilityJson);
|
||||||
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
Mqtt::publish(lightInfo.stateTopic, stateJson);
|
||||||
mqttClient->publish(lightInfo.availabilityTopic, availabilityJson);
|
Mqtt::publish(lightInfo.availabilityTopic, availabilityJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::operatePin() {
|
void Light::operatePin() {
|
||||||
@@ -159,7 +159,7 @@ void Light::operatePin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Light::subscribeToMqttTopics() {
|
void Light::subscribeToMqttTopics() {
|
||||||
mqttClient->subscribe(lightInfo.commandTopic, [this](uint8_t* payload, int length) {
|
Mqtt::subscribe(lightInfo.commandTopic, [this](uint8_t* payload, int length) {
|
||||||
std::string command(reinterpret_cast<char*>(payload), length);
|
std::string command(reinterpret_cast<char*>(payload), length);
|
||||||
handleCommand(command);
|
handleCommand(command);
|
||||||
});
|
});
|
||||||
@@ -241,11 +241,11 @@ void Light::publishCurrentState() {
|
|||||||
std::string stateJson;
|
std::string stateJson;
|
||||||
serializeJson(stateInfo, stateJson);
|
serializeJson(stateInfo, stateJson);
|
||||||
Serial.println("Publishing current state: " + String(stateJson.c_str()));
|
Serial.println("Publishing current state: " + String(stateJson.c_str()));
|
||||||
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
Mqtt::publish(lightInfo.stateTopic, stateJson);
|
||||||
std::string attributeJson;
|
std::string attributeJson;
|
||||||
serializeJson(attributeInfo, attributeJson);
|
serializeJson(attributeInfo, attributeJson);
|
||||||
Serial.println("Publishing current attributes: " + String(attributeJson.c_str()));
|
Serial.println("Publishing current attributes: " + String(attributeJson.c_str()));
|
||||||
mqttClient->publish(lightInfo.jsonAttributesTopic, attributeJson);
|
Mqtt::publish(lightInfo.jsonAttributesTopic, attributeJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Light::correctGamma(uint32_t originalPwm) {
|
uint32_t Light::correctGamma(uint32_t originalPwm) {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "mqtt.h"
|
|
||||||
|
|
||||||
struct LightInfo {
|
struct LightInfo {
|
||||||
std::string uniqueId;
|
std::string uniqueId;
|
||||||
@@ -65,9 +64,9 @@ enum ActiveMode {
|
|||||||
|
|
||||||
class Light {
|
class Light {
|
||||||
public:
|
public:
|
||||||
Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId);
|
Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId);
|
||||||
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Mqtt* mqttClient, std::string uniqueId);
|
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, std::string uniqueId);
|
||||||
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId);
|
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId);
|
||||||
void subscribeToMqttTopics();
|
void subscribeToMqttTopics();
|
||||||
void publishInitialState();
|
void publishInitialState();
|
||||||
void publishCurrentState();
|
void publishCurrentState();
|
||||||
@@ -97,7 +96,6 @@ private:
|
|||||||
Pin* pinB;
|
Pin* pinB;
|
||||||
Pin* pinCW;
|
Pin* pinCW;
|
||||||
Pin* pinWW;
|
Pin* pinWW;
|
||||||
Mqtt* mqttClient;
|
|
||||||
LightInfo lightInfo;
|
LightInfo lightInfo;
|
||||||
DeviceInfo deviceInfo;
|
DeviceInfo deviceInfo;
|
||||||
LightType lightType = onOff; // Default light type
|
LightType lightType = onOff; // Default light type
|
||||||
|
|||||||
@@ -5,31 +5,18 @@
|
|||||||
WIFIMANAGER Network::WifiManager;
|
WIFIMANAGER Network::WifiManager;
|
||||||
AsyncWebServer Network::webServer(80);
|
AsyncWebServer Network::webServer(80);
|
||||||
|
|
||||||
Network::Network(std::string_view hostname, std::string_view apSsid) {
|
Network::Network(std::string hostname, std::string apSsid) : hostname(hostname), apSsid(apSsid) {
|
||||||
this->hostname = std::string(hostname);
|
|
||||||
this->apSsid = std::string(apSsid);
|
|
||||||
setHostname(hostname);
|
setHostname(hostname);
|
||||||
WifiManager.startBackgroundTask(apSsid.data(), "");
|
WifiManager.startBackgroundTask(apSsid.c_str(), "");
|
||||||
WifiManager.fallbackToSoftAp(true);
|
WifiManager.fallbackToSoftAp(true);
|
||||||
WifiManager.attachWebServer(&webServer);
|
WifiManager.attachWebServer(&webServer);
|
||||||
WifiManager.attachUI();
|
WifiManager.attachUI();
|
||||||
webServer.on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
|
webServer.on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
|
||||||
request->send(200, "text/html", this->defaultHomepage.data());
|
request->send(200, "text/html", this->defaultHomepage.c_str());
|
||||||
});
|
});
|
||||||
webServer.begin();
|
webServer.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
Network::~Network() {
|
|
||||||
webServer.end();
|
|
||||||
WifiManager.detachUI();
|
|
||||||
WifiManager.detachWebServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Network::reconnect() {
|
|
||||||
WifiManager.stopWifi(true);
|
|
||||||
WifiManager.startBackgroundTask(apSsid.data(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Network::isConnected() const {
|
bool Network::isConnected() const {
|
||||||
return WiFi.status() == WL_CONNECTED;
|
return WiFi.status() == WL_CONNECTED;
|
||||||
}
|
}
|
||||||
@@ -38,15 +25,14 @@ std::string Network::getHostname() const {
|
|||||||
return WiFi.getHostname();
|
return WiFi.getHostname();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Network::setHostname(const std::string_view &hostname) {
|
bool Network::setHostname(const std::string &hostname) {
|
||||||
this->hostname = std::string(hostname);
|
return WiFi.setHostname(hostname.c_str());
|
||||||
return WiFi.setHostname(this->hostname.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::registerMDNS() {
|
void Network::registerMDNS() {
|
||||||
if (!MDNS.begin(this->hostname.c_str())) {
|
if (!MDNS.begin(hostname.c_str())) {
|
||||||
Serial.println("Error setting up MDNS responder!");
|
Serial.println("Error setting up MDNS responder!");
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("mDNS responder started with hostname: %s\n", this->hostname.c_str());
|
Serial.printf("mDNS responder started with hostname: %s\n", hostname.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <string_view>
|
|
||||||
#include "wifimanager.h"
|
#include "wifimanager.h"
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
public:
|
public:
|
||||||
Network(std::string_view hostname, std::string_view apSsid = "Smart RGB");
|
Network(std::string hostname, std::string apSsid = "Smart RGB");
|
||||||
~Network();
|
|
||||||
void reconnect();
|
|
||||||
bool isConnected() const;
|
bool isConnected() const;
|
||||||
std::string getHostname() const;
|
std::string getHostname() const;
|
||||||
bool setHostname(const std::string_view &hostname);
|
bool setHostname(const std::string &hostname);
|
||||||
void registerMDNS();
|
void registerMDNS();
|
||||||
private:
|
private:
|
||||||
static WIFIMANAGER WifiManager;
|
static WIFIMANAGER WifiManager;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
|
||||||
OTAHandler::OTAHandler(std::string_view hostname) {
|
OTAHandler::OTAHandler(std::string hostname) {
|
||||||
ArduinoOTA.setHostname(hostname.data());
|
ArduinoOTA.setHostname(hostname.c_str());
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("OTA Start");
|
Serial.println("OTA Start");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string_view>
|
#include <string>
|
||||||
|
|
||||||
class OTAHandler {
|
class OTAHandler {
|
||||||
public:
|
public:
|
||||||
OTAHandler(std::string_view hostname);
|
OTAHandler(std::string hostname);
|
||||||
|
|
||||||
void poll();
|
void poll();
|
||||||
};
|
};
|
||||||
Submodule lib/state-machine deleted from fc7d0d9706
@@ -13,10 +13,6 @@ platform = espressif32
|
|||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
build_unflags = -std=gnu++11
|
|
||||||
build_flags =
|
|
||||||
-std=c++17
|
|
||||||
-std=gnu++17
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
martinverges/ESP32 Wifi Manager@^1.5.0
|
martinverges/ESP32 Wifi Manager@^1.5.0
|
||||||
esp32async/ESPAsyncWebServer@^3.7.10
|
esp32async/ESPAsyncWebServer@^3.7.10
|
||||||
@@ -28,4 +24,4 @@ lib_deps =
|
|||||||
|
|
||||||
[env:esp32dev-ota]
|
[env:esp32dev-ota]
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = smart-rgb-dev.local
|
upload_port = smart-rgb.local
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class Pin;
|
|
||||||
template <uint32_t MAX_NUMBER_OF_STATES, uint32_t MAX_NUMBER_OF_TRANSITIONS>
|
|
||||||
class StateMachine;
|
|
||||||
class OTAHandler;
|
|
||||||
class Mqtt;
|
|
||||||
class Light;
|
|
||||||
class Network;
|
|
||||||
class Scheduler;
|
|
||||||
|
|
||||||
struct AppContext {
|
|
||||||
Pin *pinR = nullptr;
|
|
||||||
Pin *pinG = nullptr;
|
|
||||||
Pin *pinB = nullptr;
|
|
||||||
Pin *pinCW = nullptr;
|
|
||||||
Pin *pinWW = nullptr;
|
|
||||||
|
|
||||||
Network *network = nullptr;
|
|
||||||
Light *light = nullptr;
|
|
||||||
Mqtt *mqtt = nullptr;
|
|
||||||
OTAHandler *otaHandler = nullptr;
|
|
||||||
Scheduler *scheduler = nullptr;
|
|
||||||
};
|
|
||||||
84
src/main.cpp
84
src/main.cpp
@@ -1,42 +1,28 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "appcontext.hpp"
|
|
||||||
#include "config.h"
|
|
||||||
#include "light.h"
|
#include "light.h"
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "statemachine.hpp"
|
|
||||||
#include "states.hpp"
|
|
||||||
#include "TaskScheduler.h"
|
#include "TaskScheduler.h"
|
||||||
#include "wifimanager.h"
|
#include "wifimanager.h"
|
||||||
|
|
||||||
// Network* network = nullptr;
|
Network* network = nullptr;
|
||||||
// OTAHandler* otaHandler = nullptr;
|
OTAHandler* otaHandler = nullptr;
|
||||||
// Mqtt* mqttClient = nullptr;
|
Mqtt* mqttClient = nullptr;
|
||||||
// Light *light = nullptr;
|
Light *light = nullptr;
|
||||||
|
|
||||||
|
Task *updateTask = nullptr;
|
||||||
|
Task *mqttTickTask = nullptr;
|
||||||
|
Task *mqttCheckConnectionTask = nullptr;
|
||||||
|
|
||||||
// Task *updateTask = nullptr;
|
Pin *pinR = new Pin(16, true, true, 5000, 0); // Example pin numbers, adjust as needed
|
||||||
// Task *mqttTickTask = nullptr;
|
Pin *pinG = new Pin(17, true, true, 5000, 1);
|
||||||
// Task *mqttCheckConnectionTask = nullptr;
|
Pin *pinB = new Pin(18, true, true, 5000, 2);
|
||||||
Task *appStateMachineUpdateTask = nullptr;
|
Pin *pinCW = new Pin(19, true, true, 5000, 3);
|
||||||
|
Pin *pinWW = new Pin(21, true, true, 5000, 4);
|
||||||
|
|
||||||
Pin *pinR = new Pin(ledPinR, true, true, 5000, 0);
|
Scheduler *scheduler;
|
||||||
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);
|
|
||||||
NetworkInitializeState *networkInitializeState = new NetworkInitializeState(appContext);
|
|
||||||
RunningState *runningState = new RunningState(appContext);
|
|
||||||
|
|
||||||
|
|
||||||
StateMachine<maxNumberOfStates> *stateMachine = nullptr;
|
|
||||||
|
|
||||||
Scheduler *scheduler = nullptr;
|
|
||||||
|
|
||||||
void initializeScheduler();
|
void initializeScheduler();
|
||||||
|
|
||||||
@@ -44,21 +30,18 @@ void setup() {
|
|||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Starting Smart RGB ESP32...");
|
Serial.println("Starting Smart RGB ESP32...");
|
||||||
stateMachine = new StateMachine<maxNumberOfStates>();
|
pinR->setLedLevel(0);
|
||||||
|
pinG->setLedLevel(0);
|
||||||
|
pinB->setLedLevel(0);
|
||||||
|
pinCW->setLedLevel(0);
|
||||||
|
pinWW->setLedLevel(0);
|
||||||
|
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, "smart_rgb_light");
|
||||||
initializeScheduler();
|
initializeScheduler();
|
||||||
appContext->pinR = pinR;
|
|
||||||
appContext->pinG = pinG;
|
|
||||||
appContext->pinB = pinB;
|
|
||||||
appContext->pinCW = pinCW;
|
|
||||||
appContext->pinWW = pinWW;
|
|
||||||
stateMachine->addStateRaw(startState);
|
|
||||||
stateMachine->addStateRaw(networkInitializeState);
|
|
||||||
stateMachine->addTransition(StateId::StartState, EventId::PinInitialized, StateId::NetworkInitializeState);
|
|
||||||
stateMachine->addStateRaw(runningState);
|
|
||||||
stateMachine->addTransition(StateId::NetworkInitializeState, EventId::WifiConnected, StateId::RunningState);
|
|
||||||
stateMachine->addTransition(StateId::RunningState, EventId::WifiDisconnected, StateId::NetworkInitializeState);
|
|
||||||
stateMachine->setInitialState(StateId::StartState);
|
|
||||||
// light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -68,15 +51,14 @@ void loop() {
|
|||||||
|
|
||||||
void initializeScheduler() {
|
void initializeScheduler() {
|
||||||
scheduler = new Scheduler();
|
scheduler = new Scheduler();
|
||||||
appStateMachineUpdateTask = new Task(TASK_MILLISECOND, TASK_FOREVER, []() {
|
updateTask = new Task(TASK_SECOND, TASK_FOREVER, []() {
|
||||||
if (stateMachine) {
|
otaHandler->poll(); // Poll for OTA updates
|
||||||
stateMachine->update();
|
|
||||||
}
|
}, 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);
|
}, 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);
|
|
||||||
}
|
}
|
||||||
130
src/states.hpp
130
src/states.hpp
@@ -1,130 +0,0 @@
|
|||||||
#include <Arduino.h>
|
|
||||||
#include "appcontext.hpp"
|
|
||||||
#include "config.h"
|
|
||||||
#include "mqtt.h"
|
|
||||||
#include "pin.h"
|
|
||||||
#include "statemachine.hpp"
|
|
||||||
|
|
||||||
enum class StateId
|
|
||||||
{
|
|
||||||
StartState,
|
|
||||||
NetworkInitializeState,
|
|
||||||
RunningState
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class EventId
|
|
||||||
{
|
|
||||||
PinInitialized,
|
|
||||||
WifiConnected,
|
|
||||||
WifiDisconnected
|
|
||||||
};
|
|
||||||
|
|
||||||
class StartState : public State
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
StartState(AppContext *appContext) : State("StartState", StateId::StartState), appContext(appContext) {}
|
|
||||||
|
|
||||||
void onEnter(StateMachineBase &stateMachine) override {
|
|
||||||
if (appContext) {
|
|
||||||
appContext->pinR->setLedLevel(0);
|
|
||||||
appContext->pinG->setLedLevel(0);
|
|
||||||
appContext->pinB->setLedLevel(0);
|
|
||||||
appContext->pinCW->setLedLevel(0);
|
|
||||||
appContext->pinWW->setLedLevel(0);
|
|
||||||
}
|
|
||||||
stateMachine.postEvent(EventId::PinInitialized);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onExit(StateMachineBase &stateMachine) override {
|
|
||||||
}
|
|
||||||
|
|
||||||
void onUpdate(StateMachineBase &stateMachine) override {
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
AppContext *appContext = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NetworkInitializeState : public State
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {}
|
|
||||||
|
|
||||||
void onEnter(StateMachineBase &stateMachine) override {
|
|
||||||
Serial.println("Entering NetworkInitializeState");
|
|
||||||
if (appContext && !appContext->network) {
|
|
||||||
appContext->network = new Network(hostName, friendlyName);
|
|
||||||
} else if (appContext && appContext->network) {
|
|
||||||
appContext->network->reconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void onExit(StateMachineBase &stateMachine) override {
|
|
||||||
if (appContext && appContext->network) {
|
|
||||||
appContext->network->registerMDNS();
|
|
||||||
if (!appContext->otaHandler) {
|
|
||||||
appContext->otaHandler = new OTAHandler(hostName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Mqtt::connect(mqttBroker.data(), 1883, hostName.data(), "mqtt", "mqtt");
|
|
||||||
}
|
|
||||||
|
|
||||||
void onUpdate(StateMachineBase &stateMachine) override {
|
|
||||||
if (appContext && appContext->network && appContext->network->isConnected()) {
|
|
||||||
stateMachine.postEvent(EventId::WifiConnected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
AppContext *appContext = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class RunningState : public State
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
RunningState(AppContext *appContext) : State("RunningState", StateId::RunningState), appContext(appContext) {}
|
|
||||||
void onEnter(StateMachineBase &stateMachine) override {
|
|
||||||
lastOtaPollMs = millis();
|
|
||||||
lastMqttPollMs = millis();
|
|
||||||
lastMqttCheckConnectionPollSecond = millis() / 1000;
|
|
||||||
lastNetworkCheckPollMs = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
void onExit(StateMachineBase &stateMachine) override {
|
|
||||||
Serial.println("Exiting RunningState");
|
|
||||||
}
|
|
||||||
|
|
||||||
void onUpdate(StateMachineBase &stateMachine) override {
|
|
||||||
if ((millis() - lastOtaPollMs) >= otaPollInterval) {
|
|
||||||
lastOtaPollMs = millis();
|
|
||||||
if (appContext && appContext->otaHandler) {
|
|
||||||
appContext->otaHandler->poll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((millis() - lastMqttPollMs) >= mqttPollInterval) {
|
|
||||||
lastMqttPollMs = millis();
|
|
||||||
Mqtt::poll();
|
|
||||||
}
|
|
||||||
if ((millis() - lastMqttCheckConnectionPollSecond) >= mqttCheckConnectionPollIntervalSecond * 1000) {
|
|
||||||
lastMqttCheckConnectionPollSecond = millis() / 1000;
|
|
||||||
Mqtt::checkConnection();
|
|
||||||
}
|
|
||||||
if ((millis() - lastNetworkCheckPollMs) >= networkCheckPollInterval) {
|
|
||||||
lastNetworkCheckPollMs = millis();
|
|
||||||
if (appContext && appContext->network && !appContext->network->isConnected()) {
|
|
||||||
stateMachine.postEvent(EventId::WifiDisconnected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
AppContext *appContext = nullptr;
|
|
||||||
uint32_t lastOtaPollMs = 0;
|
|
||||||
static constexpr uint32_t otaPollInterval = 1000; // Poll every second
|
|
||||||
uint32_t lastNetworkCheckPollMs = 0;
|
|
||||||
static constexpr uint32_t networkCheckPollInterval = 5000; // Poll every 5 seconds
|
|
||||||
uint32_t lastMqttPollMs = 0;
|
|
||||||
static constexpr uint32_t mqttPollInterval = 100; // Poll every 100 milliseconds
|
|
||||||
uint32_t lastMqttCheckConnectionPollSecond = 0;
|
|
||||||
static constexpr uint32_t mqttCheckConnectionPollIntervalSecond = 30; // Poll every 30 seconds
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user