From ea49576b206582d2a8bab9fab3fcb22239c33e6a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 20 May 2024 00:44:18 +0700 Subject: [PATCH 1/4] global state request --- .gitignore | 1 + ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index 9bea433..a965236 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store +/ESPMegaPRO-OS-SDK diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 80fd0d3..2b9c877 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -56,6 +56,16 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) { callback.second(topic_without_base, payload_buffer); } + // Check for global state request + if (!strcmp(topic_without_base,"requeststate")) { + for (int i = 0; i < 255; i++) + { + if (components[i] != NULL) + { + components[i]->publishReport(); + } + } + } // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id // /base_topic/card_id/... From 3e7d3041f2ac8fce10f5bb6f1cc4af6288c845e2 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 20 May 2024 00:53:09 +0700 Subject: [PATCH 2/4] mqtt summary request --- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 36 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 3 ++ 2 files changed, 39 insertions(+) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 2b9c877..b11e46b 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -66,6 +66,10 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) } } } + // Check for global summary request + if (!strcmp(topic_without_base,"requestinfo")) { + this->publishSystemSummary(); + } // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id // /base_topic/card_id/... @@ -844,3 +848,35 @@ String ESPMegaIoT::getMac() else return this->getETHMac(); } + +/** + * @brief Publish a json object containing the system summary + */ +void ESPMegaIoT::publishSystemSummary() { + char payload[1024]; + StaticJsonDocument<1024> doc; + JsonObject root = doc.to(); + root["ip"] = this->getIp().toString(); + root["mac"] = this->getMac(); + root["mqtt_connected"] = this->mqttConnected(); + root["network_connected"] = this->networkConnected(); + root["mqtt_server"] = this->mqtt_config.mqtt_server; + root["mqtt_port"] = this->mqtt_config.mqtt_port; + root["hostname"] = this->network_config.hostname; + root["firmware"] = SW_VERSION; + root["idf_version"] = esp_get_idf_version(); + root["sdk_version"] = SDK_VESRION; + root["board_model"] = BOARD_MODEL; + JsonArray cards = root.createNestedArray("cards"); + for (int i = 0; i < 255; i++) + { + if (components[i] != NULL) + { + JsonObject card = cards.createNestedObject(); + card["id"] = i; + card["type"] = components[i]->getType(); + } + } + serializeJson(doc, payload); + this->publishRelative("info", payload); +} \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp index c5467b2..d47f723 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include // MQTT Connection Parameters #define TCP_TIMEOUT_SEC 5 @@ -115,6 +117,7 @@ public: void bindEthernetInterface(ETHClass *ethernetIface); bool networkConnected(); void bindFRAM(FRAM *fram); + void publishSystemSummary(); IoTComponent* getComponent(uint8_t card_id); IPAddress getETHIp(); From 2430b43f77546c768c23aeb77abee3a8062df37f Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 20 May 2024 01:25:25 +0700 Subject: [PATCH 3/4] info request --- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 45 +++++++++++++++---- .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 2 + ESPMegaPRO-OS-SDK/src/main.cpp | 7 +-- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index b11e46b..51ffd56 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -65,10 +65,12 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) components[i]->publishReport(); } } + return; } // Check for global summary request if (!strcmp(topic_without_base,"requestinfo")) { this->publishSystemSummary(); + return; } // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id @@ -393,6 +395,18 @@ void ESPMegaIoT::publish(const char *topic, const char *payload) mqtt.publish(topic, payload); } +/** + * @brief Publish a message to a topic + * + * @param topic The topic to publish to + * @param payload The payload to publish + * @param length The length of the payload + */ +void ESPMegaIoT::publish(const char *topic, const char *payload, unsigned int length) +{ + mqtt.publish(topic, (const uint8_t *)payload, length); +} + /** * @brief Register a callback for MQTT messages * @@ -416,7 +430,7 @@ void ESPMegaIoT::unregisterMqttCallback(uint16_t handler) } /** - * @brief Subscribe to all components's topics + * @brief Subscribe to all components's topics and all other topics */ void ESPMegaIoT::mqttSubscribe() { @@ -436,6 +450,9 @@ void ESPMegaIoT::mqttSubscribe() mqtt.loop(); } } + // Global topics + this->subscribeRelative("requeststate"); + this->subscribeRelative("requestinfo"); } /** @@ -531,6 +548,21 @@ void ESPMegaIoT::publishRelative(const char *topic, const char *payload) mqtt.loop(); } +/** + * @brief Publish a message relative to the base topic + * + * @param topic The topic to publish to + * @param payload The payload to publish + * @param length The length of the payload + */ +void ESPMegaIoT::publishRelative(const char *topic, const char *payload, unsigned int length) +{ + char absolute_topic[100]; + sprintf(absolute_topic, "%s/%s", this->mqtt_config.base_topic, topic); + mqtt.publish(absolute_topic, (const uint8_t *)payload, length); + mqtt.loop(); +} + /** * @brief Subscribe to a topic relative to the base topic * @@ -857,14 +889,7 @@ void ESPMegaIoT::publishSystemSummary() { StaticJsonDocument<1024> doc; JsonObject root = doc.to(); root["ip"] = this->getIp().toString(); - root["mac"] = this->getMac(); - root["mqtt_connected"] = this->mqttConnected(); - root["network_connected"] = this->networkConnected(); - root["mqtt_server"] = this->mqtt_config.mqtt_server; - root["mqtt_port"] = this->mqtt_config.mqtt_port; - root["hostname"] = this->network_config.hostname; root["firmware"] = SW_VERSION; - root["idf_version"] = esp_get_idf_version(); root["sdk_version"] = SDK_VESRION; root["board_model"] = BOARD_MODEL; JsonArray cards = root.createNestedArray("cards"); @@ -878,5 +903,7 @@ void ESPMegaIoT::publishSystemSummary() { } } serializeJson(doc, payload); - this->publishRelative("info", payload); + ESP_LOGD("ESPMegaIoT", "Publishing system summary: %s", payload); + mqtt.loop(); + this->publishRelative("info", payload, strlen(payload)); } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp index d47f723..76498c0 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -83,6 +83,7 @@ public: void publishCard(uint8_t card_id); // Publish topic appended with base topic void publishRelative(const char *topic, const char *payload); + void publishRelative(const char *topic, const char *payload, unsigned int length); // Subscribe topic appended with base topic void subscribeRelative(const char *topic); void subscribe(const char *topic); @@ -107,6 +108,7 @@ public: bool mqttConnected(); void disconnectFromMqtt(); void publish(const char *topic, const char *payload); + void publish(const char *topic, const char *payload, unsigned int length); uint16_t registerMqttCallback(std::function callback); void unregisterMqttCallback(uint16_t handler); uint16_t registerRelativeMqttCallback(std::function callback); diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index e8e43af..aa0bedf 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -15,13 +15,13 @@ // #define FRAM_DEBUG // #define MQTT_DEBUG #define WRITE_DEFAULT_NETCONF -//#define CLIMATE_CARD_ENABLE +#define CLIMATE_CARD_ENABLE #define MQTT_CARD_REGISTER #define DISPLAY_ENABLE #define WEB_SERVER_ENABLE #define LCD_OTA_ENABLE -//#define REMOTE_VARIABLE_ENABLE -//#define CT_ENABLE +// #define REMOTE_VARIABLE_ENABLE +// #define CT_ENABLE #define SMART_VARIABLE_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library @@ -236,6 +236,7 @@ void setup() ct.bindFRAM(&espmega.fram, 7000); ct.loadEnergy(); ct.setEnergyAutoSave(true); + espmega.iot->registerCard(3); #endif #ifdef SMART_VARIABLE_ENABLE ESP_LOGI("Initializer", "Initializing smart variable"); From 64d6c4fab3e2e8fefded0898ecc07421dcf64dd7 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 20 May 2024 01:29:36 +0700 Subject: [PATCH 4/4] Update ESPMegaCommon.hpp --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index 377ba10..59d64f4 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.7.0" \ No newline at end of file +#define SDK_VESRION "2.8.0" \ No newline at end of file