fixed input publish and change climate to name

This commit is contained in:
Siwat Sirichai 2024-01-01 00:07:11 +07:00
parent 4c77474a94
commit e1f0fd5651
6 changed files with 55 additions and 14 deletions

View File

@ -129,6 +129,16 @@ void ClimateCard::setMode(uint8_t mode)
saveStateToFRAM(); 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) void ClimateCard::setFanSpeed(uint8_t fan_speed)
{ {
this->state.ac_fan_speed = fan_speed; this->state.ac_fan_speed = fan_speed;
@ -137,6 +147,30 @@ void ClimateCard::setFanSpeed(uint8_t fan_speed)
saveStateToFRAM(); 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<void(uint8_t, uint8_t, uint8_t)> callback) uint8_t ClimateCard::registerChangeCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback)
{ {
callbacks[callbacks_handler_count] = callback; callbacks[callbacks_handler_count] = callback;

View File

@ -52,9 +52,13 @@ class ClimateCard : public ExpansionCard {
void setTemperature(uint8_t temperature); void setTemperature(uint8_t temperature);
uint8_t getTemperature(); uint8_t getTemperature();
void setMode(uint8_t mode); void setMode(uint8_t mode);
void setModeByName(const char* mode_name);
uint8_t getMode(); uint8_t getMode();
char* getModeName();
void setFanSpeed(uint8_t fan_speed); void setFanSpeed(uint8_t fan_speed);
void setFanSpeedByName(const char* fan_speed_name);
uint8_t getFanSpeed(); uint8_t getFanSpeed();
char* getFanSpeedName();
float getRoomTemperature(); float getRoomTemperature();
float getHumidity(); float getHumidity();
uint8_t getSensorType(); uint8_t getSensorType();

View File

@ -41,15 +41,11 @@ void ClimateIoT::publishClimateTemperature() {
} }
void ClimateIoT::publishClimateMode() { void ClimateIoT::publishClimateMode() {
char payload[2]; this->publishRelative(AC_MODE_REPORT_TOPIC, this->card->getModeName());
itoa(this->card->getMode(), payload, 10);
this->publishRelative(AC_MODE_REPORT_TOPIC, payload);
} }
void ClimateIoT::publishClimateFanSpeed() { void ClimateIoT::publishClimateFanSpeed() {
char payload[2]; this->publishRelative(AC_FAN_SPEED_REPORT_TOPIC, this->card->getFanSpeedName());
itoa(this->card->getFanSpeed(), payload, 10);
this->publishRelative(AC_FAN_SPEED_REPORT_TOPIC, payload);
} }
void ClimateIoT::publishSensor() { 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) { bool ClimateIoT::processSetModeMessage(char *topic, char *payload, uint8_t topic_length) {
if (!strcmp(topic, AC_MODE_SET_TOPIC)) { if (!strcmp(topic, AC_MODE_SET_TOPIC)) {
uint8_t mode = atoi(payload); this->card->setModeByName(payload);
this->card->setMode(mode);
return true; return true;
} }
return false; 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) { bool ClimateIoT::processSetFanSpeedMessage(char *topic, char *payload, uint8_t topic_length) {
if (!strcmp(topic, AC_FAN_SPEED_SET_TOPIC)) { if (!strcmp(topic, AC_FAN_SPEED_SET_TOPIC)) {
uint8_t fan_speed = atoi(payload); this->card->setFanSpeedByName(payload);
this->card->setFanSpeed(fan_speed);
return true;
} }
return false; return false;
} }

View File

@ -57,7 +57,7 @@ uint8_t DigitalInputIoT::getType() {
void DigitalInputIoT::publishDigitalInput(uint8_t pin) { void DigitalInputIoT::publishDigitalInput(uint8_t pin) {
char topic[20] = {0}; char topic[20] = {0};
char payload[20] = {0}; char payload[20] = {0};
topic[0] = pin-pin%10 + '0'; topic[0] = pin/10 + '0';
topic[1] = pin%10 + '0'; topic[1] = pin%10 + '0';
topic[2] = '\0'; topic[2] = '\0';
payload[0] = this->card->digitalRead(pin, false) + '0'; payload[0] = this->card->digitalRead(pin, false) + '0';

View File

@ -7,6 +7,7 @@ void IoTComponent::setMqttClient(PubSubClient *mqtt) {
void IoTComponent::publishRelative(const char *topic, const char *payload) { void IoTComponent::publishRelative(const char *topic, const char *payload) {
static char absolute_topic[100]; static char absolute_topic[100];
sprintf(absolute_topic, "%s/%02d/%s", base_topic, card_id, topic); 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->publish(absolute_topic, payload);
mqtt->loop(); mqtt->loop();
delay(50); delay(50);

View File

@ -4,11 +4,11 @@
#include <ClimateCard.hpp> #include <ClimateCard.hpp>
// #define FRAM_DEBUG // #define FRAM_DEBUG
#define MQTT_DEBUG // #define MQTT_DEBUG
// #define WRITE_DEFAULT_NETCONF // #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE #define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER #define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE // #define DISPLAY_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library // 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); 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 #ifdef WRITE_DEFAULT_NETCONF
void setNetworkConfig() void setNetworkConfig()
{ {
@ -106,6 +114,7 @@ void setup()
#endif #endif
ESP_LOGI("Initializer", "Connecting to MQTT"); ESP_LOGI("Initializer", "Connecting to MQTT");
espmega.iot->connectToMqtt(); espmega.iot->connectToMqtt();
espmega.iot->registerMqttCallback(mqtt_callback);
#ifdef MQTT_CARD_REGISTER #ifdef MQTT_CARD_REGISTER
ESP_LOGI("Initializer", "Registering cards 0"); ESP_LOGI("Initializer", "Registering cards 0");
espmega.iot->registerCard(0); espmega.iot->registerCard(0);