diff --git a/examples/BresserWeatherSensorMQTTWifiMgr/BresserWeatherSensorMQTTWifiMgr.ino b/examples/BresserWeatherSensorMQTTWifiMgr/BresserWeatherSensorMQTTWifiMgr.ino index f7be5eb0..7ed787ec 100644 --- a/examples/BresserWeatherSensorMQTTWifiMgr/BresserWeatherSensorMQTTWifiMgr.ino +++ b/examples/BresserWeatherSensorMQTTWifiMgr/BresserWeatherSensorMQTTWifiMgr.ino @@ -72,6 +72,7 @@ // 20230619 Created from BresserWeatherSensorMQTT // 20230709 Added lightning sensor // 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 @@ // RTC Memory Address for the DoubleResetDetector to use #define DRD_ADDRESS 0 +// 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. @@ -166,6 +177,12 @@ #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[] = "BresserWeatherSensorMQTTWifiMgr"; // Map sensor IDs to Names @@ -595,7 +612,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); @@ -604,15 +621,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]; @@ -621,24 +638,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);