update state publishing

This commit is contained in:
Siwat Sirichai 2023-11-30 18:53:01 +07:00
parent d2610b983f
commit 549500822c
2 changed files with 44 additions and 0 deletions

View File

@ -579,6 +579,9 @@ void mqtt_connect()
publish_ac_state(); publish_ac_state();
#endif #endif
mqtt_connected_user_callback(); mqtt_connected_user_callback();
publish_dac_states();
publish_dac_values();
publish_all_adc();
standalone = false; standalone = false;
ESPMega_updateTimeFromNTP(); ESPMega_updateTimeFromNTP();
} }
@ -1004,6 +1007,9 @@ void state_request_callback()
publish_env_state(); publish_env_state();
#endif #endif
user_state_request_callback(); user_state_request_callback();
publish_all_adc();
publish_dac_states();
publish_dac_values();
} }
#ifdef ENABLE_IR_MODULE #ifdef ENABLE_IR_MODULE
@ -1929,6 +1935,22 @@ void publish_all_adc()
publish_adc(i); 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. * @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; dac_values[id] = value;
ESPMega_dacWrite(id, dac_values[id] * dac_states[id]); ESPMega_dacWrite(id, dac_values[id] * dac_states[id]);
ESPMega_FRAM.write16(EEPROM_ADDRESS_DAC_VALUE + id * 2, dac_values[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; dac_states[id] = state;
ESPMega_dacWrite(id, dac_values[id] * dac_states[id]); ESPMega_dacWrite(id, dac_values[id] * dac_states[id]);
ESPMega_FRAM.write8(EEPROM_ADDRESS_DAC_STATE + 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);
}
} }
/** /**

View File

@ -147,3 +147,7 @@ void dac_set_value(int id, int value);
void dac_set_state(int id, bool state); 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_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 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();