Skip to content

14. Interfacing

Group Project

The group assignment for this week was to “compare as many tool options as possible”. In our documentation, Angelina Yang and I compared the tools we used, as well as a couple other presented by Neil on the schedule. The link to our group documentation can be found here.

Week Assignment

This week, I worked with Evan Park to setup a MQTT broker, connect to it from a Raspberry Pi Pico W, and send messages from a GUI through this broker to the Raspberry Pi Pico W, ultimately to simply blink an LED.

Thonny IDE

Originally because of a tutorial we were following, Evan Park and I used Thonny IDE for this week’s process, as this IDE works primarily in Micropython, the language the Raspberry Pi Pico W takes which combines many of the benefits of other programming languages. After installing Thonny IDE, I connected it to my Pico W by holding down on the BOOTSEL button on the Pico W while simultaneously plugging it in (through USB) to my computer. I could then add the UF2 file (included in the file download) to the board in my files. This installs Micropython on the board so that I could program it. I could then select this in the bottom right corner as the Port I wanted to use. Later, when I would create my GUI and just wanted the Micropython code to run on the computer, I would reselect “Local Python 3 Thonny’s Python”.

MQTT Tutorials/Setting Up Raspberry Pi Pico W

Originally, Evan and I followed this tutorial which utilized a Raspberry Pi Pico W, Thonny IDE, MQTT, and Node-RED. Hive MQ also had a video walkthrough for this tutorial. Firstly, we connected a Raspberry Pi Pico W to Thonny IDE as explained above. I then ran print("hello, world") in the Thonny IDE embedded command terminal to make sure Micropython was working correctly on the board. Then, I ran these few lines of code in the terminal to connect the Pico W to Wi-Fi (will eventually be run in the code so might as well just put them in the code and then run it):


import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID", "Wi-Fi password")

*Note: insert Wi-Fi name where “SSID” is, and that Wi-Fi’s password for “Wi-Fi password” After eventually getting this to work, the Wi-Fi was connected.

I then had to setup the libraries and code to actually connect to the MQTT broker. I first installed used pip install upip as the tutorial said that I needed to use UPIP to install a library. For a while, I got confused because I read online that UPIP is outdated, and the newer version is MIP. I also installed mip, and tried both for upip.install('micropython-umqtt.robust). I was not completely sure what this does, nor what each of the components in this line did individually (other than install of course), but I tried this using upip and mip, and ofr umqtt.robust and umqtt.simple, another thing the tutorial said I would need. I also installed micropython-upip, as well as micropython-umqtt.robust and micropython-umqtt.simple in manage packages under tools. With some combination of these (I am still not entirely sure what), I could have the line of code from umqtt.simple import MQTT which would import this aspect of the library, allowing the Pi to connect to a MQTT broker.

MQTT Broker

Following along with the tutorial, we originally both created our own MQTT servers using Hive MQ, but eventually found this to be to complicated and just used the Fab Academy MQTT server. To connect to the Fab Academy MQTT server, it was very simple, as the username and password were both “fabacademy” and the server name was “mqtt.fabcloud.org” (I am only sharing this because it was already publicly accessible information, do not share this information if you wish your server to be private).

Pico Board

In order to allow this Pico to display its communication with other devices using the MQTT server, I created a board for it, which would include many output headers for I2C, a button, and an LED. This is the board looked like in KiCAD:

After I milled and soldered it:

Button

First, I wanted to publish an indicating message when the button was pressed. To do this, I used this code:


**button code**

To ensure that these messages were being published and for troubleshooting, I would connect to the network simply through the command terminal, by inputting this line: mosquitto_sub -h mqtt.fabcloud.org -p 1883 -u fabacademy -P fabacademy -t fabacademy The -h is the “host” or server, -p is the port, -u is the username, -P is the password, and -t is the topic to subscribe to. After sucessfully subscribing to the same topic I published to in my Pico code, I saw the messages pop up as I pushed the button on the board.

LED

Next, I wanted to communicate from the computer to the board. To display this, I would use the on-board LED. To do this I used a GUI.

GUI

When it came to setting up a GUI, Evan and I split apart. He wanted to use Node-RED, a block based, “browser-based flow editor”, to connect everything together. I was tempted to do this at first, but ran into complications trying to install Node-RED, and thought it would be interesting to create a GUI myself anyway. Thus, I decided to do it all in Thonny IDE. Setting up the GUI was not the difficult part of this, though. Receiving a message was what I was mostly confused about. Because I am currently taking AP Computer Science Principles, I have had some experience using the library Tkinter, and understood what it is and what is does. Initially, I still used Copilot to setup a basic button using Tkinter, and I would learn commands using the library from there. I did not know the commands to receive a message using MQTT though, nor how it worked. After much trial and error, Connor Cruz’s code, as he had already blinked an LED using a GUI. This is what the code for the computer looked like:


**computer code**

Since I was using his code for the computer, I also had to use his code for the Pico, as his code used specific variables. This is the code for the Pico:


**pico**

I then wanted to change the code up some, so I made it so that the user could input the amount of time they wanted to blink the LED for in the GUI. This is how that looked different:


**modified pico**

Week Reflection

This week I learned about interfacing a board or chip to a computer or other device through an MQTT broker. I also came to understand that there are many ways of doing this, MQTT being one of them. Of course, many (if not all) of these methods involve the use of Wi-Fi, but this knowledge nonetheless significantly expands my capabilities with a microchip/microcontroller. This week is arguably one of the most valuable if taken advantage of properly.

File Downloads

The files involved in the processes included in this week’s documentation can be downloaded here.


Last update: May 21, 2024