From 71475ef2f15272a55416b3a10b885a75f9c86a23 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 29 Dec 2023 23:43:12 +0700 Subject: [PATCH] migrate mqtt config to struct --- Template Project/.vscode/settings.json | 44 +++++++++++++- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 57 +++---------------- .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 8 +-- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 10 +--- .../lib/ESPMegaPRO/InternalDisplay.cpp | 56 ++++++++++++++---- .../lib/ESPMegaPRO/InternalDisplay.hpp | 21 +++++-- .../lib/ESPMegaPRO/TimeStructure.hpp | 11 ++++ 7 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 Template Project/lib/ESPMegaPRO/TimeStructure.hpp diff --git a/Template Project/.vscode/settings.json b/Template Project/.vscode/settings.json index 9d43f7f..eade1b3 100644 --- a/Template Project/.vscode/settings.json +++ b/Template Project/.vscode/settings.json @@ -13,8 +13,48 @@ "*.tcc": "cpp", "memory": "cpp", "random": "cpp", - "functional": "cpp" + "functional": "cpp", + "atomic": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" }, "cmake.configureOnOpen": true, - "cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO" + "cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO", + "editor.tokenColorCustomizations": { + "comments": "", + "textMateRules": [] + } } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index 7b8be66..b535b17 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -17,10 +17,6 @@ ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) ESPMegaIoT::~ESPMegaIoT() { - // Delete the mqtt server - delete[] mqtt_server; - delete[] mqtt_user; - delete[] mqtt_password; } void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) @@ -248,13 +244,13 @@ void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *payload) bool ESPMegaIoT::mqttReconnect() { - if (mqtt_useauth) + if (this->mqtt_config.mqtt_useauth) { - return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password); + return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port, this->mqtt_config.mqtt_user, this->mqtt_config.mqtt_password); } else { - return this->connectToMqtt(client_id, mqtt_server, mqtt_port); + return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port); } } @@ -300,7 +296,6 @@ void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void)) void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config) { this->network_config = network_config; - client_id = network_config.hostname; } void ESPMegaIoT::loadNetworkConfig() @@ -323,38 +318,21 @@ void ESPMegaIoT::ethernetBegin() void ESPMegaIoT::loadMqttConfig() { // Load the mqtt config from FRAM - MqttConfig mqtt_config; - fram->read(sizeof(NetworkConfig), (uint8_t *)&mqtt_config, sizeof(MqttConfig)); + fram->read(sizeof(NetworkConfig), (uint8_t *)&this->mqtt_config, sizeof(MqttConfig)); // Populate the mqtt connection parameters - strcpy(mqtt_server, mqtt_config.mqtt_server); - mqtt_port = mqtt_config.mqtt_port; - strcpy(mqtt_user, mqtt_config.mqtt_user); - strcpy(mqtt_password, mqtt_config.mqtt_password); - mqtt_useauth = mqtt_config.mqtt_useauth; } void ESPMegaIoT::saveMqttConfig() { - // Save the mqtt config to FRAM - MqttConfig mqtt_config; - strcpy(mqtt_config.mqtt_server, mqtt_server); - mqtt_config.mqtt_port = mqtt_port; - strcpy(mqtt_config.mqtt_user, mqtt_user); - strcpy(mqtt_config.mqtt_password, mqtt_password); - mqtt_config.mqtt_useauth = mqtt_useauth; fram->write(MQTT_CONFIG_ADDRESS, (uint8_t *)&mqtt_config, sizeof(MqttConfig)); } void ESPMegaIoT::connectToMqtt() { - if (mqtt_useauth) - { - this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password); - } + if (mqtt_config.mqtt_useauth) + this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port, mqtt_config.mqtt_user, mqtt_config.mqtt_password); else - { - this->connectToMqtt(client_id, mqtt_server, mqtt_port); - } + this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port); } void ESPMegaIoT::connectNetwork() @@ -380,24 +358,7 @@ void ESPMegaIoT::connectNetwork() void ESPMegaIoT::setMqttConfig(MqttConfig mqtt_config) { - if (mqtt_server != nullptr) { - delete[] mqtt_server; - } - this->mqtt_server = new char[32]; - strcpy(mqtt_server, mqtt_config.mqtt_server); - mqtt_port = mqtt_config.mqtt_port; - if (mqtt_user != nullptr) { - delete[] mqtt_user; - } - this->mqtt_user = new char[32]; - strcpy(mqtt_user, mqtt_config.mqtt_user); - if (mqtt_password != nullptr) { - delete[] mqtt_password; - } - this->mqtt_password = new char[32]; - strcpy(mqtt_password, mqtt_config.mqtt_password); - mqtt_useauth = mqtt_config.mqtt_useauth; - strcpy(base_topic, mqtt_config.base_topic); + this->mqtt_config = mqtt_config; } void ESPMegaIoT::bindEthernetInterface(ETHClass *ethernetIface) @@ -405,7 +366,7 @@ void ESPMegaIoT::bindEthernetInterface(ETHClass *ethernetIface) this->ethernetIface = ethernetIface; } -IoTComponent* ESPMegaIoT::getComponent(uint8_t card_id) +IoTComponent *ESPMegaIoT::getComponent(uint8_t card_id) { return components[card_id]; } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp index 7fb6dbc..96be8ea 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -74,6 +74,7 @@ public: void registerSubscribeCallback(void (*callback)(void)); void setBaseTopic(char *base_topic); void bindEthernetInterface(ETHClass *ethernetIface); + IoTComponent* getComponent(uint8_t card_id); IPAddress getETHIp(); @@ -101,13 +102,8 @@ private: uint8_t base_topic_length; ExpansionCard **cards; // Points to card array in ESPMegaPRO Core // MQTT Connection Parameters - char *mqtt_server; - uint16_t mqtt_port; - char *mqtt_user; - char *mqtt_password; - char *client_id; - bool mqtt_useauth; bool mqtt_connected; NetworkConfig network_config; + MqttConfig mqtt_config; ETHClass *ethernetIface; }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index fb65de2..cab86ec 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -9,15 +9,7 @@ #include #include #include - -struct rtctime_t { - uint8_t hours; - uint8_t minutes; - uint8_t seconds; - uint8_t day; - uint8_t month; - uint16_t year; -}; +#include #define FRAM_ADDRESS 0x56 #define INPUT_BANK_A_ADDRESS 0x21 diff --git a/Template Project/lib/ESPMegaPRO/InternalDisplay.cpp b/Template Project/lib/ESPMegaPRO/InternalDisplay.cpp index 3f75678..71ec1cc 100644 --- a/Template Project/lib/ESPMegaPRO/InternalDisplay.cpp +++ b/Template Project/lib/ESPMegaPRO/InternalDisplay.cpp @@ -1,8 +1,7 @@ #include -void InternalDisplay::begin(ESPMegaPRO *espmega) { - this->espmega = espmega; +void InternalDisplay::begin() { } void InternalDisplay::loop() { @@ -26,17 +25,23 @@ void InternalDisplay::bindOutputCard(uint8_t card_id) { void InternalDisplay::handleInputStateChange(uint8_t pin, bool state) { // If the input card is binded to the display and the current page is the input page // then update the respective input component - // TODO: implementation + if (!this->bindedInputCard || this->currentPage != INTERNAL_DISPLAY_INPUT_PAGE) return; + // Update the input state + this->setInputMarker(pin, state); } -void InternalDisplay::handlePwmStateChange(uint8_t pin, uint16_t value) { +void InternalDisplay::handlePwmStateChange(uint8_t pin, bool state, uint16_t value) { // If the output card is binded to the display and the current page is the output page // then update the respective output component - // TODO: implementation + if (!this->bindedOutputCard || this->currentPage != INTERNAL_DISPLAY_OUTPUT_PAGE) return; + // Update the output state + this->setOutputBar(pin, value); + this->setOutputStateColor(pin, state); } void InternalDisplay::handlePageChange(uint8_t page) { - // TODO: implementation + // Refresh the page + this->refreshPage(page); } void InternalDisplay::saveNetworkConfig() { @@ -47,24 +52,51 @@ void InternalDisplay::saveMQTTConfig() { // TODO: implementation } -void InternalDisplay::updateStatusIcons() { - // TODO: implementation +void InternalDisplay::updateStatusIcons(bool networkStatus, bool mqttStatus) { + this->setNumber("server.pic", mqttStatus ? PIC_MQTT_CONNECTED : PIC_MQTT_DISCONNECTED); + this->setNumber("lan.pic", networkStatus ? PIC_LAN_CONNECTED : PIC_LAN_DISCONNECTED); } void InternalDisplay::updateClock() { - // TODO: implementation + rtctime_t time = this->getRtcTime(); + this->displayAdapter->print("time.txt="); + this->displayAdapter->print(time.hours%12); + this->displayAdapter->print(":"); + this->displayAdapter->print(time.minutes); + this->displayAdapter->print(" "); + this->displayAdapter->print(time.hours/12 ? "PM" : "AM"); + this->sendStopBytes(); + } void InternalDisplay::refreshPage() { - // TODO: implementation + this->refreshPage(this->currentPage); } void InternalDisplay::refreshPage(uint8_t page) { - // TODO: implementation + switch (page) { + case INTERNAL_DISPLAY_DASHBOARD_PAGE: + this->refreshDashboard(); + break; + case INTERNAL_DISPLAY_INPUT_PAGE: + this->refreshInput(); + break; + case INTERNAL_DISPLAY_OUTPUT_PAGE: + this->refreshOutput(); + break; + case INTERNAL_DISPLAY_AC_PAGE: + this->refreshAC(); + break; + } } void InternalDisplay::refreshDashboard() { - // TODO: implementation + // The dashboard have the following components: + // 1. Hostname + // 2. IP Address + // 3. MQTT Server with port + // 4. MQTT Connection status + this->iot-> } void InternalDisplay::refreshInput() { diff --git a/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp b/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp index e2e28a7..e2efe9c 100644 --- a/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp +++ b/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp @@ -1,24 +1,32 @@ -#include #include +#include +#include #define INTERNAL_DISPLAY_DASHBOARD_PAGE 0 #define INTERNAL_DISPLAY_INPUT_PAGE 1 #define INTERNAL_DISPLAY_OUTPUT_PAGE 2 #define INTERNAL_DISPLAY_AC_PAGE 3 +// Picture IDs +#define PIC_LAN_DISCONNECTED 2 +#define PIC_LAN_CONNECTED 3 +#define PIC_MQTT_DISCONNECTED 4 +#define PIC_MQTT_CONNECTED 5 + + class InternalDisplay : public ESPMegaDisplay { public: InternalDisplay(HardwareSerial *displayAdapter); - void begin(ESPMegaPRO *espmega); + void begin(ESPMegaIoT *iot, std::function getRtcTime); void loop(); void bindInputCard(uint8_t card_id); void bindOutputCard(uint8_t card_id); + private: uint8_t bindedInputCard; uint8_t bindedOutputCard; - ESPMegaPRO *espmega; void handleInputStateChange(uint8_t pin, bool state); - void handlePwmStateChange(uint8_t pin, uint16_t value); + void handlePwmStateChange(uint8_t pin, bool state, uint16_t value); void handlePageChange(uint8_t page); void setOutputBar(uint8_t pin, uint16_t value); void setOutputStateColor(uint8_t pin, bool state); @@ -28,7 +36,7 @@ class InternalDisplay : public ESPMegaDisplay { void setPWMAdjustmentButton(bool state); void saveNetworkConfig(); void saveMQTTConfig(); - void updateStatusIcons(); + void updateStatusIcons(bool networkStatus, bool mqttStatus); void updateClock(); void refreshPage(); void refreshPage(uint8_t page); @@ -36,4 +44,7 @@ class InternalDisplay : public ESPMegaDisplay { void refreshInput(); void refreshOutput(); void refreshAC(); + // Pointers to various data + ESPMegaIoT *iot; + std::function getRtcTime; }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/TimeStructure.hpp b/Template Project/lib/ESPMegaPRO/TimeStructure.hpp new file mode 100644 index 0000000..39d7085 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/TimeStructure.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +struct rtctime_t { + uint8_t hours; + uint8_t minutes; + uint8_t seconds; + uint8_t day; + uint8_t month; + uint16_t year; +};