Analysing Swimming Pool Sensor Data with Python
IoT systems often collect measurements from the physical world and transform them into useful information. Swimming pool monitoring is one example: a system might record water temperature, pH measurements, and equipment status.
Before building a dashboard, developers need a reliable way to validate and process those readings.
In this tutorial, we'll create a small Python program that analyses simulated pool sensor data.
1. Define Sample Readings
readings = [
{"temperature": 27.4, "ph": 7.3},
{"temperature": 28.1, "ph": 7.4},
{"temperature": 27.8, "ph": 7.2},
{"temperature": None, "ph": 7.3},
{"temperature": 29.0, "ph": 7.5}
]
These values are simulated for demonstration. They are not live measurements or recommendations for pool water treatment.
2. Validate the Data
A sensor may send incomplete data or values that cannot be processed safely.
import math
def valid_reading(reading):
temperature = reading.get("temperature")
ph = reading.get("ph")
for value in (temperature, ph):
if not isinstance(value, (int, float)):
return False
if not math.isfinite(value):
return False
return True
This function checks that both values are numeric and finite. A real application should also validate expected ranges, units, timestamps, and sensor identity.
3. Filter Invalid Records
valid_readings = [
reading
for reading in readings
if valid_reading(reading)
]
print("Total records:", len(readings))
print("Valid records:", len(valid_readings))
print("Rejected records:", len(readings) - len(valid_readings))
Rejecting invalid records prevents missing measurements from accidentally being treated as zero.
However, invalid readings should not simply disappear in a production system. Logging them can help identify failing sensors or communication problems.
4. Calculate an Average
def average_temperature(records):
if not records:
return None
values = [
record["temperature"]
for record in records
]
return sum(values) / len(values)
average = average_temperature(valid_readings)
if average is None:
print("No valid temperature data.")
else:
print(f"Average temperature: {average:.1f} °C")
Notice how the function handles an empty list. This avoids a division-by-zero error when no valid records are available.
5. Design the Next Version
A more complete monitoring system could store readings in a database, plot changes over time, and send alerts when configured conditions are met.
Developers should also consider timestamp consistency, sensor calibration, duplicate messages, missing data, and access control.
Water-treatment decisions should rely on appropriate testing procedures and qualified guidance rather than a software dashboard alone.
Conclusion
Reliable monitoring starts with reliable data processing. Separating validation, filtering, and analysis makes the program easier to test and maintain.
This approach is useful far beyond pools: it also applies to weather stations, greenhouse systems, industrial equipment, and environmental monitoring projects.
Top comments (0)