DEV Community

freelancing
freelancing

Posted on

Building a Simple Tyre Pressure Monitoring Simulator with Python

Vehicle monitoring systems collect measurements and use them to help identify conditions that may require attention. The same general idea can be explored with a small Python program.

In this tutorial, we'll build a simple tyre pressure monitoring simulator that accepts sample readings, validates the input, and generates alerts when a reading falls outside a configurable range.

This is an educational simulation, not a replacement for a real tyre pressure monitoring system or vehicle manufacturer's recommendations.

1. Create Sample Data

Let's represent each tyre using a dictionary.

tyres = {
    "front_left": 34.0,
    "front_right": 33.5,
    "rear_left": 32.0,
    "rear_right": 34.0
}
Enter fullscreen mode Exit fullscreen mode

These numbers are illustrative values in PSI. The appropriate pressure depends on the specific vehicle, tyre configuration, and operating conditions.

2. Validate the Readings

Sensor data should not be trusted automatically. It may be missing, malformed, or outside the range the application can process.

import math

def validate_pressure(value):
    if not isinstance(value, (int, float)):
        return False

    if not math.isfinite(value):
        return False

    return value > 0
Enter fullscreen mode Exit fullscreen mode

The math.isfinite() check rejects values such as NaN and infinity.

3. Generate Alerts

For this simulation, let's define a configurable lower threshold.

def check_pressure(tyres, minimum_pressure):
    alerts = []

    for tyre, pressure in tyres.items():
        if not validate_pressure(pressure):
            alerts.append(f"{tyre}: invalid reading")
        elif pressure < minimum_pressure:
            alerts.append(
                f"{tyre}: pressure below configured threshold"
            )

    return alerts
Enter fullscreen mode Exit fullscreen mode

Run the simulation:

alerts = check_pressure(tyres, minimum_pressure=32.5)

if alerts:
    for alert in alerts:
        print(alert)
else:
    print("No readings below the configured threshold.")
Enter fullscreen mode Exit fullscreen mode

The threshold is only an example for demonstrating conditional logic. A real system must use vehicle-specific pressure specifications and account for measurement conditions.

4. Test Edge Cases

A monitoring system should be tested with more than normal readings.

test_values = [
    34.0,
    0,
    -5,
    None,
    float("nan"),
    float("inf")
]

for value in test_values:
    print(value, validate_pressure(value))
Enter fullscreen mode Exit fullscreen mode

Testing edge cases helps prevent invalid measurements from being mistaken for valid sensor readings.

5. How Would This Work in a Real System?

A production implementation would require more than a dictionary and a threshold.

It would need to consider:

  • Sensor calibration and measurement accuracy.
  • Vehicle-specific recommended pressures.
  • Temperature-related pressure changes.
  • Missing or delayed sensor messages.
  • Warning thresholds and alert severity.
  • Logging, testing, and failure handling.

It is also important to distinguish a low-pressure warning from a sensor fault. These are different conditions and should not be represented by the same alert.

Conclusion

This small project demonstrates a useful programming pattern: collect measurements, validate them, evaluate rules, and report meaningful results.

The same pattern can be adapted to equipment monitoring, industrial sensors, energy systems, and other applications where data needs to be checked before it is acted upon.

Top comments (0)