I wanted to know whether the shed gets cold enough overnight to bother heating, which is the sort of question that should take five minutes to answer and instead becomes a fortnight of hardware. An ESP32, a BME280 for temperature, humidity and pressure, a small LiPo, and a solar panel that does almost nothing in a Yorkshire January. The plan was to deep-sleep between readings and post to an MQTT broker. The plan, broadly, worked.
The ESP32 talks to the BME280 over I2C, which is two wires and a great deal of cursing about pull-up resistors. The breakout I used had them on board, so that bit was painless. The code is barely worth showing: wake, read the sensor, connect to wifi, publish, sleep again.
#define uS_TO_S 1000000ULL
#define SLEEP_S 600 // ten minutes
void setup() {
Wire.begin();
bme.begin(0x76);
float t = bme.readTemperature();
// ... connect wifi, publish t/h/p over MQTT ...
esp_sleep_enable_timer_wakeup(SLEEP_S * uS_TO_S);
esp_deep_sleep_start();
}
void loop() {} // never runs; deep sleep restarts at setup()
The deep-sleep trick is the whole point of the battery lasting. The ESP32 in deep sleep pulls a few microamps; awake with wifi it's a couple of hundred milliamps. Spend ten minutes asleep and two seconds awake and the average current is tiny. The radio is the expensive bit, so the less time it's on, the better.
The "mostly" in the title is earned. Every third or fourth reading came back as a flat zero, or occasionally the wonderful value -45.00, which is not a temperature the shed has ever achieved. I assumed a dodgy sensor and ordered another. Of course it wasn't the sensor. It was a cold solder joint on the SDA line that read fine when I poked it with a multimeter and intermittently failed when the board was actually running and warm. Reflowed it, added a tiny blob more solder, and the zeros stopped.
That cost me three days of suspecting my code, which had been correct the entire time. The lesson, relearned for roughly the tenth time: when the data is intermittently garbage, suspect the physical layer before the software. A reading that's wrong in a clean, repeatable way is a bug. A reading that's wrong at random is usually a wire.
It's been up a week now. The shed does get cold enough to bother heating. I could have just put a thermometer out there, but then I wouldn't have a graph, and what's the point of being cold without a graph.