From 3661a7d30527fefbd29185b876d14257cb22b3d9 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 8 Aug 2019 16:46:07 +0200 Subject: [PATCH] mqtt publishf --- src/main.cpp | 24 ++++++++++++------------ src/network/XD0MQTT.cpp | 21 +++++++++++++++++++-- src/network/XD0MQTT.h | 4 +++- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cd0d4c7..5f5c547 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -503,16 +503,16 @@ void sendValues() { String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity"); String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure"); - mqtt.publish(topic_temperature.c_str(), sensor_readings.temperature, "%.2f"); + mqtt.publishf(topic_temperature.c_str(), "%.2f", sensor_readings.temperature); delay(10); - mqtt.publish(topic_humidity.c_str(), sensor_readings.humidity, "%.2f"); + mqtt.publishf(topic_humidity.c_str(), "%.2f", sensor_readings.humidity); delay(10); - mqtt.publish(topic_pressure.c_str(), sensor_readings.pressure, "%.2f"); + mqtt.publishf(topic_pressure.c_str(), "%.2f", sensor_readings.pressure); delay(10); } if (bme680_active) { String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc"); - mqtt.publish(topic_voc.c_str(), sensor_readings.voc / 1000.0F, "%.2f"); + mqtt.publishf(topic_voc.c_str(), "%.2f", sensor_readings.voc / 1000.0F); delay(10); } if (!bme280_active && !bme680_active) { @@ -527,35 +527,35 @@ void sendValues() { String topic_uvi = String("thomas/sensor/") + ota.getMAC() + String("/uvi"); String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva"); String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb"); - mqtt.publish(topic_uvi.c_str(), sensor_readings.uvi, "%.2f"); - mqtt.publish(topic_uva.c_str(), sensor_readings.uva, "%.2f"); - mqtt.publish(topic_uvb.c_str(), sensor_readings.uvb, "%.2f"); + mqtt.publishf(topic_uvi.c_str(), "%.2f", sensor_readings.uvi); + mqtt.publishf(topic_uva.c_str(), "%.2f", sensor_readings.uva); + mqtt.publishf(topic_uvb.c_str(), "%.2f", sensor_readings.uvb); delay(10); } if (light_active) { String topic_lux = String("thomas/sensor/") + ota.getMAC() + String("/lux"); - mqtt.publish(topic_lux.c_str(), sensor_readings.lux, "%.2f"); + mqtt.publishf(topic_lux.c_str(), "%.2f", sensor_readings.lux); delay(10); } if (sds_active) { String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10"); String topic_pm25 = String("thomas/sensor/") + ota.getMAC() + String("/pm25"); - mqtt.publish(topic_pm10.c_str(), sensor_readings.pm10, "%.2f"); - mqtt.publish(topic_pm25.c_str(), sensor_readings.pm25, "%.2f"); + mqtt.publishf(topic_pm10.c_str(), "%.2f", sensor_readings.pm10); + mqtt.publishf(topic_pm25.c_str(), "%.2f", sensor_readings.pm25); delay(10); } { String topic_battery = String("thomas/sensor/") + ota.getMAC() + String("/battery"); - mqtt.publish(topic_battery.c_str(), (sensor_readings.battery/4096.0)*2*3.42, "%.2f"); + mqtt.publishf(topic_battery.c_str(), "%.2f", (sensor_readings.battery/4096.0)*2*3.42); delay(10); } { String topic_rssi = String("thomas/sensor/") + ota.getMAC() + String("/rssi"); - mqtt.publish(topic_rssi.c_str(), sensor_readings.rssi, "%d"); + mqtt.publishf(topic_rssi.c_str(), "%d", sensor_readings.rssi); delay(10); } diff --git a/src/network/XD0MQTT.cpp b/src/network/XD0MQTT.cpp index 65e2400..339ad12 100644 --- a/src/network/XD0MQTT.cpp +++ b/src/network/XD0MQTT.cpp @@ -122,9 +122,26 @@ bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos, int ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); } -bool XD0MQTT::publish(const char* topic, const float data, const char* format, int qos, int retain) { +bool XD0MQTT::publishf(const char* topic, const char* format, ...) { + va_list argptr; + va_start(argptr, format); + bool ret = publishf2(topic, 1, 0, format, argptr); + va_end(argptr); + return ret; + +} + +bool XD0MQTT::publishf2(const char* topic, int qos, int retain, const char* format, ...) { + va_list argptr; + va_start(argptr, format); + bool ret = publishf2(topic, qos, retain, format, argptr); + va_end(argptr); + return ret; +} + +bool XD0MQTT::publishf2(const char* topic, int qos, int retain, const char* format, va_list argp) { char data_str[64]; - sprintf(data_str, format, data); + vsprintf(data_str, format, argp); int msg_id = esp_mqtt_client_publish(client, topic, data_str, strlen(data_str), qos, retain); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); } diff --git a/src/network/XD0MQTT.h b/src/network/XD0MQTT.h index 54d64a2..1f0fba5 100644 --- a/src/network/XD0MQTT.h +++ b/src/network/XD0MQTT.h @@ -30,7 +30,9 @@ class XD0MQTT { XD0MQTT(void); bool begin(void); bool publish(const char* topic, const char* data, int len, int qos=1, int retain=0); - bool publish(const char* topic, const float data, const char* format, int qos=1, int retain=0); + bool publishf(const char* topic, const char* format, ...); + bool publishf2(const char* topic, int qos, int retain, const char* format, ...); + bool publishf2(const char* topic, int qos, int retain, const char* format, va_list argp); bool subscribe(const char* topic, const cb_t &cb, int qos=1); bool unsubscribe(const char* topic); esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event);