From ceccd1d0e2392a575943e64e5716727e6fe4ec1a Mon Sep 17 00:00:00 2001 From: Matthias Prinke <83612361+matthias-bs@users.noreply.github.com> Date: Mon, 10 Jul 2023 13:45:03 +0200 Subject: [PATCH] Update BresserWeatherSensorMQTT.ino --- .../BresserWeatherSensorMQTT.ino | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino b/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino index a6e0c1e7..e74c98bb 100644 --- a/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino +++ b/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino @@ -87,6 +87,7 @@ // 20230114 Fixed rain gauge update // 20230124 Improved WiFi connection robustness // 20230708 Changed MQTT payload and topic from char[] to String +// 20230710 Added optional JSON output of floating point values as strings // // ToDo: // @@ -126,6 +127,16 @@ #define USE_SECUREWIFI // use secure WIFI //#define USE_WIFI // use non-secure WIFI +// See +// https://stackoverflow.com/questions/19554972/json-standard-floating-point-numbers +// and +// https://stackoverflow.com/questions/35709595/why-would-you-use-a-string-in-json-to-represent-a-decimal-number +// +// Summary: +// A string representation of a float (e.g. "temp_c":"21.5") is recommended if the value shall displayed with the specified number of decimals. +// Otherwise the float value can be output as a numerical value (e.g. "temp_c":21.5). +// +//#define JSON_FLOAT_AS_STRING // Enable to debug MQTT connection; will generate synthetic sensor data. //#define _DEBUG_MQTT_ @@ -133,8 +144,6 @@ // Generate sensor data to test collecting data from multiple sources //#define GEN_SENSOR_DATA -int const num_sensors = 1; - // END User specific configuration #if ( defined(USE_SECUREWIFI) && defined(USE_WIFI) ) || ( !defined(USE_SECUREWIFI) && !defined(USE_WIFI) ) @@ -161,6 +170,12 @@ int const num_sensors = 1; #include "WeatherUtils.h" #include "RainGauge.h" +#if defined(JSON_FLOAT_AS_STRING) + #define JSON_FLOAT(x) String("\"") + x + String("\"") +#else + #define JSON_FLOAT(x) x +#endif + const char sketch_id[] = "BresserWeatherSensorMQTT 20230709"; // Map sensor IDs to Names @@ -433,7 +448,7 @@ void publishWeatherdata(bool complete) } // Example: - // {"ch":0,"battery_ok":1,"humidity":44,"wind_gust":"1.2","wind_avg":"1.2","wind_dir":150,"rain":146} + // {"ch":0,"battery_ok":1,"humidity":44,"wind_gust":1.2,"wind_avg":1.2,"wind_dir":150,"rain":146} mqtt_payload = "{"; mqtt_payload2 = "{"; mqtt_payload += String("\"id\":") + String(weatherSensor.sensor[i].sensor_id); @@ -442,15 +457,15 @@ void publishWeatherdata(bool complete) #endif mqtt_payload += String(",\"battery_ok\":") + (weatherSensor.sensor[i].battery_ok ? String("1") : String("0")); if (weatherSensor.sensor[i].temp_ok || complete) { - mqtt_payload += String(",\"temp_c\":") + String(weatherSensor.sensor[i].temp_c, 1); + mqtt_payload += String(",\"temp_c\":") + JSON_FLOAT(String(weatherSensor.sensor[i].temp_c, 1)); } if (weatherSensor.sensor[i].humidity_ok || complete) { mqtt_payload += String(",\"humidity\":") + String(weatherSensor.sensor[i].humidity); } if (weatherSensor.sensor[i].wind_ok || complete) { - mqtt_payload += String(",\"wind_gust\":") + String(weatherSensor.sensor[i].wind_gust_meter_sec, 1); - mqtt_payload += String(",\"wind_avg\":") + String(weatherSensor.sensor[i].wind_avg_meter_sec, 1); - mqtt_payload += String(",\"wind_dir\":") + String(weatherSensor.sensor[i].wind_direction_deg, 1); + mqtt_payload += String(",\"wind_gust\":") + JSON_FLOAT(String(weatherSensor.sensor[i].wind_gust_meter_sec, 1)); + mqtt_payload += String(",\"wind_avg\":") + JSON_FLOAT(String(weatherSensor.sensor[i].wind_avg_meter_sec, 1)); + mqtt_payload += String(",\"wind_dir\":") + JSON_FLOAT(String(weatherSensor.sensor[i].wind_direction_deg, 1)); } if (weatherSensor.sensor[i].wind_ok) { char buf[4]; @@ -459,24 +474,25 @@ void publishWeatherdata(bool complete) mqtt_payload2 += String(",\"wind_avg_bft\":") + String(windspeed_ms_to_bft(weatherSensor.sensor[i].wind_avg_meter_sec)); } if ((weatherSensor.sensor[i].temp_ok) && (weatherSensor.sensor[i].humidity_ok)) { - mqtt_payload2 += String(",\"dewpoint_c\":") + String(calcdewpoint(weatherSensor.sensor[i].temp_c, weatherSensor.sensor[i].humidity), 1); + mqtt_payload2 += String(",\"dewpoint_c\":") + + JSON_FLOAT(String(calcdewpoint(weatherSensor.sensor[i].temp_c, weatherSensor.sensor[i].humidity), 1)); if (weatherSensor.sensor[i].wind_ok) { mqtt_payload2 += String(",\"perceived_temp_c\":") - + String(perceived_temperature(weatherSensor.sensor[i].temp_c, weatherSensor.sensor[i].wind_avg_meter_sec, weatherSensor.sensor[i].humidity), 1); + + JSON_FLOAT(String(perceived_temperature(weatherSensor.sensor[i].temp_c, weatherSensor.sensor[i].wind_avg_meter_sec, weatherSensor.sensor[i].humidity), 1)); } } if (weatherSensor.sensor[i].uv_ok || complete) { - mqtt_payload += String(",\"uv\":") + String(weatherSensor.sensor[i].uv, 1); + mqtt_payload += String(",\"uv\":") + JSON_FLOAT(String(weatherSensor.sensor[i].uv, 1); } if (weatherSensor.sensor[i].light_ok || complete) { - mqtt_payload += String(",\"light_klx\":") + String(weatherSensor.sensor[i].light_klx, 1); + mqtt_payload += String(",\"light_klx\":") + JSON_FLOAT(String(weatherSensor.sensor[i].light_klx, 1)); } if (weatherSensor.sensor[i].rain_ok || complete) { - mqtt_payload += String(",\"rain\":") + String(weatherSensor.sensor[i].rain_mm, 1); - mqtt_payload += String(",\"rain_d\":") + String(rainGauge.currentDay(), 1); - mqtt_payload += String(",\"rain_w\":") + String(rainGauge.currentWeek(), 1); - mqtt_payload += String(",\"rain_m\":") + String(rainGauge.currentMonth(), 1); + mqtt_payload += String(",\"rain\":") + JSON_FLOAT(String(weatherSensor.sensor[i].rain_mm, 1)); + mqtt_payload += String(",\"rain_d\":") + JSON_FLOAT(String(rainGauge.currentDay(), 1)); + mqtt_payload += String(",\"rain_w\":") + JSON_FLOAT(String(rainGauge.currentWeek(), 1)); + mqtt_payload += String(",\"rain_m\":") + JSON_FLOAT(String(rainGauge.currentMonth(), 1)); } if (weatherSensor.sensor[i].moisture_ok || complete) { mqtt_payload += String(",\"moisture\":") + String(weatherSensor.sensor[i].moisture);