Pico W - MicroPython MQTT tutorial using umqtt.simple library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to Don's key Tech in this video I am going to show you how to connect publish And subscribe to mqtt or mq Telemetry transport topics with your Raspberry Pi Pico W running micro python firmware using the umqtt.simple library I have here my Raspberry Pi Pico W running the latest micro python firmware and I will be using that funny IDE running the latest version which is Sony 4.0.2 I also have my mosquito mqtt broker running in the background which will serve as the middleman in our mqtt message Exchange we will be using the library micro python to mqtt.simple in connecting with our broker unlike the esp8266 and esp32 which has the module pre-installed in its firmware the Raspberry Pi Pico or Pico W needs to manually manually install it but it really is just easy all you have to do is just click the tools here manage package search for the umqtt simple then click the first row in the result set and then click install now once the installation is finished then just click close And I'm going to close it here to verify the installation then check the live folder it should contain the umqtt that simple library and in the terminal window what you can do is type in the following type hit enter the ignore error is encountered then the library is installed correctly once the installation is okay then let us create a sample program that will allow us to connect subscribe and publish to mqtt topics the code that I am showing you here is available in my GitHub repository we are going to program our Raspberry Pi picot to do the following and you will subscribe the topic LED and turn on the LED opener up depending on the message coming from our publisher next we're going to publish Eco topic called temperature and we're going to send in adami temperature reading every five seconds so start the testing all we have to do is just copy this boot.py and main.py into the file system of your micro python device so just right click and upload click into the micro python device once it is uploaded what you would do is just click run and then send eof record but you can see there is a message here that I am now connected to the mqtt broker running in my background in here so you can see there is a Connect knowledge message here in the Shell terminal for demo purposes we will be using this application called mqttx if you are not familiar with mqttx then I have a separate video on how to use this application I have configured my mqttx to connect to my local mosquito broker and subscribe to the temperature topic so once you have put the IP address here just click connect and once you are connected click the Subscribe button in here and create the new topic called temperature once you are subscribed then you would be able to see the random temperature readings coming from a Raspberry Pi Pico W every five seconds if we check our mosquito terminal then you would see that the mqtt messages are being received and then it is being published by our mosquito mqtt broker now that we're done verifying the publish command the next test that we're going to do is to verify if we are able to receive qtt messages from the topic that we subscribed in I have subscribed to the LED topic so that if I receive an own message then I would turn on the built-in LED of my Raspberry Pi Pico W so let's try this one I'm in LED and then the message is on click Send there but you can see the built-in LED turned on if I click off if I type in off then send and you would notice that the LED turn off let's try one more time then the oil and then send so it turns on and if I click off then it turns off then it means that we are able to subscribe and able to receive mqtt messages from our broker would you like to know how I did this then let's start exploring [Music] foreign discussion by discussing this both.py the boot.py is the standard micro python file that gets executed first when you your device reboots in the case of our project we are using it to connect to our Wi-Fi network just remember to change these two variables to match your network configurations the name.py is the code that we will use to connect publish And subscribe to our mqtt broker using the umqtt.simport as you can see the first lineup here are importing the necessary packages needed to connect our mqtt broker okay the next line which is this one we are going to define the mqtt broker IP for DNS name here and create the client ID the topic that we're going to subscribe and the topic that we're going to publish next we're going to configure the built-in LED of our Raspberry Pi Pico W or output so that we can control the turning on and turning off of it next these two variables will be the variables that we're going to use to publish the interval cycle which is 5 Seconds and then we have these three functions in here that do different things first the sub underscore CV is our callback function that gets called whenever an mqtt message arrives from our broker since we are only subscribed in one copy then we just check the message if it is on or once we see that it is on then we're going to turn on the LED otherwise we turn it off next the reset function is use in case an exception of word calling the machine that reset lastly the get temperature meeting is used to generate I mean sensor readings by creating a random number then change the main entry point of our project in the in the main function we set the call back of our subscribe and as you can see here we have subscribed to the topic LED in the loop function we check for mqtt messages by using the API check messages and then at the same time we're going to send the publish command every five seconds so there is the API here which is the equity.client that published so basically that is how the code work as I have said it's really easy to connect publish And subscribe to mqtt topics with your Raspberry Pi Pico using the unqtt.simple library the companion write up of this video contains much detailed explanation of the project the write-up code maybe pop in the description of this video I hope you learned something copy exploring
Info
Channel: DonskyTech
Views: 7,308
Rating: undefined out of 5
Keywords: mqtt, mosquitto, micropython, picow, raspberrypipicow, umqttsimple, mqttx
Id: THUGLRGuOU8
Channel Id: undefined
Length: 8min 44sec (524 seconds)
Published: Wed Feb 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.