Getting Started with Raspberry Pi Pico W for IoT: Micropython and MQTT
In this tutorial, I will show you how to build an IoT project using Raspberry Pi Pico W, MicroPython, and MQTT. By the end of this step-by-step, you’ll be able to build a system for collecting sensor data, publish it to the cloud, and visualise the information on a dashboard.
Raspberry Pi Pico W Microcontroller
Let us begin by taking a closer look at the Raspberry Pi Pico W microcontroller board. The Pico W is part of a family of microcontroller boards from Raspberry Pi that includes the Pi Pico, Pi Pico H, the Pi Pico W and the Pi Pico WH.
The main difference between the Pi Pico W and the other Pico boards is that it comes with inbuilt WiFi connectivity, hence the W. This makes it well suited for building Internet of Things (IoT) applications as I’ll demonstrate in this article.
Selecting the Sensor ( BM2480 )
To build an IoT project using the Raspberry Pi Pico W, the first thing that we need to do is to select a sensor that we are going to use to measure physical quantities such as temperature.
For our project, we will use the BME280, an environmental sensor built for indoor monitoring of temperature, pressure, and humidity. It exposes an I2C interface for reading the sensor data it generates. I2C is a serial communication protocol that uses two lines for exchanging information, a Serial Clock Line (SCL) and a Serial Data Line (SDA).
Since our Raspberry Pi Pico W has two I2C interfaces, I’m going to connect the BME280 sensor to one of the interfaces.
Raspberry Pi Pico W and BME280 Wiring and Circuit Diagram
When you buy the Raspberry Pi Pico W, you can purchase it with the pin header soldered in, or you can buy the pin header separately and solder it yourself. I purchased mine without the pins and soldered them onto the microcontroller board we will use for this demo.
After soldering the pins, I placed my Raspberry Pi Pico W on this breadboard and used jumper cables to connect it to the BME280 sensor.
Let’s visualize the specific connections by viewing the circuit diagram.
I have my BME280 sensor connected through the I2C interface with Serial Data Line (SDA) and Serial Clock Line (SCL) connected to pins 1 and 2 of the Raspberry Pi Pico W, respectively.
Further, the ground on the BME280 connects to pin 38 of the Pico W, which is also ground. The VCC of the BME280 sensor is connected to pin 36 of the Raspberry Pi, which provides 3,3 Volts of output.
Installing MicroPython on Raspberry Pi Pico W
While you can program your Raspberry Pi Pico W using languages like C and C++, the easiest language to use is MicroPython. MicroPython fully implements Python 3 programming language but runs directly on embedded hardware like Raspberry Pi Pico.
To program the Raspberry Pi Pico W using MicroPython, you need to begin by installing MicroPython on the device. You achieve that by connecting the Raspberry Pi Pico W to your computer and then dragging and dropping a UF2 bootloader file onto the device.
Let’s go through the process step-by-step.
First, you need to get a Micro USB Cable to connect the Pico W to a computer via USB.
Next, push and hold the BOOTSEL button on the microcontroller board while you plug your Pico W into the USB port of your computer. When your Pico W is connected to the computer USB port, release the BOOTSEL button.
When you do that, you’ll notice that a Mass Storage Device called RPI-RP2 will populate on your computer.
At this point, you will need a copy the UF2 bootloader file to flash your Pico W with MicroPython, which you can get by visiting the documentation section on the Raspberry Pi website.
There is a list of instructions to follow and a link for you to download the UF2 file.
I’ll click to download the Raspberry Pi Pico W version of the file.
Then I’ll copy the bootloader file into the RP1-RP2 storage that was mounted when I connected my Pico W to the computer.
The Pico will automatically detect the file and reboot. Then it will be ready to be programmed.
Downloading and Setting up Thonny IDE
Next, to program the Raspberry Pi Pico W using MicroPython, we need to use an integrated development environment (IDE) that allows us to access the Pico W from the computer and program it. The easiest MicroPython IDE to use for this case is Thonny.
You can download Thonny IDE by going to Thonny’s website and downloading a version that works with your computer’s operating system.
I’ll download Thonny for Windows, and install it. After installation, open Thonny, go to the bottom right corner and click to select where you need your program to be deployed.
Since your Raspberry Pi Pico W is plugged-in, it should appear on the list. Select it.
Now, you should see the Thonny terminal indicating you are now working on the Pico W. To test that our Raspberry Pi Pico is working, we can run a hello world command by executing:
in the terminal.
Programming Raspberry Pi Pico W Using MicroPython
Now that my development environment is set up and ready, I can begin writing the code for my IoT project.
Before I write the code, I’ll need to save the program file on the Raspberry Pi Pico W. To do that, I’ll go to File, then click on Save. Then on the Where to save file? Prompt, I’ll select Raspberry Pi Pico.
Now, if you want to automatically run your program each time your Raspberry Pi boots up, you must give your File the name main.py, which I’m going to do.
So I’ve got my program file saved on my Pico W, now I can start writing the code on it.
Since I’m going to read data from the BME280 sensor and publish it to the cloud using MQTT, I’ll need to install the respective MicroPython libraries on the Pico. So, I’ll go to Tools, and select Manage Packages.
On the Manage Packages Search Bar, I’ll type in bme280 and click on search.
Then I’ll proceed by selecting the MicroPython-bme280 search result, and clicking on install thereafter.
Next, to install MQTT Client library I’ll go to the terminal, and connect my Raspberry Pi Pico W to the WiFi using this set of commands.
Next, I’ll install MicroPython upip.
I’ll use pip on the terminal to install an MQTT library called micropython-umqtt.simple. However, I’ll need to start by installing micropython-umqtt.robust (as shown below).
Then I will go to the micropython-umqtt.simple library.
Now, it’s important to mention that the installation of the MQTT library can present some challenges, as I’ve experienced. If you come across any, here are some troubleshooting tips.
MQTT Installation Troubleshooting Tips
First, make sure you are using the latest MicroPython firmware.
If that doesn’t solve your problems, use MicroPython to navigate to your Pico W file directory and detele a folder called lib.
These steps usually solve your difficulties.
Programming the Raspberry Pi Pico W
Now that the required library packages for the IoT project are installed we can proceed.
Let’s go through the code that I’ve put together.
First, I’m importing the machine library to gain access to the pins of the Raspberry Pi and for I2C communication.
Next, I import network to access my WiFi network, utime for time delay, bme280 library, and lastly the MQTT library.
Then I initialise my WiFi connection details.
Next, I activate my WiFi connectivity on the Raspberry Pi and connect to my WiFi network. After that, I check to see the status of my WiFi connectivity.
Then I initialise my Raspberry Pi’s I2C interface
Next, I define a method for connecting to my MQTT Broker.
I’m using a HiveMQ Cloud MQTT broker, which I’ve already created, and I will show you how to do that shortly.
First, let’s quickly go through the connection details.
Now, it’s important to mention that these connection details are specific to a HiveMQ Cloud MQTT Broker.
Under server, you put the MQTT broker URL address. For port number you put 0 even though my broker is at port 8883. Next, I have MQTT client username and password, which I’ve set on my HiveMQ Cloud portal.
Keep Alive is set to 7200, and I’ve set SSL to True.
Then finally, under ssl_params, I’ve put the server hostname of my MQTT broker. Again these settings are specific to a HiveMQ Cloud broker, so if you use a different broker, you probably will have different settings.
Then I connect to my MQTT Broker.
Creating a HiveMQ Cloud MQTT Broker
Before we proceed with the code, let me show you how I created the MQTT broker my code connects to.
I’ll login to my HiveMQ Cloud portal.
When you sign up, HiveMQ automatically creates the broker cluster for you. Copy its URL by going to Manage Cluster.
And then you also need to specify the username and password for connecting to your broker under Access Management.
I’ve already copied these details, so let’s get back to the code.
Publishing Sensor Data to the MQTT broker
Moving on, I’ve created a method for publishing messages to my MQTT broker, and it prints publish done when the message is successfully published.
Finally, I have my superloop for executing the main logic.
I read the sensor data from the BME280 sensor and assigning it to temperature, pressure, and humidity variables.
Then after printing the sensor readings, I use the publish method to publish the sensor data as MQTT messages under the topics:
- picow/temperature for temperature data
- picow/pressure for pressure data
- picow/humidity for humidity data
Then I delay for five seconds before reading and publishing again.
Now that I’ve my code ready, I can deploy and run the code on my Raspberry Pi Pico W.
To do that, I’ll go to the Menu and click on Run. And then I’ll select Run current script.
As shown below, my code is running and publishing the bme280 sensor data to my MQTT broker.
Subscribing to and Visualising MQTT Data from Pico W
To complete our project, we need to subscribe to this MQTT data from anywhere on the internet and visualise it to know what the current temperature, pressure and humidity is in my demo room. To do that, I’m going to use Node-Red.
If you are unfamiliar with Node-Red, this is a very useful and easy-to-use-drag-and-drop tool for connecting online services.
I’m using MQTT nodes to connect to the same HiveMQ Cloud MQTT broker and subscribe to picow/temperature on the first node, picow/pressure on the second node and picow/humidity on the bottom node.
Next, I’m passing the MQTT payloads to function blocks to strip out the decimal value since the sensor reading comes as a string with units of measurement.
Then I pass the decimal values to gauge visualisation nodes from a dashboard package that I installed on Node-Red.
When we open up the dashboard page, you can see we have a nice visualisation.
I’ll breathe into my sensor to change the sensor readings. When I do that, you can see the values change instantly.
Now that I’ve successfully demonstrated how to build a simple IoT project using Raspberry Pi Pico W, MicroPython and MQTT, I would like you to try it out and share your experience in the comment section below. If you are in the IIoT space, you might be interested in reading this article on How to Send & Receive MQTT Sparkplug B Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud.
Sign up for a Free HiveMQ Cloud account to get started with building IoT application using MQTT.
About Kudzai Manditereza
Kudzai is an experienced Technology Communicator and Electronic Engineer based in Germany. As a Developer Advocate at HiveMQ, his goals include creating compelling content to help developers and architects adopt MQTT and HiveMQ for their IIoT projects. In addition to his primary job functions, Kudzai runs a popular YouTube channel and Podcast where he teaches and talks about IIoT and Smart Manufacturing technologies. He has since been recognized as one of the Top 100 global influential personas talking about Industry 4.0 online.
Follow Kudzai on LinkedInContact Kudzai