Electronics

PLEN

One sensor, many roles: an ESP32-C3 thermometer

For a week, the room sensor for my boiler controller was built without a real thermometer. Then it turned out the same firmware could measure a room, underfloor heating pipes and the temperature outside.

Czerwona płytka ESP32-C3 Super Mini z dolutowanym czujnikiem temperatury, pętlą przewodu i elastyczną anteną na niebieskiej macie warsztatowej

At first my boiler controller took room temperatures from Home Assistant. To work on its own, it needed its own sensor. It was built on an ESP32-C3 Super Mini board with DS18B20 probes. It sends readings over Wi-Fi straight to the controller. It started as a sensor for a single room, and a week later the same firmware could be a room thermometer, a gauge for the water loop temperatures at the underfloor heating manifold or an outdoor thermometer.

The thermometer that lied for 36 hours

The story starts in September, with an off-the-shelf sensor. My outdoor Zigbee thermometer had been showing 27.19 °C for a day and a half without any change. The device wasn’t transmitting at all, and Home Assistant restored the last remembered value after every restart. Had it stayed like that until winter, the heating’s weather compensation would never have kicked in once, and nobody would have noticed.

A single sensor always looks believable.

It took a second instrument in the same conditions to catch it. Ten days later, on 11 September, I stuck DS18B20 probes to the underfloor heating pipes. Those measurements produced one of the requirements for the future sensor: a valve closing on a loop shows up on the return within 2–3 minutes, so a sensor on the pipes has to measure every 30 seconds, not every 5 minutes like a room thermometer.

“TS1020”, the thermometer that wasn’t a thermometer

On 31 October I had a board and a part in a TO-92 package marked “TS1020”, which I took for a temperature sensor. The agent was told to ask first what this part actually was. It couldn’t find any sensor by that name online. Once soldered in, the part shorted the power supply: the board heated up and dropped off USB. The agent told me to stop connecting it and switched the work to a fake thermometer, so the rest of the firmware could be written without a sensor.

We only read a real DS18B20 on that board on 8 November. For a week, the whole firmware was built on a simulated thermometer. Since then every set of instructions carries a warning: after connecting for the first time, touch the probes, and if one of them is hot, disconnect immediately.

One evening

Most of the sensor was written in one evening, in 20 commits. In short:

  • Setup from a phone. When the sensor doesn’t know a network, it opens its own with a settings page. The page matches the look of the controller app, because the design was made in Claude Design under hard constraints: one file, no libraries or external fonts, at most about 10 kB compressed. It came out at about 8 kB.
  • The controller says when to report. The sensor sends the temperature with a separate token for the room, and the reply tells it how many seconds until the next report. The controller can shorten the gap when something is changing fast, or lengthen it to save battery, without changing the sensor’s firmware.
  • Deep sleep. In battery mode the sensor wakes up, measures, sends and goes back to sleep.
  • An update that rolls itself back. The board keeps two copies of the firmware. We tested it maliciously: we flashed firmware that crashes right after boot, and after a restart the board went back to the previous version.

The battery life figures are estimates from datasheets: about 4 months on a 2000 mAh cell with a report every 5 minutes. I didn’t measure the current, because I had no meter with a microamp range. The first units sit next to chargers anyway.

Signed updates from GitHub

The next day I asked whether updates could install themselves from releases on GitHub. The sensor checks the latest version number in the public releases repository, downloads the firmware, verifies the SHA-256 checksum and the digital signature against a key built into the firmware, and only then switches over. Firmware I build locally has my home Wi-Fi password compiled in, so only an image built from a clean copy goes out for publication, and it is also scanned for passwords.

Two bugs only showed up on the real GitHub, not on the test server. The download didn’t follow GitHub’s redirect to its file server. The firmware also only confirmed itself after a successful report to the controller, so while unconfirmed it refused the next update. This release mechanism later moved over to the controller and to the wall panel.

A bit like Tasmota, without a line of Tasmota

On 5 November I had the idea of basing the boards by the underfloor heating valves on an extended version of the room sensor firmware. The same firmware, with the number of probes and the pins set in the configuration, and profiles for different boards. The design rule: the sensor knows what is connected to it, and the controller knows what it is for. Assigning a probe to a room, an underfloor loop or the outdoor temperature is done in the controller, in one place for the whole house.

Tasmota and ESPHome are licensed under the GPL, and the controller is meant to be sold one day. The idea of “board profile plus pins in the configuration” is fair to borrow; the code is not. A buyer won’t have ESPHome or Home Assistant either, so the sensor has to talk to the controller by itself.

The first of four stages is done: up to 16 probes on one wire. I asked the agent why two probes should go to the same pin. Every DS18B20 has a unique 64-bit serial number set at the factory, so the firmware tells them apart by number, not by pin. Six probes at the manifold are still three wires, one resistor and one pin. To find out which probe is on the flow and which on the return, you warm one in your hand: its row on the page lights up.

The first real DS18B20

On 8 November I soldered two real probes to the board, in parallel on one pin. Both were found and the line worked without errors. The first difference between them, 0.4–0.8 K, looked like a problem, but it was me warming one probe with my fingers. Once it cooled down, the difference dropped to 0.19–0.25 K, with these probes rated at ±0.5 °C.

Then I created a room called “Łącznik” in the controller, connected the sensor, and the page showed “Last report: failed”. I suspected the controller happened to be updating. The agent checked: for “Lacznik” the controller answered “unknown room”, while for “lacznik” it accepted the reading. I had typed the room’s name instead of its identifier. That mistake went straight into the instructions and into a hint on the page.

First start through a buyer’s eyes

I wanted to see what the first start looks like for someone who gets a bare board. We wiped it completely and flashed a clean image from the release, just as a buyer would. Testing on a live human immediately found two bugs you can’t see in the code. As a welcome I got the full page with every setting instead of just the network and password, and it wouldn’t save without a room identifier.

Three versions came out in about 35 minutes. The “Welcome” screen now asks only for the network and the page password, and the controller and room are optional. The sensor also got a name field, which becomes its address on the home network: for example, “Łącznik parter” gives lacznik-parter.local.

There was one deliberate compromise here. Originally we planned a random password for every unit. But it was only visible on the USB console, and there is no sticker with the password, so for a layperson it was a dead end. The settings network now has a public password, but it only works when the sensor can’t connect to the home network.

What’s left to do

  • Stages 2–4 of the universal sensor, meaning probe-to-loop assignments and a device list in the controller.
  • Measuring the current and testing battery mode.
  • A day of comparison against the existing boards on the same pipes. Only after that can the old boards be replaced.
  • Checking whether the board downloaded and installed release 0.4.4 from GitHub by itself. I published the release on 9 November and checked it on the GitHub side, but I have no record of the result on the board.

For places without a socket, I’m testing another route in parallel: an off-the-shelf Xiaomi thermometer flashed with open Zigbee firmware, which runs for months on a single battery.

Related posts

Comments

No comments yet

Leave a comment

Your email address will not be published. Comments appear after approval.