From e1f0fd56511b2a516e10a093bcba525d66f8d94c Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 1 Jan 2024 00:07:11 +0700 Subject: [PATCH] fixed input publish and change climate to name --- .../lib/ESPMegaPRO/ClimateCard.cpp | 34 +++++++++++++++++++ .../lib/ESPMegaPRO/ClimateCard.hpp | 4 +++ .../lib/ESPMegaPRO/ClimateIoT.cpp | 15 +++----- .../lib/ESPMegaPRO/DigitalInputIoT.cpp | 2 +- .../lib/ESPMegaPRO/IoTComponent.cpp | 1 + ESPMegaPRO-firmware/src/main.cpp | 13 +++++-- 6 files changed, 55 insertions(+), 14 deletions(-) diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp index 4e1deb3..25484fe 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp @@ -129,6 +129,16 @@ void ClimateCard::setMode(uint8_t mode) saveStateToFRAM(); } +char* ClimateCard::getModeName() +{ + return (char*)ac.mode_names[state.ac_mode]; +} + +char* ClimateCard::getFanSpeedName() +{ + return (char*)ac.fan_speed_names[state.ac_fan_speed]; +} + void ClimateCard::setFanSpeed(uint8_t fan_speed) { this->state.ac_fan_speed = fan_speed; @@ -137,6 +147,30 @@ void ClimateCard::setFanSpeed(uint8_t fan_speed) saveStateToFRAM(); } +void ClimateCard::setFanSpeedByName(const char* fan_speed_name) +{ + for (uint8_t i = 0; i < ac.fan_speeds; i++) + { + if (strcmp(fan_speed_name, ac.fan_speed_names[i]) == 0) + { + setFanSpeed(i); + return; + } + } +} + +void ClimateCard::setModeByName(const char* mode_name) +{ + for (uint8_t i = 0; i < ac.modes; i++) + { + if (strcmp(mode_name, ac.mode_names[i]) == 0) + { + setMode(i); + return; + } + } +} + uint8_t ClimateCard::registerChangeCallback(std::function callback) { callbacks[callbacks_handler_count] = callback; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp index 3b2a9bd..f17ad50 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp @@ -52,9 +52,13 @@ class ClimateCard : public ExpansionCard { void setTemperature(uint8_t temperature); uint8_t getTemperature(); void setMode(uint8_t mode); + void setModeByName(const char* mode_name); uint8_t getMode(); + char* getModeName(); void setFanSpeed(uint8_t fan_speed); + void setFanSpeedByName(const char* fan_speed_name); uint8_t getFanSpeed(); + char* getFanSpeedName(); float getRoomTemperature(); float getHumidity(); uint8_t getSensorType(); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp index 54db2f7..c2f4c51 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp @@ -41,15 +41,11 @@ void ClimateIoT::publishClimateTemperature() { } void ClimateIoT::publishClimateMode() { - char payload[2]; - itoa(this->card->getMode(), payload, 10); - this->publishRelative(AC_MODE_REPORT_TOPIC, payload); + this->publishRelative(AC_MODE_REPORT_TOPIC, this->card->getModeName()); } void ClimateIoT::publishClimateFanSpeed() { - char payload[2]; - itoa(this->card->getFanSpeed(), payload, 10); - this->publishRelative(AC_FAN_SPEED_REPORT_TOPIC, payload); + this->publishRelative(AC_FAN_SPEED_REPORT_TOPIC, this->card->getFanSpeedName()); } void ClimateIoT::publishSensor() { @@ -113,8 +109,7 @@ bool ClimateIoT::processSetTemperatureMessage(char *topic, char *payload, uint8_ bool ClimateIoT::processSetModeMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_MODE_SET_TOPIC)) { - uint8_t mode = atoi(payload); - this->card->setMode(mode); + this->card->setModeByName(payload); return true; } return false; @@ -122,9 +117,7 @@ bool ClimateIoT::processSetModeMessage(char *topic, char *payload, uint8_t topic bool ClimateIoT::processSetFanSpeedMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_FAN_SPEED_SET_TOPIC)) { - uint8_t fan_speed = atoi(payload); - this->card->setFanSpeed(fan_speed); - return true; + this->card->setFanSpeedByName(payload); } return false; } diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp index 138f91b..2f7529e 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp @@ -57,7 +57,7 @@ uint8_t DigitalInputIoT::getType() { void DigitalInputIoT::publishDigitalInput(uint8_t pin) { char topic[20] = {0}; char payload[20] = {0}; - topic[0] = pin-pin%10 + '0'; + topic[0] = pin/10 + '0'; topic[1] = pin%10 + '0'; topic[2] = '\0'; payload[0] = this->card->digitalRead(pin, false) + '0'; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/IoTComponent.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/IoTComponent.cpp index d4fa956..f430b90 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/IoTComponent.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/IoTComponent.cpp @@ -7,6 +7,7 @@ void IoTComponent::setMqttClient(PubSubClient *mqtt) { void IoTComponent::publishRelative(const char *topic, const char *payload) { static char absolute_topic[100]; sprintf(absolute_topic, "%s/%02d/%s", base_topic, card_id, topic); + ESP_LOGD("IoTComponent", "Publishing to %s : %s", absolute_topic, payload); mqtt->publish(absolute_topic, payload); mqtt->loop(); delay(50); diff --git a/ESPMegaPRO-firmware/src/main.cpp b/ESPMegaPRO-firmware/src/main.cpp index b2c358e..5fff0b7 100644 --- a/ESPMegaPRO-firmware/src/main.cpp +++ b/ESPMegaPRO-firmware/src/main.cpp @@ -4,11 +4,11 @@ #include // #define FRAM_DEBUG -#define MQTT_DEBUG +// #define MQTT_DEBUG // #define WRITE_DEFAULT_NETCONF #define CLIMATE_CARD_ENABLE #define MQTT_CARD_REGISTER -#define DISPLAY_ENABLE +// #define DISPLAY_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library @@ -47,6 +47,14 @@ void input_change_callback(uint8_t pin, uint8_t value) Serial.println(value); } +void mqtt_callback(char *topic, char* payload) +{ + Serial.print("MQTT Callback: "); + Serial.print(topic); + Serial.print(" "); + Serial.println(payload); +} + #ifdef WRITE_DEFAULT_NETCONF void setNetworkConfig() { @@ -106,6 +114,7 @@ void setup() #endif ESP_LOGI("Initializer", "Connecting to MQTT"); espmega.iot->connectToMqtt(); + espmega.iot->registerMqttCallback(mqtt_callback); #ifdef MQTT_CARD_REGISTER ESP_LOGI("Initializer", "Registering cards 0"); espmega.iot->registerCard(0);