This commit is contained in:
Siwat Sirichai 2024-02-12 16:15:31 +07:00
parent 8378338ed8
commit 000c06809c
5 changed files with 116 additions and 24 deletions

View File

@ -1,6 +1,6 @@
#include <CurrentTransformer.hpp>
#include <CurrentTransformerCard.hpp>
CurrentTransformer::CurrentTransformer(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval)
CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> 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<void(float, double)> callback) {
uint8_t CurrentTransformerCard::registerCallback(std::function<void(float, double)> 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;
}

View File

@ -3,15 +3,17 @@
#include <FRAM.h>
#include <map>
#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<float(uint16_t)> adcToCurrent, uint32_t conversionInterval);
CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> 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();

View File

@ -0,0 +1,63 @@
#include <CurrentTransformerIoT.hpp>
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;
}

View File

@ -0,0 +1,27 @@
#include <CurrentTransformerCard.hpp>
#include <IoTComponent.hpp>
#include <ExpansionCard.hpp>
#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);
};

View File

@ -4,7 +4,7 @@
#include <ClimateCard.hpp>
#include <ESPMegaDisplayOTA.hpp>
#include <RemoteVariable.hpp>
#include <CurrentTransformer.hpp>
#include <CurrentTransformerCard.hpp>
#include <AnalogCard.hpp>
// #define FRAM_DEBUG