knaxuslearn · build · ship

Projects / Temperature logger / Wrap up

Wrap up

Keep going

Pick one when you want more:

  • An alert. When the temperature passes a limit you set, light one of the board's user LEDs, or send a message to your phone with a web request.
  • A real-time clock. Add a small battery-backed clock chip over I2C, so readings are dated correctly even with no network.
  • A digital sensor. Swap the TMP36 for an I2C sensor with better accuracy. Linux has drivers for many of them, and they appear under /sys/class/hwmon.
  • A graph. Add a page that draws the last day as an SVG line, built from the same query as the CSV.
  • Keep less. Add a nightly job that replaces readings older than a month with their hourly averages.
  • Read-only root. Make the system's files read-only and keep the database on its own partition, so a power cut can never damage the operating system.

What this version leaves out, and what a real logger does

  • The TMP36 is accurate to a degree or two. A real logger uses a calibrated sensor, or calibrates this one against a good thermometer and stores the correction.
  • Our median filter handles spikes. It does nothing for slow errors, such as a sensor warmed by the board it sits next to. Put the sensor on a short cable away from the board.
  • We trust whoever can reach port 8080. A real logger on a shared network puts the page behind a password and HTTPS, or sends its readings to a server instead.
  • clock_ok tells you a reading's time is wrong, not what the right time was. A real logger fixes this with a battery-backed clock, or records the time since boot and works the real time out later.
  • We tested crashes on a laptop and power cuts not at all. Pull the plug on your own board a few times before you trust it.
  • We never ran this guide on a real board. Expect to find something we missed, and check the board's own documentation when a pin or a path does not match.

Check yourself

Answer each question in your head first, then open it to check.

  1. A TMP36 reads 1700 on the BeagleBone's ADC. What temperature is that?
    1700 × 1.8 / 4095 is 0.747 volts. Take off the 0.5 volts at 0 degrees and multiply by 100: (0.747 − 0.5) × 100 is about 24.7 degrees.
  2. Why does the logger take the median of nine readings and not the average?
    One wild value, such as a 4095 from a spike, moves an average a long way: one 4095 among eight readings of 1640 pulls the average of the nine up to about 1913, which is 34 degrees instead of 22. In the sorted list it sits at one end, and the median takes the middle, so it has no effect at all.
  3. Why does the loop add INTERVAL to next_due instead of sleeping for INTERVAL after each reading, and why does it measure with time.monotonic()?
    Sleeping for INTERVAL after each reading adds the time the reading took, about 0.2 seconds, to every gap, so readings drift later and later. Adding INTERVAL to a fixed starting point keeps them on a grid. time.monotonic() never jumps, while the wall clock jumps when the network sets it, which would turn a ten-second sleep into hours or into a negative number.
  4. The board boots with no network, and the first reading is dated 2025. What does the logger do about it, and why can it not just fix the date?
    It stores the reading with clock_ok set to 0, and the report leaves those readings out. It cannot fix the date because, at that moment, nothing on the board knows the right time. There is no battery clock, and no network to ask.
  5. Teach it back: explain to a teammate what the crash test in Step 4.3 proves, and what it does not.
    It proves that SQLite's database comes through the logger being killed at any moment: the file stays sound, every reading reported as saved is still there, and no half-written reading appears. It does not prove anything about power cuts, because after a kill Linux still writes out what it had in memory. A power cut loses that too, and only a test on the board with the plug pulled checks it.

Sources