From 549500822ccda0edb839a3637fc72e7dead8d03a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 30 Nov 2023 18:53:01 +0700 Subject: [PATCH] update state publishing --- src/espmega_iot_core.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/espmega_iot_core.hpp | 4 ++++ 2 files changed, 44 insertions(+) diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index a997d5d..303df07 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -579,6 +579,9 @@ void mqtt_connect() publish_ac_state(); #endif mqtt_connected_user_callback(); + publish_dac_states(); + publish_dac_values(); + publish_all_adc(); standalone = false; ESPMega_updateTimeFromNTP(); } @@ -1004,6 +1007,9 @@ void state_request_callback() publish_env_state(); #endif user_state_request_callback(); + publish_all_adc(); + publish_dac_states(); + publish_dac_values(); } #ifdef ENABLE_IR_MODULE @@ -1929,6 +1935,22 @@ void publish_all_adc() publish_adc(i); } } + +void publish_dac_state(int id) +{ + DAC_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; + DAC_STATE_TOPIC[base_topic_length + 5] = (id % 10) + '0'; + mqtt.publish(DAC_STATE_TOPIC, dac_states[id] ? "on" : "off"); +} + +void publish_dac_value(int id) { + DAC_VALUE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; + DAC_VALUE_TOPIC[base_topic_length + 5] = (id % 10) + '0'; + char temp[6]; + itoa(dac_values[id], temp, DEC); + mqtt.publish(DAC_VALUE_TOPIC, temp); +} + /** * @brief Retrieves the ADC value for the specified ID. * @@ -1987,6 +2009,7 @@ void dac_set_value(int id, int value) dac_values[id] = value; ESPMega_dacWrite(id, dac_values[id] * dac_states[id]); ESPMega_FRAM.write16(EEPROM_ADDRESS_DAC_VALUE + id * 2, dac_values[id]); + publish_dac_value(id); } /** @@ -2003,6 +2026,23 @@ void dac_set_state(int id, bool state) dac_states[id] = state; ESPMega_dacWrite(id, dac_values[id] * dac_states[id]); ESPMega_FRAM.write8(EEPROM_ADDRESS_DAC_STATE + id, dac_states[id]); + publish_dac_state(id); +} + +void publish_dac_states() +{ + for (int i = 0; i < DAC_COUNT; i++) + { + publish_dac_state(i); + } +} + +void publish_dac_values() +{ + for (int i = 0; i < DAC_COUNT; i++) + { + publish_dac_value(i); + } } /** diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index a2eb29f..9e48e06 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -147,3 +147,7 @@ void dac_set_value(int id, int value); void dac_set_state(int id, bool state); void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length); void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length); +void publish_dac_value(int id); +void publish_dac_state(int id); +void publish_dac_values(); +void publish_dac_states(); \ No newline at end of file