CodeRigor

GUIDED LESSON / 1 OF 6

Define warm, okay and unknown

Give thresholds, units and failed readings different meanings.

What moves through this step
  1. 01DHT11 reading
  2. 02Range check
  3. 03Hysteresis
  4. 04LED + JSON

Decide what the display means

The finished project reads a DHT11 temperature/humidity sensor every two seconds. Green means the temperature policy is ok; red means warm; both off means unknown. Humidity is reported as data, not used to infer comfort, health or safety.

Temperatures are degrees Celsius. A reading at or above 28 enters warm. A reading at or below 26 enters ok. Between those boundaries, keep the previous valid state. This prevents a reading near one threshold from rapidly switching the LEDs.

A missing or out-of-range sample becomes unknown immediately. We do not keep a green light lit using yesterday's successful reading. The policy accepts the DHT11 working domain used by this lab: 0–50 °C and 0–100 percent relative humidity. That is a software validation range, not an accuracy claim.

Start without a board

Download and unzip the project into a new directory. On your computer run:

Bash
python3 -m unittest -v

The tests exercise policy.py using supplied values. Do not run main.py on desktop Python; its machine and dht modules belong to the MicroPython device environment.

Try it

Trace 25, 27, 28, 27, 26 °C. Write down the status after each reading. Then insert a failed reading and decide what should happen to the LEDs before the next successful sample.