feat: changed sensor value format pushed via MQTT client
This commit is contained in:
committed by
Sylvan Arnold
parent
6cd510e749
commit
7b1b986d2e
@@ -38,8 +38,7 @@
|
|||||||
#define TOPIC_NAME_BATTERY_PERCENTAGE "homeassistant/sensor/powerwall_charge/state"
|
#define TOPIC_NAME_BATTERY_PERCENTAGE "homeassistant/sensor/powerwall_charge/state"
|
||||||
#define TOPIC_NAME_CHARGER_CHARGING_POWER "home/charger/power"
|
#define TOPIC_NAME_CHARGER_CHARGING_POWER "home/charger/power"
|
||||||
#elif MQTT_CLIENT_IS_SENSOR
|
#elif MQTT_CLIENT_IS_SENSOR
|
||||||
#define TOPIC_NAME_SENSOR_TEMPERATURE "home/roof/temperature"
|
#define TOPIC_NAME_SENSORS_UPDATE "sylvan/Home/PicoSensor/update" /*<user>/<room>/<device>/update*/
|
||||||
#define TOPIC_NAME_SENSOR_HUMIDITY "home/roof/humidity"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum topic_ID_e {
|
typedef enum topic_ID_e {
|
||||||
@@ -52,8 +51,7 @@ typedef enum topic_ID_e {
|
|||||||
Topic_ID_Battery_Percentage,/* battery level percentage */
|
Topic_ID_Battery_Percentage,/* battery level percentage */
|
||||||
Topic_ID_Charging_Power, /* actual charging power */
|
Topic_ID_Charging_Power, /* actual charging power */
|
||||||
#elif MQTT_CLIENT_IS_SENSOR
|
#elif MQTT_CLIENT_IS_SENSOR
|
||||||
Topic_ID_Sensor_Temperature,
|
Topic_ID_Sensor_Update,
|
||||||
Topic_ID_Sensor_Humidity,
|
|
||||||
#endif
|
#endif
|
||||||
} topic_ID_e;
|
} topic_ID_e;
|
||||||
|
|
||||||
@@ -111,27 +109,19 @@ int MqttClient_Publish_SensorValues(float temperature, float humidity) {
|
|||||||
return ERR_DISABLED;
|
return ERR_DISABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mqtt.mqtt_client!=NULL) { /* connected? */
|
if (mqtt.mqtt_client != NULL) { /* connected? */
|
||||||
if (mqtt.doLogging) {
|
if (mqtt.doLogging) {
|
||||||
McuLog_trace("publish t:%f h:%f", temperature, humidity);
|
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_strcatNumFloat(buf, sizeof(buf), temperature, 2);
|
||||||
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"unit\": \"°C\"}");
|
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"humidity\": ");
|
||||||
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_strcatNumFloat(buf, sizeof(buf), humidity, 2);
|
McuUtility_strcatNumFloat(buf, sizeof(buf), humidity, 2);
|
||||||
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)", \"unit\": \"%\"}");
|
McuUtility_strcat(buf, sizeof(buf), (unsigned char*)"}}");
|
||||||
res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSOR_HUMIDITY, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL);
|
|
||||||
if (res!=ERR_OK) {
|
res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSORS_UPDATE, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL);
|
||||||
McuLog_error("Failed humidity mqtt_publish: %d", res);
|
if (res != ERR_OK) {
|
||||||
|
McuLog_error("Failed sensor values mqtt_publish: %d", res);
|
||||||
(void)MqttClient_Disconnect(); /* try disconnect and connect again */
|
(void)MqttClient_Disconnect(); /* try disconnect and connect again */
|
||||||
(void)MqttClient_Connect();
|
(void)MqttClient_Connect();
|
||||||
return res;
|
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);
|
McuLog_trace("bat%%: %s%%", buf);
|
||||||
}
|
}
|
||||||
#elif MQTT_CLIENT_IS_SENSOR
|
#elif MQTT_CLIENT_IS_SENSOR
|
||||||
if (mqtt.in_pub_ID == Topic_ID_Sensor_Temperature) {
|
if (mqtt.in_pub_ID == Topic_ID_Sensor_Update) {
|
||||||
McuLog_trace("Temperature");
|
McuLog_trace("Sensor update");
|
||||||
} else if (mqtt.in_pub_ID == Topic_ID_Sensor_Humidity) {
|
|
||||||
McuLog_trace("Humidity");
|
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
McuLog_trace("mqtt_incoming_data_cb: Ignoring payload...");
|
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) {
|
} else if (McuUtility_strcmp(topic, TOPIC_NAME_CHARGER_CHARGING_POWER)==0) {
|
||||||
mqtt.in_pub_ID = Topic_ID_Charging_Power;
|
mqtt.in_pub_ID = Topic_ID_Charging_Power;
|
||||||
#elif MQTT_CLIENT_IS_SENSOR
|
#elif MQTT_CLIENT_IS_SENSOR
|
||||||
if (McuUtility_strcmp(topic, TOPIC_NAME_SENSOR_TEMPERATURE)==0) {
|
if (McuUtility_strcmp(topic, TOPIC_NAME_SENSORS_UPDATE)==0) {
|
||||||
mqtt.in_pub_ID = Topic_ID_Sensor_Temperature;
|
mqtt.in_pub_ID = Topic_ID_Sensor_Update;
|
||||||
} else if (McuUtility_strcmp(topic, TOPIC_NAME_SENSOR_HUMIDITY)==0) {
|
|
||||||
mqtt.in_pub_ID = Topic_ID_Sensor_Humidity;
|
|
||||||
#endif
|
#endif
|
||||||
} else { /* unknown */
|
} else { /* unknown */
|
||||||
McuLog_trace("MQTT client \"%s\" publish cb: topic %s, len %d", client_info->client_id, topic, (int)tot_len);
|
McuLog_trace("MQTT client \"%s\" publish cb: topic %s, len %d", client_info->client_id, topic, (int)tot_len);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ extern "C" {
|
|||||||
/* platform configuration macros: set to 1 to enable functionality */
|
/* platform configuration macros: set to 1 to enable functionality */
|
||||||
#define PL_CONFIG_USE_LEDS (1) /* if using LEDs */
|
#define PL_CONFIG_USE_LEDS (1) /* if using LEDs */
|
||||||
#define PL_CONFIG_USE_APP_TASK (1) /* if using app task */
|
#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 (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 */
|
#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).
|
/* Note: on ESP32, there is a hardware bug, triggering interrupts on GPIO36 (right) and GPIO39 (down).
|
||||||
|
|||||||
Reference in New Issue
Block a user