From 000c06809c492440bc5202b1daff7bf0432eda90 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 12 Feb 2024 16:15:31 +0700 Subject: [PATCH] CT IoT --- ...sformer.cpp => CurrentTransformerCard.cpp} | 40 ++++++------ ...sformer.hpp => CurrentTransformerCard.hpp} | 8 ++- .../lib/ESPMegaPRO/CurrentTransformerIoT.cpp | 63 +++++++++++++++++++ .../lib/ESPMegaPRO/CurrentTransformerIoT.hpp | 27 ++++++++ ESPMegaPRO-OS-SDK/src/main.cpp | 2 +- 5 files changed, 116 insertions(+), 24 deletions(-) rename ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/{CurrentTransformer.cpp => CurrentTransformerCard.cpp} (60%) rename ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/{CurrentTransformer.hpp => CurrentTransformerCard.hpp} (85%) create mode 100644 ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp create mode 100644 ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.hpp diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp similarity index 60% rename from ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.cpp rename to ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index 8b581f4..12710ae 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp @@ -1,6 +1,6 @@ -#include +#include -CurrentTransformer::CurrentTransformer(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function adcToCurrent, uint32_t conversionInterval) +CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function adcToCurrent, uint32_t conversionInterval) { this->analogCard = analogCard; this->pin = pin; @@ -8,18 +8,18 @@ CurrentTransformer::CurrentTransformer(AnalogCard* analogCard, uint8_t pin, floa this->adcToCurrent = adcToCurrent; } -void CurrentTransformer::bindFRAM(FRAM *fram, uint32_t framAddress) +void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress) { this->fram = fram; this->framAddress = framAddress; } -void CurrentTransformer::begin() +void CurrentTransformerCard::begin() { this->beginConversion(); } -void CurrentTransformer::loop() +void CurrentTransformerCard::loop() { if (this->lastConversionTime == 0) { this->lastConversionTime = millis(); @@ -31,63 +31,63 @@ void CurrentTransformer::loop() } } -void CurrentTransformer::beginConversion() +void CurrentTransformerCard::beginConversion() { uint16_t adcValue = this->analogCard->analogRead(this->pin); this->current = this->adcToCurrent(adcValue); this->setEnergy(this->energy + this->current * *this->voltage * (millis() - this->lastConversionTime) / 3600000); // in Wh this->lastConversionTime = millis(); - for (auto const& callback : this->callbacks) { - callback.second(this->current, this->energy); - } } -void CurrentTransformer::setEnergy(float energy) +void CurrentTransformerCard::setEnergy(float energy) { this->energy = energy; if (this->autoSave) { this->saveEnergy(); } + for (auto const& callback : this->callbacks) { + callback.second(this->current, this->energy); + } } -void CurrentTransformer::resetEnergy() +void CurrentTransformerCard::resetEnergy() { this->setEnergy(0); } -float CurrentTransformer::getCurrent() +float CurrentTransformerCard::getCurrent() { return this->current; } -double CurrentTransformer::getEnergy() +double CurrentTransformerCard::getEnergy() { return this->energy; } -uint8_t CurrentTransformer::registerCallback(std::function callback) { +uint8_t CurrentTransformerCard::registerCallback(std::function callback) { this->callbacks[this->handler_count] = callback; return this->handler_count++; } -void CurrentTransformer::unregisterCallback(uint8_t handler) { +void CurrentTransformerCard::unregisterCallback(uint8_t handler) { this->callbacks.erase(handler); } -void CurrentTransformer::saveEnergy(){ +void CurrentTransformerCard::saveEnergy(){ this->fram->write(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy)); } -void CurrentTransformer::loadEnergy(){ +void CurrentTransformerCard::loadEnergy(){ this->fram->read(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy)); } -void CurrentTransformer::setEnergyAutoSave(bool autoSave){ +void CurrentTransformerCard::setEnergyAutoSave(bool autoSave){ this->autoSave = autoSave; } -float CurrentTransformer::getVoltage(){ +float CurrentTransformerCard::getVoltage(){ return *this->voltage; } -float CurrentTransformer::getPower(){ +float CurrentTransformerCard::getPower(){ return this->current * *this->voltage; } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp similarity index 85% rename from ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.hpp rename to ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp index e449a08..6a36d2e 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformer.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp @@ -3,15 +3,17 @@ #include #include +#define CARD_TYPE_CT 5 + /** * @brief The CurrentTransformer class is a class for reading the current and energy from a current transformer that's connected to the AnalogCard. * Also supports storing energy to FRAM. */ -class CurrentTransformer +class CurrentTransformerCard { public: - CurrentTransformer(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function adcToCurrent, uint32_t conversionInterval); + CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function adcToCurrent, uint32_t conversionInterval); void bindFRAM(FRAM *fram, uint32_t framAddress); // Takes 16 bytes of FRAM (long double energy) void begin(); void loop(); @@ -19,7 +21,7 @@ class CurrentTransformer void setEnergy(float energy); void resetEnergy(); float getCurrent(); - long double getEnergy(); + double getEnergy(); float getPower(); void saveEnergy(); void loadEnergy(); diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp new file mode 100644 index 0000000..8bdab30 --- /dev/null +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.cpp @@ -0,0 +1,63 @@ +#include + +CurrentTransformerIoT::CurrentTransformerIoT() { + +} + +CurrentTransformerIoT::~CurrentTransformerIoT() { + +} + +bool CurrentTransformerIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { + ESP_LOGD("CurrentTransformerIoT", "Beginning CurrentTransformerIoT"); + this->card_id = card_id; + this->currentTransformerCard = (CurrentTransformerCard*) card; + this->mqtt = mqtt; + this->base_topic = base_topic; + auto bindedCTCallback = std::bind(&CurrentTransformerIoT::handleCTCallback, this, std::placeholders::_1, std::placeholders::_2); + this->currentTransformerCard->registerCallback(bindedCTCallback); + return true; +} + +void CurrentTransformerIoT::handleMqttMessage(char *topic, char *payload) { + uint8_t payload_length = strlen(payload); + if(this->processSetEnergyMessage(topic, payload, payload_length)) return; + if (!strcmp(topic, CT_RESET_ENERGY_TOPIC)) { + this->currentTransformerCard->resetEnergy(); + return; + } else if (!strcmp(topic, CT_REQUESTSTATE_TOPIC)) { + this->publishReport(); + return; + } +} + +void CurrentTransformerIoT::subscribe() { + this->subscribeRelative(CT_SET_ENERGY_TOPIC); + this->subscribeRelative(CT_RESET_ENERGY_TOPIC); + this->subscribeRelative(CT_REQUESTSTATE_TOPIC); +} + +void CurrentTransformerIoT::loop() { + // Not used, still need this to meet polymorphism requirements +} + +void CurrentTransformerIoT::publishReport() { + char outputBuffer[256]; + snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getPower()); + this->publishRelative(CT_POWER_TOPIC, outputBuffer); + snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getEnergy()); + this->publishRelative(CT_ENERGY_TOPIC, outputBuffer); + snprintf(outputBuffer, sizeof(outputBuffer), "%.2f", this->currentTransformerCard->getCurrent()); + this->publishRelative(CT_CURRENT_TOPIC, outputBuffer); +} + +uint8_t CurrentTransformerIoT::getType() { + return CARD_TYPE_CT; +} + +bool CurrentTransformerIoT::processSetEnergyMessage(char* topic, char* payload, uint8_t topic_length) { + if(strcmp(topic, CT_SET_ENERGY_TOPIC)) return false; + this->currentTransformerCard->setEnergy(atof(payload)); + return true; +} + diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.hpp new file mode 100644 index 0000000..19f14ef --- /dev/null +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerIoT.hpp @@ -0,0 +1,27 @@ +#include +#include +#include + +#define CT_REQUESTSTATE_TOPIC "requeststate" +#define CT_SET_ENERGY_TOPIC "energy/set" +#define CT_RESET_ENERGY_TOPIC "energy/reset" +#define CT_ENERGY_TOPIC "energy" +#define CT_POWER_TOPIC "power" +#define CT_CURRENT_TOPIC "current" + +class CurrentTransformerIoT : public IoTComponent +{ +public: + CurrentTransformerIoT(); + ~CurrentTransformerIoT(); + bool begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic); + void handleMqttMessage(char *topic, char *payload); + void subscribe(); + void loop(); + void publishReport(); + uint8_t getType(); +private: + CurrentTransformerCard *currentTransformerCard; + bool processSetEnergyMessage(char* topic, char* payload, uint8_t topic_length); + void handleCTCallback(float current, double energy); +}; \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 84c1ef3..7e1c6b9 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include // #define FRAM_DEBUG