improve MQTT state request

This commit is contained in:
Siwat Sirichai 2024-05-17 21:29:50 +07:00
parent dc96be25b8
commit d75b028cd2
4 changed files with 9 additions and 2 deletions

View File

@ -148,6 +148,7 @@ void ClimateIoT::subscribe() {
this->subscribeRelative(AC_TEMPERATURE_SET_TOPIC); this->subscribeRelative(AC_TEMPERATURE_SET_TOPIC);
this->subscribeRelative(AC_MODE_SET_TOPIC); this->subscribeRelative(AC_MODE_SET_TOPIC);
this->subscribeRelative(AC_FAN_SPEED_SET_TOPIC); this->subscribeRelative(AC_FAN_SPEED_SET_TOPIC);
this->subscribeRelative(AC_REQUEST_STATE_TOPIC);
ESP_LOGD("ClimateIoT", "Subscribed to topics"); ESP_LOGD("ClimateIoT", "Subscribed to topics");
} }

View File

@ -12,7 +12,7 @@
#define AC_FAN_SPEED_SET_TOPIC "set/fan_speed" #define AC_FAN_SPEED_SET_TOPIC "set/fan_speed"
#define AC_ROOM_TEMPERATURE_REPORT_TOPIC "room_temperature" #define AC_ROOM_TEMPERATURE_REPORT_TOPIC "room_temperature"
#define AC_HUMIDITY_REPORT_TOPIC "humidity" #define AC_HUMIDITY_REPORT_TOPIC "humidity"
#define AC_REQUEST_STATE_TOPIC "request_state" #define AC_REQUEST_STATE_TOPIC "requeststate"
/** /**
* @brief The ClimateIoT class is a class for connecting the Climate Card to the IoT module. * @brief The ClimateIoT class is a class for connecting the Climate Card to the IoT module.

View File

@ -31,6 +31,7 @@ bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *
*/ */
void DigitalInputIoT::subscribe() { void DigitalInputIoT::subscribe() {
this->subscribeRelative(PUBLISH_ENABLE_TOPIC); this->subscribeRelative(PUBLISH_ENABLE_TOPIC);
this->subscribeRelative(INPUT_REQUEST_STATE_TOPIC);
} }
/** /**
@ -40,14 +41,18 @@ void DigitalInputIoT::subscribe() {
* @param payload The null-terminated payload of the MQTT message. * @param payload The null-terminated payload of the MQTT message.
*/ */
void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) { void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) {
if (!strcmp(topic, INPUT_REQUEST_STATE_TOPIC)) {
this->publishDigitalInputs();
}
// payload is char '0' or '1' // payload is char '0' or '1'
if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) { else if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) {
if (payload[0] == '1') { if (payload[0] == '1') {
this->setDigitalInputsPublishEnabled(true); this->setDigitalInputsPublishEnabled(true);
} else { } else {
this->setDigitalInputsPublishEnabled(false); this->setDigitalInputsPublishEnabled(false);
} }
} }
} }
/** /**

View File

@ -6,6 +6,7 @@
// MQTT Topics // MQTT Topics
#define PUBLISH_ENABLE_TOPIC "publish_enable" #define PUBLISH_ENABLE_TOPIC "publish_enable"
#define INPUT_REQUEST_STATE_TOPIC "requeststate"
/** /**
* @brief The DigitalInputIoT class is a class for connecting the Digital Input Card to the IoT module. * @brief The DigitalInputIoT class is a class for connecting the Digital Input Card to the IoT module.