From 69b2198342ac3509606f87d1e7dea9f2eff5f352 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 12 Feb 2024 16:20:16 +0700 Subject: [PATCH] integrate CT with ESPMegaIoT --- .../lib/ESPMegaPRO/CurrentTransformerCard.cpp | 4 ++++ .../lib/ESPMegaPRO/CurrentTransformerCard.hpp | 3 ++- .../lib/ESPMegaPRO/CurrentTransformerIoT.cpp | 2 +- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp | 8 ++++++++ ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp | 2 ++ 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index 12710ae..346a322 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp @@ -90,4 +90,8 @@ float CurrentTransformerCard::getVoltage(){ float CurrentTransformerCard::getPower(){ return this->current * *this->voltage; +} + +uint8_t CurrentTransformerCard::getType() { + return CARD_TYPE_CT; } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp index 6a36d2e..915889f 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp @@ -3,7 +3,7 @@ #include #include -#define CARD_TYPE_CT 5 +#define CARD_TYPE_CT 4 /** * @brief The CurrentTransformer class is a class for reading the current and energy from a current transformer that's connected to the AnalogCard. @@ -29,6 +29,7 @@ class CurrentTransformerCard float getVoltage(); uint8_t registerCallback(std::function callback); void unregisterCallback(uint8_t handler); + uint8_t getType(); private: AnalogCard* analogCard; uint8_t pin; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp index 8bdab30..18f5571 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp @@ -52,7 +52,7 @@ void CurrentTransformerIoT::publishReport() { } uint8_t CurrentTransformerIoT::getType() { - return CARD_TYPE_CT; + return this->currentTransformerCard->getType(); } bool CurrentTransformerIoT::processSetEnergyMessage(char* topic, char* payload, uint8_t topic_length) { diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index e4ee713..9362e10 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -164,6 +164,14 @@ void ESPMegaIoT::registerCard(uint8_t card_id) components[card_id]->publishReport(); } break; + case CARD_TYPE_CT: + components[card_id] = new CurrentTransformerIoT(); + components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.base_topic); + if (mqtt_connected) + { + components[card_id]->subscribe(); + components[card_id]->publishReport(); + } default: ESP_LOGE("ESPMegaIoT", "Registering card %d failed: Unknown card", card_id); return; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp index c595465..74f5dd2 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include