This tutorial will hopefully show you how to transmit temperature readings via MQTT to Adafruit.io.
What is MQTT?MQTT stands for MQ Telemetry Transport. It's what's known as a Subscriber/Publisher Protocol. This can be thought of two devices, they both have their own "channels" or in MQTT terms, a "topic". When one of them sends a message, this is known as publishing. Another device can subscribe to that specific message thread.
Devices can both subscribe and publish to topics but do not directly connect to each other, instead they create topics on a "broker" which is like a server, just for MQTT.
What you'll need:In order to follow along with this tutorial, you'll need the following:
- A MicroPython powered device
- A Temperature Sensor
- An account on Adafruit IO (io.adafruit.com)
- The MQTT Library (GitHub)
- The onewire Library (GitHub)
- Set up you're MicroPython Device (this tutorial uses WiPy 2.0)
- Ensure that it's updated to the latest firmware. If it's not, head to docs.pycom.io for more information about pycom devices.
- Insert the temperature sensor into the expansion board.
- Access your device's file storage. Find the lib folder (/flash/lib) and copy across the matt.py and onewire.py files referenced above and at the end of this tutorial.
With the MQTT library set up, we can start writing out main.py file to connect and publish with MQTT. Because this project only intends to send data and messages to Adafruit.io, we don't need to write the file to be able to subscribe.
- To begin with, open a new main.py. We'll want this script to connect to Wifi, connect to our MQTT Broker and then publish the information it's receiving from the temperature sensor to io.adafruit.com
from network import WLAN
from mqtt import MQTTClient
from machine import Pin
from onewire import DS18X20
from onewire import OneWire
import machine
import time
wlan = WLAN(mode=WLAN.STA)
wlan.connect("WifiNetwork", auth=(WLAN.WPA2, "WifiPassword"), timeout=5000)
while not wlan.isconnected():
machine.idle()
print("Connected to Wifi\n")
client = MQTTClient(client_id="example_client", server="io.adafruit.com",user="Your_UserName", password="your_api_key", port=1883)
client.connect()
#DS18B20 data line connected to pin P23
ow = OneWire(Pin('P23'))
temp = DS18X20(ow)
def get_temp():
temp.start_convertion()
time.sleep(1)
tempdata = temp.read_temp_async()
if isinstance(tempdata, float):
return(str(tempdata))
while True:
data = get_temp()
if (data != None):
print(data)
client.publish(topic="yourAccount/feeds/temperature", msg= data)
time.sleep(1)
The main.py that we've written will first of all connect to wifi (This will need to be changed for your own Wifi details) will take a temperature reading from the device and publish it to a line graph on adafruit. To test that this is working, we'll have each reading also print to the Pymakr console.
Setting up Adafruit IOAdafruit's IO broker is what we'll be using to try out MQTT and publishing the data we receive.
- Head to io.adafruit.com, register an account and then log in.
- Click on the feed tab and create a new feed. You can name it anything you like but for this tutorial, we'll be calling it "temperature", the same as we've used in our main.py file.
This is the topic that we'll be listening for the message on. We now need something to display our incoming data with, to do this we'll be using "Dashboards" which can be used to display anything that happens on a feed. Since we'll have a continuous stream of floats coming from our main.py, we'll want to display this as a line graph so that we can see trends and changes as the program runs.
- Head to dashboards, create a new one. Again, this can be anything you like.
- Select a line graph and choose the "temperature" feed as the data source.
- Our WiPy is now publishing data from the temperature sensor to Adafruit IO as a broker to then be passed to the dashboard as the subscriber.
- Go back to the main.py and modify the line of code below in order to add your own Adafruit IO username and the password (which is generated under the Adafruit IO settings tab and known as the AIO key).
client = MQTTClient(client_id="example_client", server="io.adafruit.com",user="Your_UserName", password="your_api_key", port=1883)
Once this has been done and the code is running through your WiPy then you should see the line graph on your dashboard updating every few seconds to show the most recent temperature.
And that's it!If you've followed this and I haven't made this tutorial too confusing then you should now have a WiPy temperature sensor that sends it's data through MQTT to the Adafruit IO dashboard.
Thanks and credit to Alex Bucknall as I used his tutorials in order to run this myself. If you'd like to read more about MQTT or want to try another tutorial then head to his tutorial at:
https://www.hackster.io/bucknalla/mqtt-micropython-044e77
Comments
Please log in or sign up to comment.