fix adc reporting bug

This commit is contained in:
Siwat Sirichai 2023-11-30 19:44:05 +07:00
parent 549500822c
commit 62f774964d
3 changed files with 45 additions and 20 deletions

View File

@ -581,7 +581,7 @@ void mqtt_connect()
mqtt_connected_user_callback(); mqtt_connected_user_callback();
publish_dac_states(); publish_dac_states();
publish_dac_values(); publish_dac_values();
publish_all_adc(); publish_adc_values();
standalone = false; standalone = false;
ESPMega_updateTimeFromNTP(); ESPMega_updateTimeFromNTP();
} }
@ -1007,7 +1007,7 @@ void state_request_callback()
publish_env_state(); publish_env_state();
#endif #endif
user_state_request_callback(); user_state_request_callback();
publish_all_adc(); publish_adc_values();
publish_dac_states(); publish_dac_states();
publish_dac_values(); publish_dac_values();
} }
@ -1845,6 +1845,8 @@ void enable_adc(int id)
{ {
adc_report_enable[id] = true; adc_report_enable[id] = true;
ESPMega_FRAM.write8(EEPROM_ADDRESS_ADC_REPORT_STATE + id, 1); 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. * @brief Disables the ADC reporting for the specified ID.
@ -1857,7 +1859,25 @@ void disable_adc(int id)
{ {
adc_report_enable[id] = false; adc_report_enable[id] = false;
ESPMega_FRAM.write8(EEPROM_ADDRESS_ADC_REPORT_STATE + id, 0); 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. * @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); adc_values[id] = ESPMega_analogRead(id);
} }
} }
/** /**
@ -1905,7 +1926,7 @@ void adc_update_all()
void adc_loop() void adc_loop()
{ {
adc_update_all(); adc_update_all();
publish_all_adc(); publish_adc_values();
} }
/** /**
@ -1913,13 +1934,13 @@ void adc_loop()
* *
* @param id The ID of the ADC channel. * @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_REPORT_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0';
ADC_STATE_TOPIC[base_topic_length + 5] = (id % 10) + '0'; ADC_REPORT_TOPIC[base_topic_length + 5] = (id % 10) + '0';
char temp[6]; char temp[8];
itoa(adc_values[id], temp, DEC); 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 * This function iterates through all ADC channels and publishes the values
* of the enabled channels using the publish_adc() function. * 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++) for (int i = 0; i < ADC_COUNT; i++)
{ {
if (adc_report_enable[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) void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
int a = topic[4] - '0'; Serial.println(topic);
int b = topic[5] - '0'; int a = topic[5] - '0';
int b = topic[6] - '0';
int id = 10 * a + b; int id = 10 * a + b;
Serial.println("ID = "+String(id));
if (!strcmp(payload, "on")) if (!strcmp(payload, "on"))
{ {
enable_adc(id); 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) void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
int a = topic[4] - '0'; int a = topic[5] - '0';
int b = topic[5] - '0'; int b = topic[6] - '0';
int id = 10 * a + b; int id = 10 * a + b;
int value = atoi(payload); int value = atoi(payload);
dac_set_value(id, value); 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) void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
int a = topic[4] - '0'; int a = topic[5] - '0';
int b = topic[5] - '0'; int b = topic[6] - '0';
int id = 10 * a + b; int id = 10 * a + b;
if (!strcmp(payload, "on")) if (!strcmp(payload, "on"))
{ {

View File

@ -139,8 +139,8 @@ void adc_update(int id);
void adc_update_force(int id); void adc_update_force(int id);
void adc_update_all(); void adc_update_all();
void adc_loop(); void adc_loop();
void publish_adc(int id); void publish_adc_value(int id);
void publish_all_adc(); void publish_adc_values();
uint16_t get_adc_value(int id); 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 adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length);
void dac_set_value(int id, int value); 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_value(int id);
void publish_dac_state(int id); void publish_dac_state(int id);
void publish_dac_values(); void publish_dac_values();
void publish_dac_states(); void publish_dac_states();
void publish_adc_state(int id);
void publish_adc_states();

View File

@ -13,7 +13,7 @@
//#define OVERCLOCK_FM2 //#define OVERCLOCK_FM2
// Enable Software Module(s) // Enable Software Module(s)
#define ENABLE_INTERNAL_LCD //#define ENABLE_INTERNAL_LCD
#define ENABLE_IR_MODULE #define ENABLE_IR_MODULE
#define ENABLE_CLIMATE_MODULE // Require IR Module #define ENABLE_CLIMATE_MODULE // Require IR Module
#define ENABLE_ANALOG_MODULE #define ENABLE_ANALOG_MODULE