diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 303df07..cf087c2 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -581,7 +581,7 @@ void mqtt_connect() mqtt_connected_user_callback(); publish_dac_states(); publish_dac_values(); - publish_all_adc(); + publish_adc_values(); standalone = false; ESPMega_updateTimeFromNTP(); } @@ -1007,7 +1007,7 @@ void state_request_callback() publish_env_state(); #endif user_state_request_callback(); - publish_all_adc(); + publish_adc_values(); publish_dac_states(); publish_dac_values(); } @@ -1845,6 +1845,8 @@ void enable_adc(int id) { adc_report_enable[id] = true; ESPMega_FRAM.write8(EEPROM_ADDRESS_ADC_REPORT_STATE + id, 1); + publish_adc_state(id); + Serial.println("ADC " + String(id) + " enabled."); } /** * @brief Disables the ADC reporting for the specified ID. @@ -1857,7 +1859,25 @@ void disable_adc(int id) { adc_report_enable[id] = false; ESPMega_FRAM.write8(EEPROM_ADDRESS_ADC_REPORT_STATE + id, 0); + publish_adc_state(id); + Serial.println("ADC " + String(id) + " disabled."); } + +void publish_adc_state(int id) +{ + ADC_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; + ADC_STATE_TOPIC[base_topic_length + 5] = (id % 10) + '0'; + mqtt.publish(ADC_STATE_TOPIC, adc_report_enable[id] ? "on" : "off"); +} + +void publish_adc_states() +{ + for (int i = 0; i < ADC_COUNT; i++) + { + publish_adc_state(i); + } +} + /** * @brief Updates the ADC value for the specified ID if ADC reporting is enabled. * @@ -1869,6 +1889,7 @@ void adc_update(int id) { adc_values[id] = ESPMega_analogRead(id); } + } /** @@ -1905,7 +1926,7 @@ void adc_update_all() void adc_loop() { adc_update_all(); - publish_all_adc(); + publish_adc_values(); } /** @@ -1913,13 +1934,13 @@ void adc_loop() * * @param id The ID of the ADC channel. */ -void publish_adc(int id) +void publish_adc_value(int id) { - ADC_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; - ADC_STATE_TOPIC[base_topic_length + 5] = (id % 10) + '0'; - char temp[6]; + ADC_REPORT_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; + ADC_REPORT_TOPIC[base_topic_length + 5] = (id % 10) + '0'; + char temp[8]; itoa(adc_values[id], temp, DEC); - mqtt.publish(ADC_STATE_TOPIC, temp); + mqtt.publish(ADC_REPORT_TOPIC, temp); } /** @@ -1927,12 +1948,12 @@ void publish_adc(int id) * This function iterates through all ADC channels and publishes the values * of the enabled channels using the publish_adc() function. */ -void publish_all_adc() +void publish_adc_values() { for (int i = 0; i < ADC_COUNT; i++) { if (adc_report_enable[i]) - publish_adc(i); + publish_adc_value(i); } } @@ -1980,9 +2001,11 @@ uint16_t get_adc_value(int id) */ void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length) { - int a = topic[4] - '0'; - int b = topic[5] - '0'; + Serial.println(topic); + int a = topic[5] - '0'; + int b = topic[6] - '0'; int id = 10 * a + b; + Serial.println("ID = "+String(id)); if (!strcmp(payload, "on")) { enable_adc(id); @@ -2055,8 +2078,8 @@ void publish_dac_values() */ void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length) { - int a = topic[4] - '0'; - int b = topic[5] - '0'; + int a = topic[5] - '0'; + int b = topic[6] - '0'; int id = 10 * a + b; int value = atoi(payload); dac_set_value(id, value); @@ -2074,8 +2097,8 @@ void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, un */ void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length) { - int a = topic[4] - '0'; - int b = topic[5] - '0'; + int a = topic[5] - '0'; + int b = topic[6] - '0'; int id = 10 * a + b; if (!strcmp(payload, "on")) { diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index 9e48e06..531735a 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -139,8 +139,8 @@ void adc_update(int id); void adc_update_force(int id); void adc_update_all(); void adc_loop(); -void publish_adc(int id); -void publish_all_adc(); +void publish_adc_value(int id); +void publish_adc_values(); uint16_t get_adc_value(int id); void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length); void dac_set_value(int id, int value); @@ -150,4 +150,6 @@ void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, un 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 +void publish_dac_states(); +void publish_adc_state(int id); +void publish_adc_states(); \ No newline at end of file diff --git a/src/user_code.hpp b/src/user_code.hpp index 0e04793..b6206e2 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -13,7 +13,7 @@ //#define OVERCLOCK_FM2 // Enable Software Module(s) -#define ENABLE_INTERNAL_LCD +//#define ENABLE_INTERNAL_LCD #define ENABLE_IR_MODULE #define ENABLE_CLIMATE_MODULE // Require IR Module #define ENABLE_ANALOG_MODULE