diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index cf087c2..e8713c9 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -579,9 +579,12 @@ void mqtt_connect() publish_ac_state(); #endif mqtt_connected_user_callback(); +#ifdef ENABLE_ANALOG_MODULE publish_dac_states(); publish_dac_values(); publish_adc_values(); + publish_adc_states(); +#endif standalone = false; ESPMega_updateTimeFromNTP(); } @@ -1007,9 +1010,12 @@ void state_request_callback() publish_env_state(); #endif user_state_request_callback(); +#ifdef ENABLE_ANALOG_MODULE + publish_adc_states(); publish_adc_values(); publish_dac_states(); publish_dac_values(); +#endif } #ifdef ENABLE_IR_MODULE @@ -1846,7 +1852,6 @@ 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. @@ -1860,7 +1865,6 @@ 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) @@ -1880,7 +1884,7 @@ void publish_adc_states() /** * @brief Updates the ADC value for the specified ID if ADC reporting is enabled. - * + * * @param id The ID of the ADC channel. */ void adc_update(int id) @@ -1889,12 +1893,11 @@ void adc_update(int id) { adc_values[id] = ESPMega_analogRead(id); } - } /** * @brief Updates the ADC value for the specified ID, do so even if reporting is disabled.. - * + * * @param id The ID of the ADC pin. */ void adc_update_force(int id) @@ -1902,12 +1905,11 @@ void adc_update_force(int id) adc_values[id] = ESPMega_analogRead(id); } - /** * @brief Updates all ADC channels. - * + * * This function updates all ADC channels by calling the `adc_update` function for each channel. - * + * * @return void */ void adc_update_all() @@ -1920,7 +1922,7 @@ void adc_update_all() /** * @brief Performs ADC loop operations. - * + * * This function updates all ADC values and publishes them. */ void adc_loop() @@ -1931,7 +1933,7 @@ void adc_loop() /** * Publishes the ADC value to the MQTT broker. - * + * * @param id The ID of the ADC channel. */ void publish_adc_value(int id) @@ -1964,7 +1966,8 @@ void publish_dac_state(int id) mqtt.publish(DAC_STATE_TOPIC, dac_states[id] ? "on" : "off"); } -void publish_dac_value(int id) { +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]; @@ -1974,10 +1977,10 @@ void publish_dac_value(int id) { /** * @brief Retrieves the ADC value for the specified ID. - * + * * This function checks if the ADC report is enabled for the given ID. If not, it forces an update. * It then returns the ADC value for the specified ID. - * + * * @param id The ID of the ADC channel. * @return The ADC value for the specified ID. */ @@ -1990,10 +1993,10 @@ uint16_t get_adc_value(int id) /** * @brief Sets the state of an ADC based on the received MQTT message. - * + * * This function is called when an MQTT message is received to set the state of an ADC (Analog-to-Digital Converter). * The function extracts the ADC ID from the topic and enables or disables the ADC based on the payload value. - * + * * @param topic The topic of the MQTT message. * @param topic_length The length of the topic. * @param payload The payload of the MQTT message. @@ -2001,11 +2004,9 @@ uint16_t get_adc_value(int id) */ void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length) { - 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); @@ -2018,12 +2019,12 @@ void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, un /** * @brief Sets the value of a DAC channel. - * + * * This function sets the value of a DAC channel specified by the `id` parameter. * The `value` parameter represents the desired value for the DAC channel. * The function updates the internal DAC value array, writes the value to the DAC, * and also stores the value in the FRAM memory. - * + * * @param id The ID of the DAC channel. * @param value The desired value for the DAC channel. */ @@ -2037,10 +2038,10 @@ void dac_set_value(int id, int value) /** * @brief Sets the state of a DAC channel. - * + * * This function updates the state of a DAC channel and writes the new state to the DAC output. * It also saves the state to the EEPROM for persistence across power cycles. - * + * * @param id The ID of the DAC channel. * @param state The new state of the DAC channel. */ @@ -2086,10 +2087,10 @@ void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, un } /** * @brief Callback function for setting the state of the DAC. - * + * * This function is called when a message is received on the specified topic. * It takes the topic, topic length, payload, and payload length as parameters. - * + * * @param topic The topic of the received message. * @param topic_length The length of the topic string. * @param payload The payload of the received message.