Wi-Fi on the Raspberry Pi Pico, ESP32 and Arduino in MicroPython (shorter version)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
using Wi-Fi with your microcontroller project opens up a vast range of possibilities you can connect to the internet to send and receive data to web services and you can create a web-based interface to allow you to use a browser to control and interact with your project so let me show you how to set that up [Music] hi and welcome to bites and bits there is a wide range of microcontroller boards on the market these days which come with a built-in Wi-Fi chip and that allows you to connect your project up to your local Wi-Fi network or even generate your own local Wi-Fi network directly from your microcontroller board and of course this opens up with a vast range of opportunities for expanding your project work um you can you can use existing Wi-Fi networks to obviously connect out to the internet which allows you to send and receive data to various web services and so on and we can also then connect various devices to our microcontroller board over this wi-fi network to both gather data and control and see what's happening inside our projects so in this tutorial I'm going to do a quick overview to get you up and running with micro Python and and working with your Wi-Fi chips to get a working connection and that will allow you to connect to both the internet if you're connecting up to your um home Wi-Fi network for instance and we'll also have a look at creating an access point where we can actually just use a standalone Wi-Fi network generated by your your microcontroller board and the one we're going to be using in this tutorial is the Raspberry Pi Pico W and again we're going to be working in micro python but but this only thing we do on here will of course um work equally well on any other micro python enabled um Wi-Fi enabled um microcontroller such as your esp32 or or some of the Arduino boards um as I said this is almost like a quick tutorial which will get you up and running as fast as possible um if you want to have a full explanation of exactly what is happening inside the software and on why we're doing certain things then please do have a look at my full tutorial and I'll put links to that in the description down below um so without any further Ado um let's get started and use our PI Pico W to connect to Wi-Fi so using Wi-Fi with your micro python project it is really a two-step process the first is to actually connect to a working Wi-Fi network and as you'll see in a second we can either join an existing Network like your home network or we can actually get our micro python device to generate its own Wi-Fi network but when we get to the actual software then the the main hard work is being done inside this package called Network so that contains a number of sub objects and so on which handle the actual Hardware interface with the Wi-Fi chip so we need to import that of course at the top of our code this next bit here I we we do need to set up some Network credentials um so I'm really just putting my personal networks credentials inside this little object so that I can then pull them out of there without actually revealing my passwords that's just sort of for my own security um but to be able to all you need to do is to actually use string values anywhere inside your code here and and that of course will be fine for you so we then get into the actual setting up of the interface itself so we're going to create an object here and we're going to use our Network package and use this WLAN method and with that we will actually then tell it what type of connection we want to make and there are basically two types of connections that we can choose so this one I'm specifying here so there's this network.sta underscore iaf it is a constant defined inside this network package and that will then Define this as a station interface and what that means is that we have got an existing Wi-Fi network so for instance our our home network or or some other network which is is being produced by some other hardware and we want to connect to that so that we can access devices on that Network or or indeed if that one is of Internet enabled we can then go right on to the wider internet the other option which we'll see in a second is the access point interface and that's where we don't have an existing Network and we want our Raspberry Pi Pico to generate its own network which where you can then get devices to join into but let's go through this station interface to begin with because that's the one which most people will be using so we tell our our system that that's the type of connection that we want to make we then activate the hardware with this method call and we then call the connect method supplying it with our Network's details and that should start the connection process so we can see down here we we jump then into a little while loop in this code and what we're doing is we're just simply waiting for that Wi-Fi connection to be um created um so our our Raspberry Pi speaker was going to contact our router it's going to negotiate a connection and and get and ask for an IP address and so on and that's really what's happening here so we can check the status of our connection using this method and that will return us back a set of values and we can see those values listed out here so again each of these um are our constants defined inside that Network package but this is the actual value that they relate to so before we start of course we'll be in the idle State once we initiate the connection we will go into status one which is the connecting and then after a while we will either get a successful connection which is denoted by a status of three or if something goes wrong we will then get Negative statuses and depending on what those are so passwords are not working and we didn't find that access point or just a general failure and we will get back a status on that and that's what this little bit of code here is doing so it's checking to see if we've got some sort of either success or failure status coming up otherwise it will just simply sleep for one second and then just wait for that to happen So eventually then we will either fall out of that Loop and we will either have a connection or we won't so we're checking here first of all if we have a negative status and of course that denotes an error or we will get an a status of three which means that we do have an actual Wi-Fi connection and we will then just simply print out some details so once we connect we can actually get hold of the connection parameters using this ifconfig method and that will return us back a list of four values so the first value is the IP address which is what we're going to need to get hold of because that will tell us what IP address our router has assigned to us and that's what we'll be using to make contact with our Raspberry Pi Pico and the rest of it then is just our our normal sort of network parameters where we have our subnet mask our Gateway which is an effect or a router address and our DNS server which in effect will be our router again the Raspberry Pi will generally use the router for DNS information so let's try that out on our Raspberry Pi Pico and just confirm that we actually get something happening so I've uploaded that code to my Raspberry Pi Pico and I've used the file name connect.py for that um all of the code that I'm using in this tutorial I will create a GitHub repository which you can then get access to to download all the code instead of having to type it in yourself but if I then import that file in into my into my system to get that to run we can see there that it is now trying to negotiate a connection with my router and it's managed to do that and here we can see that has now come back and telling me what has happened so my router has assigned me the value of of this IP address so that is my IP address on my local network and that will allow me to have other devices as we'll see later on to connect to my Raspberry Pi Pico now we do need to make sure that we know what that device or that IP address is and generally if you're connect reconnecting to the same network the the router will generally give you the same IP address um each time you connect or of course you can configure your router to look at your Mac address and to specifically assign that IP address to you but that is something that we do need to have um so whenever you do run your projects you will need to work out what that IP address is but anyway we now have our Raspberry Pi Pico connected to my home Wi-Fi network and we're ready to then go off and actually use that let's first of all have a look at the other way around off achieving this Wi-Fi connection by using an access point so this is the code then to go into access point interface whereas I said our Raspberry Pi Pico will generate its own local Wi-Fi network so again we're importing our Network package here we will need to Define our SSID so SSID of course is the broadcast network name that will pop up as the name of the network whenever you try to connect to it we do need to define a network password for this so again make sure you choose something sensible for that and then we get into the actual setup so again we're initially set telling it we're using this WLAN method to tell it what type of interface that we want to set up and so this is the predefined constant which will tell it to use this access point mode we then need to configure our our access point and really all there are a number of things we can do in there but the basics are that we need to set up our SSID value and our password so again we're using these named parameters inside this method call and once we've got that set up we then simply turn on our Wi-Fi interface using this active method and then our our system will try to initiate its wi-fi system so all we need to do there is to Simply have a little wit just to make sure that that comes up so we're just checking that active flag and to see that it is um well while it's false that means that it is in the process of setting up the network so once it goes true then we come out of here and that's everything set up so again we can use this if config method to pull out those four values describing um how the network has been set up so the IP address then will be the IP address of the Raspberry Pi Pico on its own internal Wi-Fi network and and that will generally be the same every time you set it up it will it will assign itself its own IP address and the rest of course then and the subnet mask Gateway and DNS server um don't don't really matter if there's obviously gateway then if you are connecting with this that's the that's the and the Raspberry Pi Pico will I guess it's on an effect router but you will find in a second that the DNS server we don't actually have that because of course we're not connected to the real internet and that really is the difference then in this access point and we don't rely on any external Wi-Fi networks we can't have this setup in the middle of a field in the middle of nowhere but of course we don't have internet access but let's run this just to make sure we can um just verify that that all does happen so this time the file I need to import is AP connect and again that will then start that code and we should then see in this window over here this is a view on my mobile phone we should see our Network pop-up here so let's run that there we have it active and hopefully that should pop up in a second on my system and there we go so if I wanted then of course I can connect to that I've already entered in my details for this so that should then allow me to connect so there we are again it's saying that there's no internet and my phone will probably complain about that in a second there we go and we can say I'm actually no we'll we'll just use that Network so my phone is now connected to my Raspberry Pi Pico if we want to talk to the Raspberry Pi Pico that is the IP address on this network that we need to talk to and at this point then we are in exactly the same situation as we were with the station mode where we have our Raspberry Pi Pico connected to a Wi-Fi network and it being assigned a particular IP address on that Network everything else after this is identical for both types of connections of course the only difference being in this access point mode we do not have internet access so let's have a look and see what we can actually do then with this system so for the rest of the tutorial I'm just simply going to be using my pipe Eco in the station mode which will give us the sort of full access to the internet and everything else but as I said everything is the same for access point mode so the first thing we'll look at doing is to actually connect out to the internet and to use some web services um so for this we need to import an extra package into our system which is this request package or in this instance of course we're using the micro python version of the request package and that will allow us to handle um various Communications on our Wi-Fi network so we basically go through and we set up our our connection so at this point here we have our Wi-Fi connection set up and the test that then is the code for connecting up to a as well in this instance a web API so you will need to of course work out how to use the web API so I'm using one for this time API dot IO and that is a website that provides free timestamp information over over the web so for this particular API we need to use what's known as an HTTP get request so if we visit this web address we will get back some Json data which represents our current time zone information so again we have got an API endpoint URL there and then in our query string we are giving it some data which we want to collect and so we're saying that we want to have the Europe London time zone because of course I'm based in the UK but to do that then we are using this you requests package and we're using its get method to send an HTTP get request so once we send that get request it will send us back some information and we're going to catch that in this response variable now I'm I'm just putting a little bit of timing around that so we'll see how fast our Raspberry Pi Pico is able to get hold of this information but once we get that response back we will in effect we will have some content for our response so this is actually an object which is part of the shoe request package and one of the attributes in that object is the content and that is a byte string version of the information sent back from this get call now I've already said that this is going to come back as Json and that is one of the standard types of or formats of data transmission over over the web so Json is your your JavaScript um object notation um which is which is a cross platform uh text based um data format which which is actually human readable as well as we'll see in a second but of course we will need to um convert that text string into data values so the actual parsing of that um is something to do but of course our response object has a built-in method because this is a standard thing we need to do we can just simply call that method and that will then generate a dictionary object where we have variables defined as our indexes in our in our dictionary and then of course the values of those being the data values sent back to us but let's have a look and see how that works then so if I import the get time.py code that will set that all running so again wait for a connection and once that pops in we should now be connecting out to that web service and we can see here that um we're getting our response back from our web services this is the raw data coming back from the web service and if you're familiar with Json you'll be able to notice that that is what we're getting so we have here value so we have the name of this variable followed by its value and so on so we can see here year month hour minute and and so on so let me let me just stop that for a second just so we can see what's What's Happening Here um so again um we've got that Jason coming back we've seen there that we use that Json method to extract the time value so if we look at the time value here so that's where it's held in the return data and we got that value and we can see there that our micro python has correctly picked that out and we've again got that that Json method returning us a dictionary of all of the return data now one of the important things here is um that I did put in some timing to show us how fast our Raspberry Pi Pico was accessing this data so there we have two seconds or well you can see here it's between two and two and a half seconds to actually complete that um trip out to the API and to gather back that data now the code that we've been using here if you noticed um it's it's a single line of code to perform that get request and our code will actually sit and it will be blocked on that line until we get a value coming back from that get request so do bear in mind that when we're using this simple method of of accessing data your code can get held up for about two to two and a half seconds on this particular one anyway um sitting there just waiting for a response back from the server so if if you do embed these get requests in the middle of some time critical Loop um things are probably going to go wrong for you so do bear that in mind there are ways around that which I'm not going to look at in this tutorial and this tutorial is really going to be the basic setup and basic usage of the Wi-Fi but we can of course go to concurrent programming and asynchronous programming which I'll cover in a further tutorial where we look more deep into a proper rest API for our PI Pico or micro Python and that will also cover then sort of multi-processing to to get around these blocking codes but for now then we we have our connection onto Wi-Fi we can access web services over the internet and we can see here that the Json data we're turning back we can very easily parse that out and then gather the data that we want so the flip side of being able to send these HTTP get requests is that we are able to then receive them so let's have a look at how we can do that and this is where we now are able to have one device on our Network perhaps our mobile phone or laptop and connecting to our PI Pico and then gathering information from that or some other data so if we look back at our code then um to do this we're going to need one extra package imported in which is the socket package which will actually let us open up a port on which our PI Pico will be able to listen and then if we come down in our code so the actual generating the Wi-Fi connection is exactly the same as before but then down here if I can use my brightness to go down we actually come down to actually open up a a port on our Raspberry Pi Pico which will listen for other devices trying to connect to it so you can see here that we are setting up in this line here these this block of lines a socket so we need to tell our socket um which address our IP address we're going to be using and then which Port so supports are basically like doors um which we can open onto the network and then we can pipe or stream the information from that into our socket so remember from our ifconfig system we were able to gather what IP address our router had um allocated to us so we're basically saying I want to create a socket it will be connected up to this IP address which is our raspberry Pico and Port 80 which is the standard HTTP port number so we're then going to create a socket we're then going to bind the socket to that address information so at this point we have now created an effect a corridor from this port um on our Raspberry Pi Pico into this socket which will let us then receive information and send information and we then tell our system to start listening to that port so the number are in the brackets on here if you check the documentation and that is the number of queued messages that we are allowed to process um so basically we're going to be processing one message have One queued up but any um subsequent messages before we've handled those will just get rejected and then we can just we're just printing out here so we're saying okay let's print out um which IP address we're listening on and of course that will be the IP address that we we then need to connect to so in our Loop here so at this point we now have our Raspberry Pi has opened Port 80 and is now listening on that port so we on this line here we are using our socket to accept a connection so that is is a blocking method so our basic our Raspberry Pi Pico will sit at this line of code waiting for somebody to connect with it and that actually does stop the Raspberry Pi pigger from doing any other operations at that time it will sit here until somebody connects um so do bear in mind if you're using this type of code um that will stop the other parts of your code from running as I said there are always running that but I'll cover that in a different tutorial so when we get this um connection being made this particular method will return us back two values um one of them will be a client connection and one of them will tell us which IEP address that client was connected to so to get hold of the information we simply need to just ask it to receive a block of data from our client connection and that's what's happening here so we're going to say I want to read in up to one kilobyte of data that's what this number here is saying if it's less than that we will just read whatever message has been sent and we're going to collect that in this variable and for now we're just going to print that out so we can see what actually is coming over when a client connects to you it's it's good form to send back some sort of response so we're just going to send back it's just a very simple text message saying that we have received your message and then we're going to close down the connection and that really then just releases and resources which our PI Pico has allocated to this particular um event and then it will go back and it will then try and listen for the next client connection so let's have a look at this working and we'll see what information we've got coming through so the phylum I've used for this one is the receive get in that abbreviation so that should fire up our station connection and then set it waiting listening for some device to connect to it so there we go um so we now have our Raspberry Pi Pico sitting at this IP address and it's now listening on Port 80 which as I said is the standard HTTP address so if I copy that and then just put that into my browser we can see a number of things happening um so first of all our browser has got that message coming back from our Raspberry Pi paper so it sent a get request to this IP address as we can see over here we'll go through that in a second um our Raspberry Pi Pico has picked up a number of um pieces of data and then it has responded back with that message so let's have a look at what's happening over here so if we um decode this a bit we can see that we have a a get request coming in and if I just highlight the actual get request itself so the get request is everything down to there and this then is a second get request coming over and this is what it is sent across whenever one device is trying to connect with another over an HTTP connection and this is what I was actually happening every time you browse a website so the B at the front of this just denotes that this is actually a python byte string which is slightly different to a normal string but we will we'll deal with that a little bit later on but if we have a look at the actual format of this request then so this is a get request so so the B apostrophe is just part of the way it's been printed out here the actual data that we receive starts with that get and if I highlight that we can see that the slash r slash n is actually a new line character so the first line of our get request contains three um in effect words so they're three three sections separated by spaces so the first is rhttp method which as you can see in this one is a gap request there are other methods such as post and so on but we're not going to worry about those in this particular tutorial the second word is the resource that we are trying to access so we just accessed it by IP address we didn't specify an actual page within this um IP address so we will just get back the default so slash is just your default resource the third word then is our HTTP protocol which for us at the moment doesn't really make any difference the two bits that we're interested in now are our get and especially then this resource so let's have a look at how we can use these get requests and the resource value to control and monitor what's happening inside our PI Pico project so we can create a number of these endpoints which allow us to control things within our project so if we aim to allow people to have a web page control of an LED we can set up three endpoints so the first would be our default page which allows people to call up and view the actual control web page with two buttons on there and those two buttons will then activate an LED on endpoint and an LED off endpoint and activate those we simply need to send get requests to those web addresses as we said in that description then we're going to use a web page with some buttons on it so we need some HTML to generate that page and I've created a little file here which is just an HTML file and I've uploaded that onto the Raspberry Pi Pico so that's available to my project so if we go through that we're just setting up a normal page well on that page then we have two buttons and when we click those buttons we will send a get request so by actually visiting a web page and that sends the get request so when you click the button the browser will try and go to this web page and that will then trigger our LED off end on endpoint and on the LED off endpoint I'm then going to add a little token into my HTML code and we're going to use Python so when it's um sends this piece of block of text back it's going to replace this token with either on or off depending on what it's just done to the LED so let's say the actual python code to get this all hooked together so in our python code then we're doing everything the same down to our main Loop so again we're going to set up some control for our LED and we're going to monitor its state with this led State variable so we do our normal thing where we're waiting for a client connection we receive the data we're now going to decode that byte string and turn it into a proper python string and that's what we're doing here with this decode and mostly over the web we get utfit encoding coming across so we're just going to let it um decode that byte string in that way and we're then going to print out this new raw request which will actually be a better string formatted version of it now we saw whenever we looked at the get request um basically the first line is split into three words so we're going to split the whole request up into words so doing the split with nothing in the brackets um just we'll split it on spaces and we know then that the very first thing that we get the very first word we get is our ahttp method and then the second string is actually the request URL which is this um end point that we want to pick out so we're then looking to see which end point we've got so I'm using the string find method here to look for this string within our URL and sometimes the URL might have other data at the end of it so if if we do find that this string is in there we can be pretty sure that then is somebody requesting that led on endpoint so if we do receive that we will set our LED State and this is this is the value this is the variable tracking our state and then we'll actually turn the LED on if we get an off value then of course we'll do the off um path methods if we do not receive either of those then it's either going to be the default page or or something else and at that point we're not going to do anything with the LEDs because on every single request we get we're going to send back a version of that simple led.html file so in here we're just working off whether we need to say on or off we're then bringing the file in as a text file and we're then doing the replacement of that token so whatever we've worked out on or off we're going to replace that token that we created with that new bit of text and then we're going to send that back to the browser and the browser will recognize that that is a web page some HTML code coming across and it will display it in the proper way and again now we're going to close our connection so in this way we are intercepting our get requests we're working out what endpoint the person has requested we're then able to take some actions within our project to um answer that request and then we're sending back some information to let the user have some indication of what's actually gone on within our project so let's set that up and running on our PI Pico so if I import the simple LED code our system should set itself up and then wait for some connections so again if I browse to this web address we should get our web page interface coming up and you can see there we have our get request coming through to our Raspberry Pi Pico and on the on the actual circuit board we're looking out for that little red LED so if I turn the LED on we should get that coming on and we can see again in our um Pico then we've got a get request to the LED on endpoint and that of course turned the LED on and it's being reported back then in our web page itself and if I turn that off we should see it again working on there and please don't worry about the other circuitry on that board um I'm I'm part way through making the more advanced API version of this code so again all of those other bits and pieces will be involved in that so that's pretty much it for this simple web tutorial so we're not able to generate a web page that acts as a front-end control panel for our project we've seen that our micro python can respond and intercept these get requests and then take action we can use buttons on our project page to control various devices and then report back to our user through the web page by input by by putting various bits of data into the HTML code so that should cover everything you need to Wi-Fi enable your micro python projects as I said at the beginning this is very much a basic web interface I will cover a more in-depth rest API version in the next tutorial so please do make sure that you subscribe to my channel to make sure you don't miss the notifications as to when I release that next video so I hope you've enjoyed this tutorial I look forward to seeing you again sometime very soon so bye for now for more games programming the electronics projects and Retro Gaming please make sure you like this video subscribe to my YouTube channel and visit my website
Info
Channel: Bytes N Bits
Views: 4,667
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, Wi-Fi, ESP32, Micropython
Id: ltozEpvR4Wg
Channel Id: undefined
Length: 36min 2sec (2162 seconds)
Published: Sun Jan 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.