Today, we're going to look at sending and receiving data over the cloud using a Raspberry Pi Pico W and the popular MQTT protocol. MQTT is a publish-subscribe messaging protocol, or “Pub-Sub” for short, that enables devices to broadcast and receive messages in a simple manner. It has a lightweight and efficient design, making it a popular choice for network-capable microcontrollers such as the Pico W.
In this guide, we're going to send data from a Pico W to a cloud dashboard and use the dashboard to control real hardware on the Pico. Let's get started!
To get started, we're going to need an MQTT broker. Fortunately for us, there are heaps of free ones available on the internet and they're public. Check the guide for links to more. We're going to use Adafruit IO because it has some pretty cool dashboards and it's really easy to use. Head on over to Adafruit IO and create a free account.
Now you've got your free account set up, go and find your account key. We're going to use this later, so copy-paste it into Notepad. Create a feed, open the feed, go to feed info, and grab the MQTT keystring. Paste that into your notepad; we'll use it later.
Create a new dashboard, open the dashboard, and go to settings. Create a new block and I'm going to use a line chart. We need to connect our feed to the chart and, most importantly, change the show history to live because we want to see it update in real time.
That's it for Adafruit IO for the moment, but leave it open because we're going to want to have a look at that chart in the next step. So let's get the Pico talking to the dashboard. Open up Mu and the first thing we're going to need to do is install a new library. The library we're going to use is umqtt.simple. Add to tools packages and search for umqtt.simple. Note there's a number of packages that look very similar; we don't necessarily want the one with the underscore, so make sure you get the one without the underscore. the actual publishing
Want to make sure you're selecting the right package? Hit install and you can verify it's been installed by checking out the files section in Funny. Now head on over to the first example in the tutorial. Copy paste the code into a new file in Sony and save it to the Raspberry Pi Pico. I'm going to call mine publish.pi.
Before we run the script, we need to update a few details. Enter in your Wi-Fi details (there's a link in the guide if you're not sure about how to get Wi-Fi running on the Pico) and then head down to the MQTT details. You're going to need your Adafruit username, your password (which is the key that we copied earlier), and the topic (which is the MQTT by key for the feed we created earlier). Finally, we're going to select a globally unique string for our client ID (link in the guide if you need help getting started with that).
Now let's run this script in Thonny. Hit run and watch the console. You'll see it connect to Wi-Fi and then start publishing some numbers. If you wait, you'll see about every three seconds a different number gets published and on the right you'll see it graphed. This is graphing a sine wave. If you don't see the plotter, just right click in the console.
Now let's jump on back over to Adafruit IO and see if it's showing up in the dashboard. When you look at that, our data is showing up in the cloud via MQTT, the graph in Adafruit IO and the graph in the Thonny are identical. The data is coming through in real time. Imagine the possibilities if we were sending more than just a random number.
Now let's check out what's happening in the code and how this works. After we set up our connection details, there's just a few lines of code that are doing the real MQTT publishing. We're generating a random number here which is a sine wave and it's just one line of code that does the actual publishing. subscribing and publishing data
The publish one thing to note is that we're sending a string over MQTT. We always send a string and there's a three second delay so we don't hit our rate limit on Adafruit. But that's it, it's super easy to send data on the Pico W over MQTT.
But what about receiving data? Let's check it out. Let's add a button to the dashboard and control the LED on the Pico. Let's create a new feed, we don't want to mix up our sine wave data with our LED data, do we? Just like in the last example, make sure you take note of the MQTT by key field.
Now head on back to the dashboard you created before and we're going to add a toggle. Go to settings, create a new block and select the toggle widget. Now hook this widget up to your new feed. In the toggle settings, I'm going to set the on value to lowercase on and the off value to lowercase off. These are the messages we're going to send from the toggle down to the Pico. So we need to make sure the uppercase/lowercase is right as well.
And there you have it, we have our toggle but it doesn't really do anything yet. So let's head back to Sony and wire it up. Create a new file in Sony and I'm going to call this subscribe because we're subscribing to data in this example. Head on over to the guide and copy paste the second example. Just like before, update your Wi-Fi details and update the MQTT details. This time around though, let's change the topic to our new feed topic and give it a new random client ID.
Now let's run this code and make this LED blink. In the console, you'll see it connect to Wi-Fi and then subscribe and finally a message saying waiting for messages on your topic. Now let's head on over to Adafruit and hit that button. Are you ready for it? Where you look at that! And now let's turn it off.
All right, I know it's just an LED but imagine what else you can do with subscribing and publishing data.
I've got one more thing to show you. As we can see, the LED is on and the button is on in the dashboard. But what happens if we lose power to the Picker? We turn it back on and the LEDs off, but our button's still on. Now we're out of sync, let's fix that.
Let's go back to the code and we'll tell the Pico to update the button when it first starts to correctly reflect the state of the LED. This will keep the dashboard and the Pico in sync. We're going to go to the guide and go to the third example. Now this example is just a snippet, it's not their full code. I'm just going to copy these lines and put them just before the try while loop.
You might notice that the two lines just turn the LED off and publish the word off. Now let's run this. You can see the toggle is still on and when you look at that, the toggle just turned itself off.
Let's take a look at what's going on in this subscribe example. It's a little bit more complicated than the publish because we're doing a little bit more. Most importantly, when subscribing we need a callback. A callback is what's going to run when the Picker receives a message. The Callback receives messages for all topics, so if you've got multiple topics you can just use some if statements to handle it appropriately.
You can see here that we're Towing the mqtt client to use the Callback before we connect. That's important. And then after we connect, we tell it to subscribe to our topic. So if you had more than one topic, you'd have more than one line saying subscribe here. And then finally we have our while loop. This Loop just infinitely waits for messages, but a little note is a blocking call. So if you're trying to do other things like use other sensors at the same time, you'll need to look at maybe timers or threading. That's outside the scope of this guide.
Leveraging MQTT on a Pico W, we can easily join the Internet of Things and control hardware in the real world. In this guide, we just use the button on a dashboard, but that's just the beginning. Check this out: here's a demo I came up with that uses a rangefinder and an LED that works a little bit like traffic lights for maybe a water tank. These are not connected in any way other than MQTT; this one publishes and this one subscribes. As I get closer to the sensor, notice that the LEDs change position. This is all happening over MQTT via the cloud in real time.
If you have any questions or want to show off a cool way that you're using MQTT, let us know in the forums. Until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.