diff --git a/TSM_PicoW_Sensor/src/mqtt_client.c b/TSM_PicoW_Sensor/src/mqtt_client.c index 638119a..41fb4a5 100644 --- a/TSM_PicoW_Sensor/src/mqtt_client.c +++ b/TSM_PicoW_Sensor/src/mqtt_client.c @@ -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" /*///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; @@ -111,27 +109,19 @@ int MqttClient_Publish_SensorValues(float temperature, float humidity) { return ERR_DISABLED; } - if (mqtt.mqtt_client!=NULL) { /* connected? */ + if (mqtt.mqtt_client != NULL) { /* connected? */ 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); - if (res!=ERR_OK) { - McuLog_error("Failed humidity mqtt_publish: %d", res); + 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 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); diff --git a/TSM_PicoW_Sensor/src/platform.h b/TSM_PicoW_Sensor/src/platform.h index aae7ba0..244c0c0 100644 --- a/TSM_PicoW_Sensor/src/platform.h +++ b/TSM_PicoW_Sensor/src/platform.h @@ -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).