Skip to content

Commit

Permalink
Merge pull request #102 from matthias-bs/refactor-sensor-data
Browse files Browse the repository at this point in the history
Refactor sensor data
  • Loading branch information
matthias-bs committed Oct 30, 2023
2 parents 8fd74d2 + 127f0bb commit 59e45b6
Show file tree
Hide file tree
Showing 16 changed files with 2,468 additions and 2,689 deletions.
6 changes: 0 additions & 6 deletions .vscode/arduino.json

This file was deleted.

541 changes: 0 additions & 541 deletions .vscode/c_cpp_properties.json

This file was deleted.

175 changes: 82 additions & 93 deletions examples/BresserWeatherSensorBasic/BresserWeatherSensorBasic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include "WeatherSensor.h"


WeatherSensor weatherSensor;
WeatherSensor ws;


void setup() {
Expand All @@ -68,7 +68,7 @@ void setup() {

Serial.printf("Starting execution...\n");

weatherSensor.begin();
ws.begin();
}


Expand All @@ -78,109 +78,98 @@ void loop()
int const i=0;

// Clear all sensor data
weatherSensor.clearSlots();
ws.clearSlots();

// Tries to receive radio message (non-blocking) and to decode it.
// Timeout occurs after a small multiple of expected time-on-air.
int decode_status = weatherSensor.getMessage();
int decode_status = ws.getMessage();

if (decode_status == DECODE_OK) {
if (!weatherSensor.sensor[i].temp_ok && (weatherSensor.sensor[i].s_type == SENSOR_TYPE_LIGHTNING)) {
Serial.printf("Id: [%8X] Typ: [%X] Battery: [%s] ",
(unsigned int)weatherSensor.sensor[i].sensor_id,
weatherSensor.sensor[i].s_type,
weatherSensor.sensor[i].battery_ok ? "OK " : "Low");
Serial.printf("Lightning Counter: [%3d] ", weatherSensor.sensor[i].lightning_count);
if (weatherSensor.sensor[i].lightning_distance_km != 0) {
Serial.printf("Distance: [%2dkm] ", weatherSensor.sensor[i].lightning_distance_km);
} else {
Serial.printf("Distance: [----] ");
Serial.printf("Id: [%8X] Typ: [%X] Ch: [%d] St: [%d] Bat: [%-3s] RSSI: [%6.1fdBm] ",
(unsigned int)ws.sensor[i].sensor_id,
ws.sensor[i].s_type,
ws.sensor[i].chan,
ws.sensor[i].startup,
ws.sensor[i].battery_ok ? "OK " : "Low",
ws.sensor[i].rssi);

if (ws.sensor[i].s_type == SENSOR_TYPE_LIGHTNING) {
// Lightning Sensor
Serial.printf("Lightning Counter: [%3d] ", ws.sensor[i].lgt.strike_count);
if (ws.sensor[i].lgt.distance_km != 0) {
Serial.printf("Distance: [%2dkm] ", ws.sensor[i].lgt.distance_km);
} else {
Serial.printf("Distance: [----] ");
}
Serial.printf("unknown1: [0x%03X] ", ws.sensor[i].lgt.unknown1);
Serial.printf("unknown2: [0x%04X]\n", ws.sensor[i].lgt.unknown2);

}
Serial.printf("unknown1: [0x%03X] ", weatherSensor.sensor[i].lightning_unknown1);
Serial.printf("unknown2: [0x%04X] ", weatherSensor.sensor[i].lightning_unknown2);

} else if (weatherSensor.sensor[i].s_type == SENSOR_TYPE_LEAKAGE) {
Serial.printf("Id: [%8X] Typ: [%X] Battery: [%s] Ch: [%d] ",
(unsigned int)weatherSensor.sensor[i].sensor_id,
weatherSensor.sensor[i].s_type,
weatherSensor.sensor[i].battery_ok ? "OK " : "Low",
weatherSensor.sensor[i].chan
);
Serial.printf("Leakage: [%-5s] ", (weatherSensor.sensor[i].water_leakage_alarm) ? "ALARM" : "OK");
else if (ws.sensor[i].s_type == SENSOR_TYPE_LEAKAGE) {
// Water Leakage Sensor
Serial.printf("Leakage: [%-5s]\n", (ws.sensor[i].leak.alarm) ? "ALARM" : "OK");

} else if (weatherSensor.sensor[i].s_type == SENSOR_TYPE_AIR_PM) {
// Air Quality (Particle Matter) Sensor
Serial.printf("Id: [%8X] Typ: [%X] Battery: [%s] ",
(unsigned int)weatherSensor.sensor[i].sensor_id,
weatherSensor.sensor[i].s_type,
weatherSensor.sensor[i].battery_ok ? "OK " : "Low");
Serial.printf("Ch: [%d] ", weatherSensor.sensor[i].chan);
Serial.printf("PM2.5: [%uµg/m³] ", weatherSensor.sensor[i].aqs_pm_2_5);
Serial.printf("PM10: [%uµg/m³] ", weatherSensor.sensor[i].aqs_pm_10);
} else {
// Any other (weather-like) sensor is very similar
Serial.printf("Id: [%8X] Typ: [%X] Battery: [%s] ",
(unsigned int)weatherSensor.sensor[i].sensor_id,
weatherSensor.sensor[i].s_type,
weatherSensor.sensor[i].battery_ok ? "OK " : "Low");
#ifdef BRESSER_6_IN_1
Serial.printf("Ch: [%d] ", weatherSensor.sensor[i].chan);
#endif
if (weatherSensor.sensor[i].temp_ok) {
Serial.printf("Temp: [%5.1fC] ",
weatherSensor.sensor[i].temp_c);
} else {
Serial.printf("Temp: [---.-C] ");
}
if (weatherSensor.sensor[i].humidity_ok) {
Serial.printf("Hum: [%3d%%] ",
weatherSensor.sensor[i].humidity);
}
else {
Serial.printf("Hum: [---%%] ");
}
if (weatherSensor.sensor[i].wind_ok) {
Serial.printf("Wind max: [%4.1fm/s] Wind avg: [%4.1fm/s] Wind dir: [%5.1fdeg] ",
weatherSensor.sensor[i].wind_gust_meter_sec,
weatherSensor.sensor[i].wind_avg_meter_sec,
weatherSensor.sensor[i].wind_direction_deg);
} else {
Serial.printf("Wind max: [--.-m/s] Wind avg: [--.-m/s] Wind dir: [---.-deg] ");
else if (ws.sensor[i].s_type == SENSOR_TYPE_AIR_PM) {
// Air Quality (Particular Matter) Sensor
Serial.printf("PM2.5: [%uµg/m³] ", ws.sensor[i].pm.pm_2_5);
Serial.printf("PM10: [%uµg/m³]\n", ws.sensor[i].pm.pm_10);

}
if (weatherSensor.sensor[i].rain_ok) {
Serial.printf("Rain: [%7.1fmm] ",
weatherSensor.sensor[i].rain_mm);
else if (ws.sensor[i].s_type == SENSOR_TYPE_SOIL) {
Serial.printf("Temp: [%5.1fC] ", ws.sensor[i].soil.temp_c);
Serial.printf("Moisture: [%2d%%]\n", ws.sensor[i].soil.moisture);

} else {
Serial.printf("Rain: [-----.-mm] ");
}
if (weatherSensor.sensor[i].moisture_ok) {
Serial.printf("Moisture: [%2d%%] ",
weatherSensor.sensor[i].moisture);
}
else {
Serial.printf("Moisture: [--%%] ");
}
#if defined BRESSER_6_IN_1 || defined BRESSER_7_IN_1
if (weatherSensor.sensor[i].uv_ok) {
Serial.printf("UV index: [%1.1f] ",
weatherSensor.sensor[i].uv);
}
else {
Serial.printf("UV index: [-.-%%] ");
}
#endif
#ifdef BRESSER_7_IN_1
if (weatherSensor.sensor[i].light_ok) {
Serial.printf("Light (Klux): [%2.1fKlux] ",
weatherSensor.sensor[i].light_klx);
}
else {
Serial.printf("Light (lux): [--.-Klux] ");
}
#endif
// Any other (weather-like) sensor is very similar
if (ws.sensor[i].w.temp_ok) {
Serial.printf("Temp: [%5.1fC] ", ws.sensor[i].w.temp_c);
} else {
Serial.printf("Temp: [---.-C] ");
}
if (ws.sensor[i].w.humidity_ok) {
Serial.printf("Hum: [%3d%%] ", ws.sensor[i].w.humidity);
}
else {
Serial.printf("Hum: [---%%] ");
}
if (ws.sensor[i].w.wind_ok) {
Serial.printf("Wmax: [%4.1fm/s] Wavg: [%4.1fm/s] Wdir: [%5.1fdeg] ",
ws.sensor[i].w.wind_gust_meter_sec,
ws.sensor[i].w.wind_avg_meter_sec,
ws.sensor[i].w.wind_direction_deg);
} else {
Serial.printf("Wmax: [--.-m/s] Wavg: [--.-m/s] Wdir: [---.-deg] ");
}
if (ws.sensor[i].w.rain_ok) {
Serial.printf("Rain: [%7.1fmm] ",
ws.sensor[i].w.rain_mm);
} else {
Serial.printf("Rain: [-----.-mm] ");
}

#if defined BRESSER_6_IN_1 || defined BRESSER_7_IN_1
if (ws.sensor[i].w.uv_ok) {
Serial.printf("UVidx: [%2.1f] ",
ws.sensor[i].w.uv);
}
else {
Serial.printf("UVidx: [--.-] ");
}
#endif
#ifdef BRESSER_7_IN_1
if (ws.sensor[i].w.light_ok) {
Serial.printf("Light: [%2.1fklx] ",
ws.sensor[i].w.light_klx);
}
else {
Serial.printf("Light: [--.-klx] ");
}
#endif
Serial.printf("\n");

}
Serial.printf("RSSI: [%5.1fdBm]\n", weatherSensor.sensor[i].rssi);

} // if (decode_status == DECODE_OK)
delay(100);
} // loop()
112 changes: 67 additions & 45 deletions examples/BresserWeatherSensorCallback/BresserWeatherSensorCallback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "WeatherSensor.h"


WeatherSensor weatherSensor;
WeatherSensor ws;

// Example for callback function which is executed while waiting for radio messages
void loopCallback(void)
Expand All @@ -67,85 +67,107 @@ void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);

weatherSensor.begin();
ws.begin();
}


void loop()
{
// Clear all sensor data
weatherSensor.clearSlots();
ws.clearSlots();

// Attempt to receive entire data set with timeout of <xxx> s and callback function
bool decode_ok = weatherSensor.getData(60000, DATA_COMPLETE, 0, &loopCallback);
bool decode_ok = ws.getData(60000, DATA_COMPLETE, 0, &loopCallback);
Serial.println();

if (!decode_ok) {
Serial.printf("Sensor timeout\n");
}
for (int i=0; i<NUM_SENSORS; i++) {
if (weatherSensor.sensor[i].valid) {
Serial.printf("Id: [%8X] Typ: [%X] Battery: [%s] ",
(unsigned int)weatherSensor.sensor[i].sensor_id,
weatherSensor.sensor[i].s_type,
weatherSensor.sensor[i].battery_ok ? "OK " : "Low");
#ifdef BRESSER_6_IN_1
Serial.printf("Ch: [%d] ", weatherSensor.sensor[i].chan);
#endif
if (weatherSensor.sensor[i].temp_ok) {
Serial.printf("Temp: [%5.1fC] ",
weatherSensor.sensor[i].temp_c);
Serial.printf("Id: [%8X] Typ: [%X] Ch: [%d] St: [%d] Bat: [%-3s] RSSI: [%6.1fdBm] ",
(unsigned int)ws.sensor[i].sensor_id,
ws.sensor[i].s_type,
ws.sensor[i].chan,
ws.sensor[i].startup,
ws.sensor[i].battery_ok ? "OK " : "Low",
ws.sensor[i].rssi);

if (ws.sensor[i].s_type == SENSOR_TYPE_LIGHTNING) {
// Lightning Sensor
Serial.printf("Lightning Counter: [%3d] ", ws.sensor[i].lgt.strike_count);
if (ws.sensor[i].lgt.distance_km != 0) {
Serial.printf("Distance: [%2dkm] ", ws.sensor[i].lgt.distance_km);
} else {
Serial.printf("Distance: [----] ");
}
Serial.printf("unknown1: [0x%03X] ", ws.sensor[i].lgt.unknown1);
Serial.printf("unknown2: [0x%04X]\n", ws.sensor[i].lgt.unknown2);

}
else if (ws.sensor[i].s_type == SENSOR_TYPE_LEAKAGE) {
// Water Leakage Sensor
Serial.printf("Leakage: [%-5s]\n", (ws.sensor[i].leak.alarm) ? "ALARM" : "OK");

}
else if (ws.sensor[i].s_type == SENSOR_TYPE_AIR_PM) {
// Air Quality (Particular Matter) Sensor
Serial.printf("PM2.5: [%uµg/m³] ", ws.sensor[i].pm.pm_2_5);
Serial.printf("PM10: [%uµg/m³]\n", ws.sensor[i].pm.pm_10);

}
else if (ws.sensor[i].s_type == SENSOR_TYPE_SOIL) {
Serial.printf("Temp: [%5.1fC] ", ws.sensor[i].soil.temp_c);
Serial.printf("Moisture: [%2d%%]\n", ws.sensor[i].soil.moisture);

} else {
// Any other (weather-like) sensor is very similar
if (ws.sensor[i].w.temp_ok) {
Serial.printf("Temp: [%5.1fC] ", ws.sensor[i].w.temp_c);
} else {
Serial.printf("Temp: [---.-C] ");
}
if (weatherSensor.sensor[i].humidity_ok) {
Serial.printf("Hum: [%3d%%] ",
weatherSensor.sensor[i].humidity);
if (ws.sensor[i].w.humidity_ok) {
Serial.printf("Hum: [%3d%%] ", ws.sensor[i].w.humidity);
}
else {
Serial.printf("Hum: [---%%] ");
}
if (weatherSensor.sensor[i].wind_ok) {
Serial.printf("Wind max: [%4.1fm/s] Wind avg: [%4.1fm/s] Wind dir: [%5.1fdeg] ",
weatherSensor.sensor[i].wind_gust_meter_sec,
weatherSensor.sensor[i].wind_avg_meter_sec,
weatherSensor.sensor[i].wind_direction_deg);
if (ws.sensor[i].w.wind_ok) {
Serial.printf("Wmax: [%4.1fm/s] Wavg: [%4.1fm/s] Wdir: [%5.1fdeg] ",
ws.sensor[i].w.wind_gust_meter_sec,
ws.sensor[i].w.wind_avg_meter_sec,
ws.sensor[i].w.wind_direction_deg);
} else {
Serial.printf("Wind max: [--.-m/s] Wind avg: [--.-m/s] Wind dir: [---.-deg] ");
Serial.printf("Wmax: [--.-m/s] Wavg: [--.-m/s] Wdir: [---.-deg] ");
}
if (weatherSensor.sensor[i].rain_ok) {
Serial.printf("Rain: [%7.1fmm] ",
weatherSensor.sensor[i].rain_mm);
if (ws.sensor[i].w.rain_ok) {
Serial.printf("Rain: [%7.1fmm] ",
ws.sensor[i].w.rain_mm);
} else {
Serial.printf("Rain: [-----.-mm] ");
}
if (weatherSensor.sensor[i].moisture_ok) {
Serial.printf("Moisture: [%2d%%] ",
weatherSensor.sensor[i].moisture);
}
else {
Serial.printf("Moisture: [--%%] ");
Serial.printf("Rain: [-----.-mm] ");
}

#if defined BRESSER_6_IN_1 || defined BRESSER_7_IN_1
if (weatherSensor.sensor[i].uv_ok) {
Serial.printf("UV index: [%1.1f] ",
weatherSensor.sensor[i].uv);
if (ws.sensor[i].w.uv_ok) {
Serial.printf("UVidx: [%1.1f] ",
ws.sensor[i].w.uv);
}
else {
Serial.printf("UV index: [-.-%%] ");
Serial.printf("UVidx: [-.-%%] ");
}
#endif
#ifdef BRESSER_7_IN_1
if (weatherSensor.sensor[i].light_ok) {
Serial.printf("Light (Klux): [%2.1fKlux] ",
weatherSensor.sensor[i].light_klx);
if (ws.sensor[i].w.light_ok) {
Serial.printf("Light: [%2.1fKlux] ",
ws.sensor[i].w.light_klx);
}
else {
Serial.printf("Light (lux): [--.-Klux] ");
Serial.printf("Light: [--.-Klux] ");
}
#endif
Serial.printf("RSSI: [%5.1fdBm]\n", weatherSensor.sensor[i].rssi);
}
Serial.printf("\n");

} // if (decode_status == DECODE_OK)
}
delay(100);
} // loop()
Loading

0 comments on commit 59e45b6

Please sign in to comment.