WiFi Control Your Micropython Project Using a Web Interface

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 baits and bits there are a number of microcontroller boards these days that come with a built-in Wi-Fi interface and this lets you connect your project to your local Wi-Fi network and undoing that opens up a wide range of possibilities um for extending your project work so for instance you can obviously use your Wi-Fi network to connect to the wider internet and that will allow your project to both gather data and push data up to various web services and websites or you can use other devices on your network to communicate with and control your microcontroller project so in this tutorial I'm going to take you through the basics of of using a micro python based microcontroller so we're going to use the Raspberry Pi pic or W in this tutorial and but again it will work with any other Wi-Fi enabled um device that's capable of running micro python such as your esp32 and various Arduino boards and so on and I'm going to talk you through and show you how to connect to your Wi-Fi network or how to use your Pi Pico to generate its own independent Wi-Fi network and then how we can then use that to talk to devices on the internet and also then how we can set up a web interface so that we can actually use a a mobile phone or laptop to connect to the pay pickle using a web page that the pi Pico generates and then use that to both communicate with and control our our little microcontroller projects now to say this is going to be um the initial basic um setup I will do a second tutorial which covers um in more depth how we can create a more robust um web um well it's going to be a rest based um web API for your Pi Pico which makes you makes it much more powerful system but in this one here we're just going to get this General setup ready and then show you how you can do some basic control of your project now this video is um the full version and it is a good sort of 45 to 50 minutes long um I will produce a second sort of summary Vision version version which will just get straight to the point of of what code you need to run but if you do want to understand exactly how everything is working then please do stick with this version as I'll take you through everything in in quite fine detail so um first thing to do then is let's get our Raspberry Pi Pico connected up and and we'll get started so just before we get to the actual code um just to make sure that everybody's in the correct place um you will of course need to have your python development system set up and micropython installed on your microcontroller so if you haven't already done that and please do have a look at my previous tutorial video which shows you how to set all of that up and I'll put links to that in the description down below now I'll also be going through quite a bit of code and different files in this tutorial so I'll put all of that into a GitHub repository and again I'll put links to that in the description down below so that you can download the whole package and get started on that code so let's have a look at the actual software we need to get this interface working so for our PI Pico to communicate over Wi-Fi we first of all need to connect to a Wi-Fi network and that's what this block of code here is going to show you how it works and the important bit here is that we are importing this network library and that is the one that contains all of the further classes and so on that allow us to control and work with the network interface chip so this block of code then um we are going to connect up to my home Wi-Fi network and for that we need to have the network ID so this is this SSID and that is the broadcast name that your Wi-Fi network uses well of course then need the password and all I've done here is I have just um buried my actual values inside a little class called Network credentials and that's just really so that I don't have to keep blanking them out on this video um but you really all you need to do here is just put a string in here with whatever it is so whatever happens to be um your your Wi-Fi connection so to make this all work then we are going to use this as I said this network library and the first thing we need to let me get rid of that stuff and the first thing to do is to tell it what type of connection we want to make so there are really two ways in which you can connect um up your your the Wi-Fi on the Raspberry Pi Pico so we can either use what's known as a station interface and that's where the Pico becomes just a device on your home Wi-Fi network so it's it's going to connect itself to any existing Network that it has that is in range for it um but we'll see a bit later on using an access point interface and that's where the pi pigal can actually generate its own Wi-Fi network and we can then connect devices into the Raspberry Pi Pico and we'll say we'll do that a little bit later on so once we have a tool that what where we want to connect we then just need to turn on the actual Wi-Fi interface and that's what this active method here is doing so once we've then turned on the interface we want to ask it then to connect to our Wi-Fi network and that's all we're doing here so again we're using this um wireless LAN object that we um have returned back from this method call so it turns us back this object we then call the connect method on that give it our network ID and our password and it should then try to negotiate a connection with our Wi-Fi router and that's what's happening then in this while loop so it's going to take a bit of time for that negotiation to take place and we just want to wait in this Loop until that either succeeds or fails so I'm just using a little Loop here which is going to wait for a maximum 10 seconds and it's going to check um it's going to each time it checks it's going to take a little sleep after that so we're going to do a check every one second on the check we're making then is looking at this wireless LAN status and that will return us back what are our current connection what the progress of it is so initially we will be in the idle status which is um value zero once we initiate our connect method it will go into this status connecting um then at some point it will either manage to negotiate a connection or the connection will fail and we can see here that we then have a number of field statuses um depending on on what has happened so again if we get our password wrong we'll get a minus three coming back or if we get a successful connection then our status will move to three and that's what this line of code here is doing is you're checking for either an error or a a success so if we do once we get that we can then break out of this Loop and we can then check and see what actually happened so we're checking here then first of all for an error and if we get an error then we will just come back and our our code will will end and it will just say that we had a connection field um you can of course as you've seen here we can we can actually look at what the error was and we could if we wanted this point here notify the user and perhaps go back and and retry or something but for the moment we're just going to Simply if it goes wrong we're just going to close the close the code down but if we get a successful connection then we're going to print that out we can then gather some information about our connection so this method here this ifconfig will tell us why our Wi-Fi connection has been set up and that returns back a list of four values so the first value on a list is the IP address so this is the IP address on the Wi-Fi on our local Wi-Fi network that the Raspberry Pi Pico has connected to um it then gives us our Network subnet mask um our Gateway on our DNS server um so again the the Gateway is in effect our router's address and the DNS server is where our PI Pico is going to ask for DNS information and that usually defaults back to your gateway and the pipe pickle will simply ask your gateway and where websites exist on the internet so that should hopefully let us make a connection so let's put that on to the Pico and see what happens foreign so I've uploaded that onto my Raspberry Pi Pico as a file named connect.py so to get that to run I just simply need to import my connect code and that should then run and we can see here it's now trying to connect to my Wi-Fi network and then once it makes that connection we can see though it's been successful so my Pi Pico has connected itself to my Wi-Fi network and it's given and the my router has assigned this IP address so that will come in handy later on when we want to talk to my Raspberry Pi Pico we've now got my subnet mask um so in my router it's only this last um block of numbers which changes when we have different prices connecting there and of course that's the address then of my Wi-Fi router itself so we've got a successful connection and my Pi Pico is not able to use this Wi-Fi connection to do well whatever you would want to do with the Wi-Fi connection so let's see if we can access um some web-based service and pull down some information from from that so at the moment our Raspberry Pi Pico isn't aware of what time and date we have so let's give it that information so there's a service here we can use this time API dot IO which offers a free time zone um web service so if we click on the get started here we can see that there are a number of API calls we can make here and basically what's going to happen is that our Raspberry Pi Pico is going to send a request to this website and then this website is going to send back a block of data we think we we can decode and get then gather the information from so these apis then use these things called get calls and this is how devices communicate over the Internet so um it's going to make an HTTP get request to this particular web address so we're going to be calling this time API dot IO API time slash current slash Zone we're probably going to specify some information after that and then this will then come back and respond with us rather than responding back with a web page and it will respond back with some data so let's have a look at this particular request then so we can see here that we've there's a number of things we need to fill in for this so let's let's try this out and I'm based actually then in in the UK so I'm going to use a time zone called Europe London and if we execute that we can see how it works and what information we're going to get back so what this is really telling us then is that to use this um API we need to send a get request so this um this thing called curl is one of the standard pieces of software you can use to to mimic these requests and that that's just the code which is used to set up that curl request but for the of interest to us then is the actual request URL that we need to talk to so this is where we are going to send a get request to this web address and you can see there that this first block there is is that API we call them API endpoints so that it gives us access into a particular place within the code in this website we then use this thing called a query string so at the end of your web address or your url you sometimes and you'll see this as you look browse to the internet you'll find a little question mark and then after that there are a series of variable definitions so here we're saying that's our URL endpoint we then want to send some data in our query string so we're sending across a variable called time zone and we're setting that equal to this Europe London value so when we send that get request across the this website is going to respond back with some information and this is what it's going to send back to us and this is in one of the standard formats for data transfer over the over the web and this is known as Json so it's JavaScript notation or JavaScript object notation and it's a very cross system friendly way and also for for human friendly we can actually see exactly what the data being sent back is so it's going to send us back an object which is denoted by these two braces and inside that object is going to be a number of parameters which define various pieces of data and you can see very much here so we're saying it's year the month day and and so on and we will be able to then take hold of this information back on our Pico decode it and pull out the information we want so let's go and have a look at how we actually do that then on the pi Pico so I have a new file called get time here um all of the code I'm using in this tutorial I'll be putting all of that into a GitHub repository which I'll put online for you all to download and that will be the links for that there will be in the description down below or if you go off to the project page for this tutorial and you'll find all the information there as well but it's basically the same code that we had in the previous connection um file all I've done here is we're using an extra Library which is this um the requests library or the micro python version of it and that then will provide us with a mechanism to generate these HTTP requests so you can see all the rest of this is all the same as it was before but down the bottom here we then have a forever Loop which is simply going to make these requests so you can see here we're using this you requests Library it has a get request method and for that we just simply need to then Supply it with the URL for our API call so you can see here this is exactly what we had on that um on the website's test system so we have our endpoint query string and then we're supplying it with the time zone that we want now I just put a little bit of code around this so we're in this line and this line here we're just timing how long this get request takes just so we can have a view on on the performance of the pi Pico over the internet the requests.get method then is going to come back to us with the response from the web server now that response has got a range of data embedded into it um but we then are able to use a few of the inbuilt methods in the response object to get that data out so there is a Json decode method which we can use there and that will generate a a dictionary in effect and we can then pull out the time value from that returned object and that's what we're going to print out so we should be able to see the time or the date time value that comes back from this web service and then we're going to do a sleep for five seconds then just repeat that so every five seconds we should be querying that API and getting back a date time string value so let's have a look at that actually in action so again for this we just simply need to import our file and that should kick off that piece of code so getting a connection once we've got our connection then it should start sending those get requests and start bringing back the time so you can see here that we actually have the response back from that web API but the important thing here is that if we look at this it's taking us around about two and a half seconds to actually complete that get request so from when the request is generated to when we actually get a response back and are able to print that out so um that is something that we do have to consider um if I if I just jump us back into the code for a second so again you can see that that is then just doing every five seconds it's going off and we can see here that we are getting our our latest time stamp coming back but let's just jump back into the code and see what the significance of this value here is for us so back at our code we can see here that the actual Communications with the web API take place at this line of code so we we Mark the start time and then we print out the end time in that third line but there are code and effect is stuck on this particular line for that two and a half seconds and nothing else is happening on the pi Pico um and that is something you do need to consider when you're using this um web system that um the the basic software and the basic interface and controls for accessing the web interface um use what's known as blocking code so certain methods that you're calling um take over the pie pickle and block any of your other code from running until they have finished now there are a number of ways around this um which I'll actually be covering that in a second follow-up to shortlist one so this tutorial I'm really going to concentrate on the basics of getting everything up and running and as you're seeing here if if um waiting for that response isn't such an issue for us as it is here um then then everything is fine but if you are thinking that perhaps you have this request in the middle of some sort of control Loop um where you you are controlling some sort of motor or or whatever then obviously um having a two and a half second Gap in your processing could make a it will things very very difficult for you so do be aware of of how this works and its limitations um but but for now then um in simple terms of getting hold of some data from a web API you can now see the basic process so again we have this get request we can send that off and let's get requests then is is a proper HTTP get request so you can access um in this case here we are accessing an API which we know is going to send back some Json data you could use this to actually request web pages and then script those web pages for certain data within them and and so on but basically this is going to send you back whatever this particular URL whatever data that sends back whether it's as I say Json data or just some html text that will come back as a as a string value in effect there are other methods built into the response you get back which will allow you to deal with that and again do have a look at the other documentation to get more details on that but the thing here a lot of the time you'll be using these get requests then to access apis and mostly they will come back either as Json some of them will send just back some plain text answers but a lot of them will come back as Json structured data and this is how you then can decode that and pull particular data values out of that return turn data so just to recap then we so far got our Raspberry Pi Pico connected up to our home Wi-Fi connection and that allows us to send HTTP requests out to other servers or API systems to gather information but we now want to look at how we can use a device to connect to our Raspberry Pi and then communicate directly with the code running in our project so if we consider any device that we connect to our router and whether that's over Wi-Fi or over ethernet our router builds up this internal Network where every device is allocated an IP address and that is its specific address within our internal Network and we can actually use these IP addresses then to start communicating between devices so just as we saw in the previous example where we sent an HTTP get request out to some external server we can actually use a device such as our mobile phone to send a get request to our Raspberry Pi Pico at its internal Network IP address now once we send that get request our PI Pico all we have to do is to make it able to collect that request decode it handle it and then send back some information if required so in effect what we're actually doing is turning our Raspberry Pi pickle into a web server so let's have a look at how we code that so we're going to work towards building up a little project where we can use a web page interface to allow us to use a mobile phone or something to control an LED on our PI Pico but first of all we need to be able to receive these get requests coming from different devices on our home network so we're going to modify the code that we already have developed we need one more Library one more package which is the socket library and we'll see where that comes in in just a second so I've added that import at the top of our code here again I'll I will be using this um pin definition to control the pins a bit later on but let's work just first on the side of the sockets so we go through our normal idea where we are connecting up to our um router but the first thing we need to do here now is we need to open up a socket and what a socket basically does it allows us to open up a doorway in on on onto the network for our PI Pico and then connect that doorway to some sort of data stream and that's what's happening in this block of code here so um there are a couple of ways of setting up our socket um you'll see in quite a lot of examples it uses this notation here which is slightly more complex but it is a slightly more powerful way of setting up a socket but for now we'll just do it the simple way where we're going to say I'm going to create this socket address by giving it a an IP address and again we're Gathering that IP address up here from our Wi-Fi configurations remember whenever we do our Wi-Fi we can call this function ifconfig which sends us back four numbers the the first of those numbers then is the actual IP address that's been allocated To Us by our router so we're saying here I want my socket address to be at my IP address and I'm going to open up Port 80. now Port 80 is the default HTTP address so whenever you contact a a website using HTTP not not https that's works on a different port but this TTP requests come across on Port 80. so in fact doorway number 80 we're going to open that up and we're then going to connect that to our socket so this is where we are now creating a socket and we are then abinding this socket to this address so that at that point there we now have a socket which is connected to Port 80 on our Pico's Wi-Fi interface and we're then going to start listening to what's coming in on that Port now I know the number inside this listen method call tells us how many um requests we can we can queue so in effect we're going to have um the request we're dealing with and one queuing in behind that and that's before if everything else comes in if we start hitting with lots more requests and the Raspberry Pi pic I was just basically going to say that it's not able to deal with things and and you'll you'll get one an error message coming up on your web browser so this point then we can see here that we're just going to get it to print out telling us um that it is now listening to this particular IP address so um this bit here is starting to set up the control of our LED but for now we're not going to just work with that we're just going to try and make sure that we can actually get some request data coming through from our mobile phone so we're going to do a forever Loop here and we're basically going to say that we want to set up a client and capture the address of the clients this is going to be our mobile phone which is connected up to our same Wi-Fi connection or or our same home network connection so we're saying we're going to say we want our sockets remember s is our socket that we bound to this port 80 we want that to accept a connection and that connection of course then will come from our client so our our so our code is going to sit here waiting for somebody to connect to us what we're then going to do is we're going to from our client we're going to receive some data and I'm telling it here to receive up to 1K of data coming in so our client will will be sending in a get request we will receive a block of data from there and then we're just simply going to print it out just so we can see exactly what has been sent across our Wi-Fi connection we're then going to send a message back to the client so we're going to send something back and just tell it that we have received the message and we're then going to close our connection um just to clean things up um if you don't close it it will be collected in the garbage collection as as the Raspberry Pi Picos micro python system runs through but it's just a good idea just to close it now just to release those resources so on to our Raspberry Pi and I'm going to import my simple LED code run that so again it's connecting up to my Wi-Fi and it's now turned on our Wi-Fi and we can see it's now listening on that Port 80 for some sort of connection so let's grab hold of this IP address and let's come across open up a browser window and I'm just simply going to browse to that IP address and see what happens so there we go we've now on our browser then you can see that message received has come back but if we look at what's actually happened on the pie pickle we can see that we've got this whole block of of data coming up and what's really happened here is we actually have um two get requests so we can see here um the the string that is printed out the b here means that it that it is a byte string so each of these characters is actually being represented by a single byte so it is still a string but it's just this this special bait version of a string and we'll deal with that a little bit later on but you can see here we have one get request sitting there and a second get request sitting there now this lets us see the structure of a get request and this is what we're going to have to deal with to be able to decode what's happening inside our our code so this first well let's just take the second get request here so we can see here that we have a get request so we've got our get method there so this is this is called a method or or the HTTP verb um that is Then followed by the um resource that we are trying to access okay or or the actual URL within our website and then we have our protocol that's being used the slash r slash n is an effect a new line character so this piece of code here is actually the first line of our HTTP request and as I said it's saying I I want to get hold of the fave icon.ico resource and I'm using HTTP version 1.1 protocol you can see that that the same is happening up on this first request here where we are saying it's a get request we are accessing slash which is the um the basic root of our website so we haven't specified a resource that will return back the default page and we're using HTTP 1.1 so that's the actual request that we sent from our browser so um the reason I talk to this one here first is you can actually see what resource it was accessing but this means that we were accessing just the default page and that was when we used up here and we just used that default IP address and that will access the default web page um when we access a website our browser will always check to see if there's this favicon.ico and that's the little picture you get up here whenever you access a website so because we haven't sent one back it just does this default web address thing here but an effect then this block here this is the get request that we now have to start working with and decoding and you can see there's lots of information embed within that but for the moment we're just going to concentrate on this very first line and that will let us do quite a bit of decoding and control within our project where if we can access different URLs within our web pages then we can use those different URLs to perform different actions in our code so let's look at how we can actually decipher that then and start allowing us to control an LED just using a web page interface so we first need to decide what URLs we're going to expose to the user so we already know that um to call up the initial web page we're simply going to you send a get request to the IP address and that's going to then trigger this forward slash default page request and for that we're then just got to send back the actual HTML that builds up the web interface itself we'll then want to have two further URLs one of those is going to allow us to turn the LED on and one of this is going to allow us to turn the LED off so basically we'll create these URLs um so um our IP address forward slash LED on and forward slash LED off so if we want to turn the LED on we're simply going to have to get our browser on on our mobile phone or whatever else we're browsing with to send a get request to that URL thank you so let's start by having a look at the HTML code that we're going to need for our web page and remember this is what the user is going to see when they log on to our Raspberry Pi and this will have the controls then to control that led um I'm not going to go deep into how HTML pages are built up but just enough to just cover what we need um so some of this code is sort of boilerplate that you need to have so you need to have your doctype declaration we then have our HTML tags which hold all of the HTML and we then have a header section inside which we can put in a title and that is the piece of text that comes up in in your browser tab at the top of the top of your window we then come into the actual body of the page itself so this is what you're actually going to see in the in the browser window and what we're going to do is we're going to put a paragraph in so HTML has built up of these tags so start tag and an end tag which has that little slash there so it's a paragraph of text which just sells says to click the buttons we're then going to create an actual button itself so this um tag here so input type equals button creates an actual press clickable button we're then going to say what's going to happen when we click that button and this is a little JavaScript piece of code here so when we click the button we're going to in effect send the browser to a new URL and this is where we're using these two URLs so these endpoints in our are our API system in effect so our Raspberry Pi Pico will be able to respond to the slash LED on and the slash LED off get requests and that's what we'll actually control then the LED turning on and off so we're going to have to need to work out High when we get these requests we can decode that URL work out which one this person has been requesting and then take the appropriate action we're then going to put a new line in there just a bit of a space just to space out the um buttons and so on and then we're going to have this piece of text here and what I'm going to do here is our pie picker once it receives an on or an off request it's then going to notify back so it's going to send this web page back to the browser and it's going to replace this little token so I I use this for a little token so asterisk asterisks then a token name asterisk asterisks and we're going to use the python string manipulation the string replace function it will look for this token and then we're going to insert either in there and on or and off depending on whether we've just turned the LED on or off and that will then appear of course in the user's browser and give a bit of feedback so they'll effectively open up their web browser they will see two buttons they can click the buttons turn the LED on or off and the system will just report back what the current state is so we next need to have a look at how we can actually decode which of these URLs the person has requested so before we look at decoding those get requests I'll just first take you through how I prefer to put my HTML code into my applications so I've created an actual HTML file on this side here which has got the code that we've just been looking at the reason I do it as a file is that I'm by making an actual HTML file I can actually view this in my browser and check what it looks like before I start embedding it into my micropython application to to actually use that then uh if we come across into this this is the actual code which runs on the pi Pico itself over on this side and you can see that we are pulling that information in as a file here so I'm opening up a file handle connected to that HTML file and then reading in the contents of that file into a variable called HTML and then closing my file at that point there just to release any resources so at this point my variable HTML contains that HTML file I can then take my token that I said there at this point here I'm working out whether my LED is on or off and I'm tracking that using this led State variable so whenever we turn the LED on we're going to set that to True when we turn the LED off we're going to set it to false and you can see here all we're doing is working out whether we need to say off or on and then just simply replacing that Tok on token with my LED State and then we send that back to the browser I'm using the client connection and then close all that down so at this point we will come in we're going to be deciphering here whether we want to turn the LED on or off or just display the page we then generate our HTML send it back to the client and then the client can then see that together with buttons and and so on so let's go off now and see how we're actually going to decode that URL coming in so back to our PI Pico micropython code then and we'll see how we decode this so we've already seen that we are receiving the actual get request information um in this variable raw request and we're doing that then because we're receiving the bytes coming in now as I've really seen that is coming in as a byte string which is still a string but it does behave slightly differently in certain circumstances so we first need to translate that into a normal string and we're doing that using this decode method here so we're taking our byte array and we're decoding it the byte array because it's a an HTTP HTTP request it's usually going to be in utf-8 format so we're just specifying that there and at this point we should now have our raw request variable as a normal string and we'll print that either so the next time we run this bit of code we should see it now behaving as a normal string so once we've got that we can start to break it apart and pull out the bits that we need so if we split our string on the spaces so we'll actually split it up into words two words that we actually need then or the very first word uh so the very first word of the first line as we've seen that always specifies our HTTP method and for us that will normally be get the second word will actually be the request URL which is the one that we're using to specify whether we're going to be turning an LED on or off so if we pull those two parts out we can then use a bit of an if statement then to test that so I I'm using this find method to look for that string inside our URL again sometimes um for whatever reason there may be bits coming in after that so they might if um there happens to be any query details attached at the end of that with the question mark Or maybe a bookmark with a hash mark or something like that so there are certain circumstances where we will get more than the bit of string that we want so if we do an equals test on that we might find although somebody has requested the LED on URL um it doesn't match so I'm just saying on this line is if it does contain this piece of text then we'll just assume that that is the LED on URL being requested and we will do the appropriate action here by setting the LED State and turning it on and similarly then for the LED off URL if um it is neither of these URLs then we're just simply going to let the system just pass through by doing nothing to the LEDs and it will just then of course send out the HTML as it is um we could do a lot more testing in here um to specifically test for unknown URLs and so on but um for the moment we're just going to just assume that if it isn't on or off then we'll just assume we just send back the page and we've already seen then that the rest of that code just simply sets up the HTML puts in the correct values and sends that back to the client so let's have a look and see if that actually works so let's import our code again and see if that all works so we've got our connection we've got our browser up and running here let me just refresh that just to make sure we've got it all set up so there we have our um you can now see that our get request is being decoded as a proper string so we're getting all the new lines coming in here and you can see each of those individual parts of our of our get request so let's try um sending it some of the buttons out so if I press the LED on button you can see there we have our get request to the LED on and it should come on here and if we have a look actually on our circuit board you'll see that our LED is on and it's a little red LED um that circuit board that I've got there at the moment um I've been working on the next tutorial after this one where we're going to be looking at a full web API um running on the Raspberry Pi Pico rather than this um this is a fairly basic system we have at the moment using these URLs um the next lesson will go much deeper into providing a proper um web-based API for your project um so let's just concentrate on that on that red LED so that turns it on if I hit the next button then of course we can see all of our requests coming up here again our LED off request there and the LED has obviously gone off as well so there we have now um being able to control our PI Pico project by putting up a web interface allowing various control buttons and so on and feedback coming back from our PI Pico and we now are able to control our projects and talk to them over our Wi-Fi connection so the last thing I want to cover in this tutorial then is what if we don't actually have a Wi-Fi network that we can connect into um well at the very start of this we did talk about when we set up our our wireless connection that when we turn the interface on we have a choice of two modes in which we can set it up so we've been using the station mode where the Pico accesses an existing Wi-Fi network and that lets us then of course use that Wi-Fi network to get out onto the internet but we can get the Pi paper to generate its own um its own Wi-Fi network and this is the access point interface um so let's go through and see how that comes on so I've just really modified that led code and we're just doing it now through its own access point interface so for the access point of course we're not going to be connecting up to an external Wi-Fi network so we can now set up our own SSID and password and that's what we're setting up here so so it's going to broadcast itself as the pi Pico access point and the network password is just going to be password to get onto this so when we specify our our wireless network you can see here that we're using this AP underscore if um constant and that will tell the interface that it wants to generate its own Wi-Fi network so we then are configuring that Network and we're setting up the SSID and the password and then we're turning on the um Wi-Fi network itself so at that point the pi Pico will generate its own Wi-Fi network and then start um listening out for clients that are trying to connect to that so in this point this little bit here so once we've turned it on we simply have to wait for all of that to become active and you can see here we're just waiting for the active flag to become true and if it's not true we're just going to say waiting and we'll just do a little sleep while we sit in that Loop waiting for it all to come active so in this particular um bit of code of course as soon as we could become active we are getting it to tell us how it set itself up and what IP address is working on um again um this IP address will be fairly constant um each time that you enable this interface so once you find out what it is um that that really is it will really be set at that each time you turn it on no now after that we now have our our Wi-Fi network setup our PI Pico is attached to that and all the rest of the code is exactly the same so you can see here we are simply picking out the IP address of the pi Pico we're then binding a socket to that and exactly the same way as before on Port 80 and then everything else is exactly as it was before so let's power that up and see if we can get that to work with my mobile phone so if we now start up the simple LED access point code we set boots up and it pretty much instantly turns on the Wi-Fi it doesn't have to bother connecting to anything so it set itself up um as our PI Pico AP access point um it's given itself a web address or an IP address there of 192.16841 and that's what it will be listening to on Port 80. um so um I've now got my phone um screen setting here which you can see and I don't know if you notice but it just popped up there once once we set that code running our PI Pico access point as an available um Wi-Fi connection so if I connect into that and then we have ourselves connected again it's saying there's no internet and of course that's because our PI Pico Knight has no internet connection we're running just a local network and again my phone's complaining about that um but no we want to stick with our PI Pico app but let's go back to our our web browser you can see that there are certain um my phone itself is is trying to work out if it can access the internet through this which is what these little get requests are are happening here but that that web address then if I if I use that in here so if I and try and type that in um so one nine two dot one six eight dot uh 4.1 so that one there so if I click onto that and let me just um adjust my screen so I can we can actually see that um on on there okay so again we've got our our web request coming through so you can see here that we've got our get request coming through it's looking for the default page and of course we've just served it back then the actual web page itself um our our LED is off so if I click on here again it's working in exactly the same way as it did um in our in our last system and again each time of course it goes back and refreshes back to a smaller screen but you can just basically the two buttons on there and I can now of course control my LED from these two buttons but this now gives us a totally stand-alone Wi-Fi connection we're not reliant on any existing wi-fi system being in the area so we because we could if we wanted to if this was out in the middle of nowhere we could have our pipe equal generating its own Wi-Fi network we could walk up with our handset log on to our system and then start controlling our project over a Wi-Fi link 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 um I look forward to seeing you again sometime very soon so bye for now for more games programming 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: 25,917
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, WiFi, micropython
Id: eym8NpHr9Xw
Channel Id: undefined
Length: 51min 12sec (3072 seconds)
Published: Sat Jan 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.