Added support some sensors

This commit is contained in:
2024-06-24 18:24:23 +03:00
parent 1e6a634872
commit 7f730f4a48
2 changed files with 44 additions and 2 deletions

View File

@ -1668,13 +1668,23 @@ void zh_espnow_sensor_send_mqtt_json_hardware_config_message(const zh_espnow_dat
void zh_espnow_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config)
{
char *voltage = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(voltage, 0, 317);
char *temperature = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(temperature, 0, 317);
char *humidity = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(humidity, 0, 317);
char *atmospheric_pressure = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(atmospheric_pressure, 0, 317);
char *aqi = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(aqi, 0, 317);
char *illuminance = (char *)heap_caps_malloc(317, MALLOC_CAP_8BIT);
memset(illuminance, 0, 317);
zh_json_t json = {0};
char buffer[512] = {0};
zh_json_init(&json);
sprintf(voltage, "%f", device_data->payload_data.status_message.sensor_status_message.voltage);
zh_json_add(&json, "voltage", voltage);
switch (device_data->payload_data.status_message.sensor_status_message.sensor_type)
{
case HAST_DS18B20:
@ -1683,10 +1693,38 @@ void zh_espnow_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *devi
break;
case HAST_DHT:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "temperature", temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "humidity", humidity);
break;
case HAST_BH1750:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.illuminance);
zh_json_add(&json, "illuminance", illuminance);
break;
case HAST_BMP280:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
zh_json_add(&json, "temperature", temperature);
sprintf(atmospheric_pressure, "%f", device_data->payload_data.status_message.sensor_status_message.atmospheric_pressure);
zh_json_add(&json, "atmospheric_pressure", atmospheric_pressure);
break;
case HAST_BME280:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
zh_json_add(&json, "temperature", temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "humidity", humidity);
sprintf(atmospheric_pressure, "%f", device_data->payload_data.status_message.sensor_status_message.atmospheric_pressure);
zh_json_add(&json, "atmospheric_pressure", atmospheric_pressure);
break;
case HAST_BME680:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
zh_json_add(&json, "temperature", temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "humidity", humidity);
sprintf(atmospheric_pressure, "%f", device_data->payload_data.status_message.sensor_status_message.atmospheric_pressure);
zh_json_add(&json, "atmospheric_pressure", atmospheric_pressure);
sprintf(aqi, "%f", device_data->payload_data.status_message.sensor_status_message.aqi);
zh_json_add(&json, "aqi", aqi);
break;
default:
break;
}
@ -1696,8 +1734,12 @@ void zh_espnow_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *devi
memset(topic, 0, strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(device_data->device_type)) + 31);
sprintf(topic, "%s/%s/" MAC_STR "/state", gateway_config->software_config.mqtt_topic_prefix, zh_get_device_type_value_name(device_data->device_type), MAC2STR(device_mac));
esp_mqtt_client_publish(gateway_config->mqtt_client, topic, buffer, 0, 2, true);
heap_caps_free(voltage);
heap_caps_free(temperature);
heap_caps_free(humidity);
heap_caps_free(atmospheric_pressure);
heap_caps_free(aqi);
heap_caps_free(illuminance);
heap_caps_free(topic);
}