Temperature Monitoring - Part 1
4 minutes read •
Table of Contents
Introduction
I’ve written about monitoring temperature around the house before.
The solution I wrote about then is still working well.
But, more recently, I wanted to not only update the system, play with some new things, but also a minor underlying driver was the desire to be able to see when the boiler was on, and where the heat was being sent; radiators and/or hot water.
I had also been looking for a reason to have a play with Raspberry Pi Pico and I’d found that it’s fairly trivial to use DS18B20 sensors on one of the Pico’s GPIO pins.
Hardware
So, I placed an order for:
- a Pico2W
- a small breadboard to prototype it
- a solderable breadboard for the final item
- and some DS18B20 sensors
The DS18B20 has a low power requirement, and so with the Pico powered by a suitable power supply, 3V3 can be taken from pin 36, with ground on any of the GND pins. I used pin 38 in reality, but pin 28 makes the diagram tidier.
The data pin from the DS18B20 can be connected to any GPIO pin, with the 4k7 resistor then connected between data on the GPIO pin and 3V3.
Each DS18B20 has a unique 64 bit address, and so it’s possible to connect multiple sensors to the same GPIO pin.
Software
Next we need to write some code for the microcontroller. Of the languages supported by the Pico, I am most familiar with Python, so I grabbed a MicroPython image1.
When first connected to your USB port, a new Pico presents in bootloader mode. Loading the MicroPython image is as simple as copying the U2F image file to the presented volume upon which it will automatically reboot.
You can persuade the Pico to connect in bootloader mode in future by pressing the bootloader button while you connect the power.
I use Thonny for programming the Pico.
We need the code to do a few things:
- connect to the WiFi
- initialise the temperature sensor(s)
- send the collected temperature data somewhere
A friend was using iotplotter.com to send time series data and visualise it in a graph so I decided to use that as the API is nice and simple to proof-of-concept.
A few articles referenced the itk_pico code, and that gave me a bit of a head start too.
I've forked it in order to make a couple of tweaks (see below); you can find it here which also references back to the original authors work/repository.
Unless I'm mistaken (I'm new-ish to Python, so this is likely!) the code assumes a single connected temperature sensor, as it returns from the subroutine as soon as it gets a temperature. For my use case, I want to be able to have multiple sensors. I also wanted to be able to give the sensors ascii names, so I made some modifications to the temperature.py code...
First, a method to convert to a human friendly name:
=
return
...a method to return all of the friendly names...
=
=
return
...modified the get_temperature method to loop around all sensors and return all of the temperatures...
=
=
=
=
return
...and lastly, some tweaks to the initialisation; a bit more debug output.
=
=
=
=
=
When the pico is powered on without a console, ie: just plugged into a USB PSU rather than your computer's USB port, it executes main.py so lets have a look at that...
I'm only using the temperature, wifi and logger code, so we import those. The IOT Plotter API expects the payload as JSON, we have our config, and we'll use the requests module to POST to the API.
Next, we'll initialise the temperature sensors and friendly names...
=
=
We'll initialise default values for each sensor and then have a look in the config to see if there's a specific setting for each...
=
=
The main loop looks like this. It's possible to set the time for each data value but we're sending data directly in real time, so by omitting the time, IOT Plotter will use the current time.
# check we're still connected to the wifi
# get the temperature from each sensor
=
# initialise headers and payload
=
=
=
# for each sensor, get the temperature and add it to the payload
=
=
=
# send the payload to the API
=
# ...and sleep
The config file looks like this:
=
=
= 15
= 60
=
=
=
=
=
=
The full code can be found in the github repo.
Having tested it, I soldered the board up.
Sensors can be connected to the screw terminal on the top right.
Stay tuned for the next article on implementing this...