Hardware

A closed electronics enclosure  box with a black temperature sensor cord coming out of it

I put an esp32 in a box with some 18650 batteries and connected a DS18B20 temperature sensor. I had lots of power problems.

  • First attempt: using the voltage regulator on a battery shield I found on ebay.

An esp32 with a battery shield in a case

The battery fully discharged within a week of running, and I determined that the battery shield was wasting a lot of power.

  • Second attempt: bare battery case with no voltage regulator

An esp32 in a project enclosure with two 18650 batteries in a simple battery case

In this case, the esp32 dev board was drawing >10mA, even when the device was in deep sleep.

  • Third attempt: use an esp32 dev board which is actually engineered to be low power.

Thanks to TvE for the nice blog post about finding a low power esp32 dev board. I also dropped the external pull-up resistor in favor of an internal pull-up resistor and powered the temperature sensor from a GPIO pin instead of the 3.3v rail, so it only draws power when the esp32 is awake.

An EzSBC dev board with a few power wires soldered to it

Hardware problems

  • The real-time clock on the esp32 is very inaccurate (error of approx 5e-3s/s). Some dev boards might have an external RTC, but mine doesn’t
  • Connecting peripherals to some pins prevents the esp32 from accepting a flash.

Software

I used the toolchain from the manufacturer (espressif-idf).

The device

  • wakes from sleep
  • turns on WiFI
  • generates a new JWT for Google Cloud IOT, if necessary
    • this requires getting the current time using NTP
  • establishes a TLS connection to the Google Cloud IOT
  • powers up the temperature sensor via GPIO
  • reads the temperature sensor
  • turns off the temperature sensor
  • uploads the temperature to the cloud, using MQTT
  • enters deep sleep for 10 minutes

On the cloud:

  • MQTT message becomes a pubsub message
  • A Google Cloud Function copies the data from the pubsub message into a BigQuery table
  • Another Google Cloud Function queries the sensor data on-demand and returns the data as JSON.
  • A little client-side JavaScript queries the latest data and displays it with Chart.js

A screenshot of a simple webpage showing a chart of the temperature of the beehive over time.

Software problems:

  • Lots of problems getting toolchains to build on my Raspberry Pi. Eventually, I settled on building the software on a x86 VM in the cloud, then copying the artifacts and flashing from the Raspberry Pi.
  • Using the manufacturer’s toolchain instead of Arduino meant that lots of convenient libraries wouldn’t work out-of-the-box. Google’s published library for creating the JWT for Google Cloud IOT core required C++ and some Arduino headers, so I had to modify it quite a bit to make it work.