some big change using state machine
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
inline constexpr uint8_t ledPinR = 16;
|
inline constexpr uint8_t ledPinR = 16;
|
||||||
inline constexpr uint8_t ledPinG = 17;
|
inline constexpr uint8_t ledPinG = 17;
|
||||||
@@ -7,5 +8,8 @@ inline constexpr uint8_t ledPinB = 18;
|
|||||||
inline constexpr uint8_t ledPinCW = 19;
|
inline constexpr uint8_t ledPinCW = 19;
|
||||||
inline constexpr uint8_t ledPinWW = 21;
|
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 uint32_t maxNumberOfStates = 10;
|
||||||
|
|
||||||
|
inline constexpr std::string_view mqttBroker = "10.238.75.81";
|
||||||
@@ -5,14 +5,14 @@
|
|||||||
WIFIMANAGER Network::WifiManager;
|
WIFIMANAGER Network::WifiManager;
|
||||||
AsyncWebServer Network::webServer(80);
|
AsyncWebServer Network::webServer(80);
|
||||||
|
|
||||||
Network::Network(std::string hostname, std::string apSsid) : hostname(hostname), apSsid(apSsid) {
|
Network::Network(std::string_view hostname, std::string_view apSsid) : hostname(hostname), apSsid(apSsid) {
|
||||||
setHostname(hostname);
|
setHostname(hostname);
|
||||||
WifiManager.startBackgroundTask(apSsid.c_str(), "");
|
WifiManager.startBackgroundTask(apSsid.data(), "");
|
||||||
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.c_str());
|
request->send(200, "text/html", this->defaultHomepage.data());
|
||||||
});
|
});
|
||||||
webServer.begin();
|
webServer.begin();
|
||||||
}
|
}
|
||||||
@@ -25,14 +25,14 @@ std::string Network::getHostname() const {
|
|||||||
return WiFi.getHostname();
|
return WiFi.getHostname();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Network::setHostname(const std::string &hostname) {
|
bool Network::setHostname(const std::string_view &hostname) {
|
||||||
return WiFi.setHostname(hostname.c_str());
|
return WiFi.setHostname(hostname.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::registerMDNS() {
|
void Network::registerMDNS() {
|
||||||
if (!MDNS.begin(hostname.c_str())) {
|
if (!MDNS.begin(hostname.data())) {
|
||||||
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", hostname.c_str());
|
Serial.printf("mDNS responder started with hostname: %s\n", hostname.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <string_view>
|
||||||
#include "wifimanager.h"
|
#include "wifimanager.h"
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
public:
|
public:
|
||||||
Network(std::string hostname, std::string apSsid = "Smart RGB");
|
Network(std::string_view hostname, std::string_view apSsid = "Smart RGB");
|
||||||
bool isConnected() const;
|
bool isConnected() const;
|
||||||
std::string getHostname() const;
|
std::string getHostname() const;
|
||||||
bool setHostname(const std::string &hostname);
|
bool setHostname(const std::string_view &hostname);
|
||||||
void registerMDNS();
|
void registerMDNS();
|
||||||
private:
|
private:
|
||||||
static WIFIMANAGER WifiManager;
|
static WIFIMANAGER WifiManager;
|
||||||
@@ -25,6 +26,6 @@ private:
|
|||||||
<p><small>ESP32 WiFi Manager (c) 2022-2025 by Martin Verges</small></p>
|
<p><small>ESP32 WiFi Manager (c) 2022-2025 by Martin Verges</small></p>
|
||||||
</body></html>
|
</body></html>
|
||||||
)html";
|
)html";
|
||||||
std::string hostname;
|
std::string_view hostname;
|
||||||
std::string apSsid; // SSID for the fallback AP
|
std::string_view apSsid; // SSID for the fallback AP
|
||||||
};
|
};
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
|
||||||
OTAHandler::OTAHandler(std::string hostname) {
|
OTAHandler::OTAHandler(std::string_view hostname) {
|
||||||
ArduinoOTA.setHostname(hostname.c_str());
|
ArduinoOTA.setHostname(hostname.data());
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("OTA Start");
|
Serial.println("OTA Start");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string_view>
|
||||||
|
|
||||||
class OTAHandler {
|
class OTAHandler {
|
||||||
public:
|
public:
|
||||||
OTAHandler(std::string hostname);
|
OTAHandler(std::string_view hostname);
|
||||||
|
|
||||||
void poll();
|
void poll();
|
||||||
};
|
};
|
||||||
@@ -28,4 +28,4 @@ lib_deps =
|
|||||||
|
|
||||||
[env:esp32dev-ota]
|
[env:esp32dev-ota]
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = smart-rgb.local
|
upload_port = smart-rgb-dev.local
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ struct AppContext {
|
|||||||
Network *network = nullptr;
|
Network *network = nullptr;
|
||||||
Light *light = nullptr;
|
Light *light = nullptr;
|
||||||
Mqtt *mqtt = nullptr;
|
Mqtt *mqtt = nullptr;
|
||||||
OTAHandler *ota = nullptr;
|
OTAHandler *otaHandler = nullptr;
|
||||||
Scheduler *scheduler = nullptr;
|
Scheduler *scheduler = nullptr;
|
||||||
};
|
};
|
||||||
59
src/main.cpp
59
src/main.cpp
@@ -11,15 +11,16 @@
|
|||||||
#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 *updateTask = nullptr;
|
||||||
Task *mqttTickTask = nullptr;
|
// Task *mqttTickTask = nullptr;
|
||||||
Task *mqttCheckConnectionTask = nullptr;
|
// Task *mqttCheckConnectionTask = nullptr;
|
||||||
|
Task *appStateMachineUpdateTask = nullptr;
|
||||||
|
|
||||||
Pin *pinR = new Pin(ledPinR, true, true, 5000, 0);
|
Pin *pinR = new Pin(ledPinR, true, true, 5000, 0);
|
||||||
Pin *pinG = new Pin(ledPinG, true, true, 5000, 1);
|
Pin *pinG = new Pin(ledPinG, true, true, 5000, 1);
|
||||||
@@ -29,6 +30,9 @@ Pin *pinWW = new Pin(ledPinWW, true, true, 5000, 4);
|
|||||||
|
|
||||||
AppContext *appContext = new AppContext();
|
AppContext *appContext = new AppContext();
|
||||||
StartState *startState = new StartState(appContext);
|
StartState *startState = new StartState(appContext);
|
||||||
|
NetworkInitializeState *networkInitializeState = new NetworkInitializeState(appContext);
|
||||||
|
RunningState *runningState = new RunningState(appContext);
|
||||||
|
|
||||||
|
|
||||||
StateMachine<maxNumberOfStates> *stateMachine = nullptr;
|
StateMachine<maxNumberOfStates> *stateMachine = nullptr;
|
||||||
|
|
||||||
@@ -38,8 +42,9 @@ void initializeScheduler();
|
|||||||
|
|
||||||
void setup() {
|
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...");
|
||||||
|
initializeScheduler();
|
||||||
stateMachine = new StateMachine<maxNumberOfStates>();
|
stateMachine = new StateMachine<maxNumberOfStates>();
|
||||||
appContext->pinR = pinR;
|
appContext->pinR = pinR;
|
||||||
appContext->pinG = pinG;
|
appContext->pinG = pinG;
|
||||||
@@ -47,14 +52,15 @@ void setup() {
|
|||||||
appContext->pinCW = pinCW;
|
appContext->pinCW = pinCW;
|
||||||
appContext->pinWW = pinWW;
|
appContext->pinWW = pinWW;
|
||||||
stateMachine->addStateRaw(startState);
|
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->setInitialState(StateId::StartState);
|
stateMachine->setInitialState(StateId::StartState);
|
||||||
network = new Network("smart-rgb");
|
|
||||||
otaHandler = new OTAHandler("smart-rgb-ota");
|
// Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
||||||
network->registerMDNS();
|
// delay(1000); // Wait for MQTT connection to stabilize
|
||||||
Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
// light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
||||||
delay(1000); // Wait for MQTT connection to stabilize
|
|
||||||
light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
|
||||||
initializeScheduler();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -64,14 +70,15 @@ void loop() {
|
|||||||
|
|
||||||
void initializeScheduler() {
|
void initializeScheduler() {
|
||||||
scheduler = new Scheduler();
|
scheduler = new Scheduler();
|
||||||
updateTask = new Task(TASK_SECOND, TASK_FOREVER, []() {
|
appStateMachineUpdateTask = new Task(TASK_MILLISECOND, TASK_FOREVER, []() {
|
||||||
otaHandler->poll(); // Poll for OTA updates
|
if (stateMachine) {
|
||||||
|
stateMachine->update();
|
||||||
}, scheduler, true, nullptr, nullptr);
|
}
|
||||||
mqttTickTask = new Task(TASK_MILLISECOND * 100, TASK_FOREVER, []() {
|
}, scheduler, true, nullptr, nullptr);
|
||||||
Mqtt::poll(); // Poll MQTT client for messages
|
// mqttTickTask = new Task(TASK_MILLISECOND * 100, TASK_FOREVER, []() {
|
||||||
}, scheduler, true, nullptr, nullptr);
|
// Mqtt::poll(); // Poll MQTT client for messages
|
||||||
mqttCheckConnectionTask = new Task(TASK_SECOND * 30, TASK_FOREVER, []() {
|
// }, scheduler, true, nullptr, nullptr);
|
||||||
Mqtt::checkConnection(); // Check MQTT connection status
|
// mqttCheckConnectionTask = new Task(TASK_SECOND * 30, TASK_FOREVER, []() {
|
||||||
}, scheduler, true, nullptr, nullptr);
|
// Mqtt::checkConnection(); // Check MQTT connection status
|
||||||
|
// }, scheduler, true, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,21 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
#include "appcontext.hpp"
|
#include "appcontext.hpp"
|
||||||
|
#include "config.h"
|
||||||
|
#include "mqtt.h"
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "statemachine.hpp"
|
#include "statemachine.hpp"
|
||||||
|
|
||||||
enum class StateId
|
enum class StateId
|
||||||
{
|
{
|
||||||
StartState,
|
StartState,
|
||||||
NetworkInitializeState
|
NetworkInitializeState,
|
||||||
|
RunningState
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class EventId
|
||||||
|
{
|
||||||
|
PinInitialized,
|
||||||
|
WifiConnected
|
||||||
};
|
};
|
||||||
|
|
||||||
class StartState : public State
|
class StartState : public State
|
||||||
@@ -21,6 +31,7 @@ public:
|
|||||||
appContext->pinCW->setLedLevel(0);
|
appContext->pinCW->setLedLevel(0);
|
||||||
appContext->pinWW->setLedLevel(0);
|
appContext->pinWW->setLedLevel(0);
|
||||||
}
|
}
|
||||||
|
stateMachine.postEvent(EventId::PinInitialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onExit(StateMachineBase &stateMachine) override {
|
void onExit(StateMachineBase &stateMachine) override {
|
||||||
@@ -36,14 +47,68 @@ private:
|
|||||||
class NetworkInitializeState : public State
|
class NetworkInitializeState : public State
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NetworkInitializeState() : State("NetworkInitializeState", StateId::NetworkInitializeState) {}
|
NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {}
|
||||||
|
|
||||||
void onEnter(StateMachineBase &stateMachine) override {
|
void onEnter(StateMachineBase &stateMachine) override {
|
||||||
|
if (appContext) {
|
||||||
|
appContext->network = new Network(hostName, friendlyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onExit(StateMachineBase &stateMachine) override {
|
||||||
|
if (appContext && appContext->network) {
|
||||||
|
appContext->network->registerMDNS();
|
||||||
|
appContext->otaHandler = new OTAHandler(hostName);
|
||||||
|
}
|
||||||
|
Mqtt::connect(mqttBroker.data(), 1883, "smart_rgb_dev", "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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onExit(StateMachineBase &stateMachine) override {
|
void onExit(StateMachineBase &stateMachine) override {
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUpdate(StateMachineBase &stateMachine) override {
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
AppContext *appContext = nullptr;
|
||||||
|
uint32_t lastOtaPollMs = 0;
|
||||||
|
static constexpr uint32_t otaPollInterval = 1000; // Poll every second
|
||||||
|
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