I decided recently to set up a string of temperature sensors around our house to work out the temperature profile of different rooms.
With the recent release of the Pico W, I realised that I could do this quite simply, as long as I could find a way to “nicely” display the data.
You’ll have read my exploits with Anvil last week, I expect, and saw that I didn’t have much luck. However, I knew that Adafruit had their very own IoT platform – Adafruit IO – and that it had been around awhile, so I thought I’d give that a try and document it.
Set-up your Adafruit account
Go to https://io.adafruit.com/ and register an account if you don’t have one. You’ll be creating Feeds and Dashboards in a bit.
Assemble your hardware
For this project, I’m using:
- A Raspberry Pi PicoW
- An Adafruit AHT20 I2C temperature and humidity sensor
As you can see, I’ve soldered them together. I’ll put a 3D printed case together at some point, I’m sure.
Assemble your software
For this project, I’ve used Micropython for the PicoW and then loaded the Pico up with an MQTT client and an AHT library as follows:
- The MQTT example library from pycom – https://github.com/pycom/pycom-libraries/tree/master/examples/mqtt
- AHT temperature sensor library from Andreas Bühl – https://github.com/targetblank/micropython_ahtx0
The code
The code is in several files and can be found on my GitHub repository.
- main.py – the main code that holds it all together
- config.py – defines the “name of the device” by way of a “feed prefix” which is used to tie the Adafruit IO feeds together with the device.
- secrets.py – credentials etc. There’s a sample secrets file in the repository so you know what to put in it.
- The two libraries as mentioned above.
Adafruit IO set-up
Set-up your config.py with a feed prefix – this can be anything by mine’s called temp_sensor_1.
Then, create two feeds in Adafruit IO: <prefix>_temperature and <prefix>_humidity. For example, temp_sensor_1_temperature etc.
Create a dashboard by adding Graph boxes that bring in those feeds.
Output
In theory, running the script through Thonny should show you the sensor readings as well as connection success/failure for wifi and notification of when the readings are published via MQTT to Adafruit IO.
You should end up with something like this on Adafruit:
Questions
If you’re trying to do this, you probably have questions – leave a comment and I’ll try and get back to you! 🙂
Well done Mike. MQTT is good, isn’t it?
Yus! 🙂
This is great and something I’ll certainly be trying myself soon. I’ve been using ESP8266’s with a few other sensors in a similar fashion, pushing data back to a nice dashboard via MQTT – so much so I decided to dump it all in a GitHub repo for others to look at: https://github.com/raspberrycoulis/mqtt-home-data – I’ll look at adding more to that with the Pico W very soon!
I am going to attempt the CircuitPython way (now that CP 8.0 betas are supporting that).
There is a guide for that, so it will be easy: https://learn.adafruit.com/quickstart-rp2040-pico-with-wifi-and-circuitpython/usage-with-adafruit-io
Sorry: https://learn.adafruit.com/pico-w-wifi-with-circuitpython/pico-w-with-adafruit-io