feat: changed sensor value format pushed via MQTT client

This commit is contained in:
SylvanArnold
2025-04-29 10:56:59 +02:00
committed by Sylvan Arnold
parent 6cd510e749
commit 7b1b986d2e
2 changed files with 15 additions and 29 deletions

View File

@@ -38,8 +38,7 @@
#define TOPIC_NAME_BATTERY_PERCENTAGE "homeassistant/sensor/powerwall_charge/state"
#define TOPIC_NAME_CHARGER_CHARGING_POWER "home/charger/power"
#elif MQTT_CLIENT_IS_SENSOR
#define TOPIC_NAME_SENSOR_TEMPERATURE "home/roof/temperature"
#define TOPIC_NAME_SENSOR_HUMIDITY "home/roof/humidity"
#define TOPIC_NAME_SENSORS_UPDATE "sylvan/Home/PicoSensor/update" /*<user>/<room>/<device>/update*/
#endif
typedef enum topic_ID_e {
@@ -52,8 +51,7 @@ typedef enum topic_ID_e {
Topic_ID_Battery_Percentage,/* battery level percentage */
Topic_ID_Charging_Power, /* actual charging power */
#elif MQTT_CLIENT_IS_SENSOR
Topic_ID_Sensor_Temperature,
Topic_ID_Sensor_Humidity,
Topic_ID_Sensor_Update,
#endif
} topic_ID_e;
@@ -115,23 +113,15 @@ int MqttClient_Publish_SensorValues(float temperature, float humidity) {
if (mqtt.doLogging) {
McuLog_trace("publish t:%f h:%f", temperature, humidity);
}
McuUtility_strcpy(buf, sizeof(buf), (unsigned char*)"{\"temperature\": ");
McuUtility_strcpy(buf, sizeof(buf), (unsigned char*)"{\"values\": {\"temperature\": ");
McuUtility_strcatNumFloat(buf, sizeof(buf), temperature, 2);
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"unit\": \"°C\"}");
res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSOR_TEMPERATURE, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL);
if (res!=ERR_OK) {
McuLog_error("Failed temperature mqtt_publish: %d", res);
(void)MqttClient_Disconnect(); /* try disconnect and connect again */
(void)MqttClient_Connect();
return res;
}
McuUtility_strcpy(buf, sizeof(buf), (unsigned char*)"{\"humidity\": ");
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"humidity\": ");
McuUtility_strcatNumFloat(buf, sizeof(buf), humidity, 2);
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"unit\": \"%\"}");
res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSOR_HUMIDITY, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL);
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)"}}");
res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSORS_UPDATE, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL);
if (res != ERR_OK) {
McuLog_error("Failed humidity mqtt_publish: %d", res);
McuLog_error("Failed sensor values mqtt_publish: %d", res);
(void)MqttClient_Disconnect(); /* try disconnect and connect again */
(void)MqttClient_Connect();
return res;
@@ -285,10 +275,8 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f
McuLog_trace("bat%%: %s%%", buf);
}
#elif MQTT_CLIENT_IS_SENSOR
if (mqtt.in_pub_ID == Topic_ID_Sensor_Temperature) {
McuLog_trace("Temperature");
} else if (mqtt.in_pub_ID == Topic_ID_Sensor_Humidity) {
McuLog_trace("Humidity");
if (mqtt.in_pub_ID == Topic_ID_Sensor_Update) {
McuLog_trace("Sensor update");
#endif
} else {
McuLog_trace("mqtt_incoming_data_cb: Ignoring payload...");
@@ -316,10 +304,8 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len
} else if (McuUtility_strcmp(topic, TOPIC_NAME_CHARGER_CHARGING_POWER)==0) {
mqtt.in_pub_ID = Topic_ID_Charging_Power;
#elif MQTT_CLIENT_IS_SENSOR
if (McuUtility_strcmp(topic, TOPIC_NAME_SENSOR_TEMPERATURE)==0) {
mqtt.in_pub_ID = Topic_ID_Sensor_Temperature;
} else if (McuUtility_strcmp(topic, TOPIC_NAME_SENSOR_HUMIDITY)==0) {
mqtt.in_pub_ID = Topic_ID_Sensor_Humidity;
if (McuUtility_strcmp(topic, TOPIC_NAME_SENSORS_UPDATE)==0) {
mqtt.in_pub_ID = Topic_ID_Sensor_Update;
#endif
} else { /* unknown */
McuLog_trace("MQTT client \"%s\" publish cb: topic %s, len %d", client_info->client_id, topic, (int)tot_len);

View File

@@ -24,7 +24,7 @@ extern "C" {
/* platform configuration macros: set to 1 to enable functionality */
#define PL_CONFIG_USE_LEDS (1) /* if using LEDs */
#define PL_CONFIG_USE_APP_TASK (1) /* if using app task */
#define PL_CONFIG_USE_BLINKY (1 && PL_CONFIG_USE_LEDS) /*!< if using blinky */
#define PL_CONFIG_USE_BLINKY (0 && PL_CONFIG_USE_LEDS) /*!< if using blinky */
#define PL_CONFIG_USE_BUTTONS (1) /* using nav switch buttons */
#define PL_CONFIG_USE_BUTTONS_IRQ (1 && !(McuLib_CONFIG_CPU_IS_ESP32 && PL_CONFIG_USE_WIFI)) /* if using button interrupts */
/* Note: on ESP32, there is a hardware bug, triggering interrupts on GPIO36 (right) and GPIO39 (down).