Skip to content

Commit

Permalink
Update BresserWeatherSensorMQTT.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Jul 10, 2023
1 parent fea3cbd commit ceccd1d
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down Expand Up @@ -126,15 +127,23 @@
#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_

// 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) )
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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);
Expand Down

0 comments on commit ceccd1d

Please sign in to comment.