Wi-Fi Temperature Logging with ESP8266

Temperature logging is awesome! It gives us visibility on a dimension of our local environment that we can usually only feel. There also seems to be some demand for precision temperature tracking for precesses like beer brewing and clay firing.

A few months ago I built a Bluetooth Thermometer using a digital thermometer and Bluetooth LE unit from Adafruit. I also started making an iOS App that could theoretically display the temperature in real-time and a give an interactive graph, with curve-fitting, so it could notify you 5 minutes before a setpoint is reached.

The ESP8266 is a shiny new low-cost WiFi chip that everyone’s excited about.

ESP-01 Top
ESP-01 Top
ESP-01 Bottom
ESP-01 Bottom

The ESP-1 module (pictured) includes the ESP8266 chip, a flash memory, a crystal oscillator for the CPU, some indicator LEDs, and an antenna for Wi-Fi.

ESP8266 Block Diagram
ESP8266 Block Diagram

The ESP8266 IC contains both the analog Wi-Fi radio, and the digital components of a microcontroller. It can run a custom application from the flash like an Arduino. Another option is to install NodeMCU, which runs Lua scripts, interactively over serial, or from flash.

NodeMCU comes with a Lua module for my digital thermometer, DS18B20! Once that’s installed, the temperature can be read like so:

The next step is to send it somewhere over the internet. Before that’s possible, we need to connect to the Wi-Fi:

Now that we’re online, we can send those temperature number somewhere.

We could POST them to a server using HTTP (like a web form), but another option is to use MQTT, a Pub/Sub protocol designed for embedded sensor applications just like this. The MQTT server is called a broker. The sensor can publish messages to the broker with with a topic, e.g. temp/0. Applications can subscribe to topics they’re interested in, e.g. temp/# (# is wildcard).  I chose the open-source broker Mosquitto.

NodeMCU comes with an MQTT client implementation. First we connect to the broker:

Then we can publish temperature messages:

MQTT has 3 levels of QoS (Quality-of-Service). Level 1 means the message will be delivered “at least once”. The client will re-send the message until the broker acknowledges receipt.

Now that the data is published to the server, we can do anything with it!

I wanted to make graphs, so I looked around and found the time-series database InfluxDB and the graphing front-end Grafana. This excellent open-source stack is also available as a hosted service. I made a little node.js script called measure that subscribes to temp/# (using MQTT.js) and dumps the samples into InfluxDB.

Here’s a diagram of the whole flow:

System Block Diagram
System Block Diagram

I ran the sensor in my room for a while, sampling the temperature every second, and made some graphs!

Grafana - Air Conditioning
Grafana – Air Conditioning

This screenshot of the Grafana Dashboard show the temperature of my room with the AC running. Note the fluctuations as the AC cycles, keeping the temperature between 77 and 78 degrees all night.

The thermometer probe I have is waterproof, and can go up to 125°C (257°F), and I was boiling some potatoes, so I figured it’d be a good test:

Grafana - Boiling Potatoes
Grafana – Boiling Potatoes

The graph shows the water rising to 210˚F, where is stays (boiling), until I turn it off, where it cools down to room temperature over a few hours.

The DS18B20 sensor has a maximum resolution of 1/16˚C (0.1125˚F). This is made visible by zooming in:

Grafana- Zoomed In
Grafana- Zoomed In

Whee temperature logging!

What next? One possibility is be to make my own controller for our old AC unit, with precise temperature and mode controls, scheduling, and HomeKit integration!

2 thoughts on “Wi-Fi Temperature Logging with ESP8266”

  1. you wrote:

    — Publish a message to the MQTT Broker

    i think it must be

    — Publish a message to the MQTT Broker

    right ?
    best regards

Leave a Reply

Your email address will not be published. Required fields are marked *