API request in Micropython | WiFi Station & Access Point Mode | urequests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome everyone to the fourth episode of micropython series where i'll make sure that you all get comfortable on using micropython on esp32 mode for any of your project this episode is all about wi-fi starting with the very basic wi-fi scan example code in micropython after that we'll be learning about how to use this wi-fi in access point mode however you'll be learning how to use it in the station mode to get connected to the nearby wi-fi router to get the internet access after getting the internet access will be requesting the websites and apis using the get request method and in the end we'll be making a very interesting project of controlling home appliances connected to the blink id platform by requesting the apis from our esp32 board running python now this is one of those episodes which you all were being waiting for because this is kind of a step ahead in iot using micro python so without wasting time let's get started with this video this video is sponsored by ltm which is a pcb designer based software company now if i tell you one very interesting feature of this software then here in ltm designer you can design rigid flex pcb now what is that so till now you must have designed the pcb which has like rigid like solid pcbs that we are not able to bend but here in ltm you can design a pcb in which some of the parts are rigid solid and some of the parts are flexible which can bend and the pcb can you know we can band it in a two-fold manner just like the modern day smartphone right so this is a really very trusting useful and unique feature of the software well you can also try out this and many other unique and interesting features of this designer software by just clicking on the link mentioned in the description yes by clicking on the link you'll get access to the free trial version of this software so go ahead try the free trial version of ltm designer software now before starting this episode i like to tell you that if you're watching this video for the very first time i'll suggest you to watch out the previous episode of the series because every episode is a continuation of its previous episode so do watch out the other episode of the series the link is in the description okay so let's start with this episode so i'll open the tony on my system and here let's start with the first example code which is to scan nearby wi-fi networks and for that we'll be importing one module called as network now this is uh again the model which comes along with the micro python now there is one question like how to check if the uh if the module of the particular model is available inside your micropython version or not for that you just need to type here in the replica as health into the circular process modules close the inverted comma close the circular braces and press enter and here are all the modules that comes along with your micropython version here as you can see the network module is available so we can easily use it okay so after i'm putting the network module you just need to type these lines of code let me explain you one by one so here first of all we have declared the wi-fi object which is responsible for all the wi-fi related activities and here we have configured our esp board first to go inside the station mode now why station mode it's just because to scan the nearby wi-fi networks we need to have the board to go inside the station mode okay so here sta stands for station okay after that we have to type this wi-fi dot active into the bracket to turn on the wireless what you can say driver inside the esp32 board after doing that we can do all the wi-fi related stuff so first of all we are just scanning the wi-fi network so far that the command is wi-fi dot scan and we are saving all the scanned data inside the variable called as networks and in the end we are just printing the network so by uploading this code we should get the nearby wi-fi networks printed here inside the terminal okay let's try and test this code so i'll click on the run button i save this code inside the micropython device with the name main dot pi okay i'll click on the ok button and yeah as you can see here we are getting the wi-fi network like the nearby wi-fi networks scanned by our esp32 board and it also contains the sms gu5 which is the router inside our tech case of studio okay so this is a simple code to scan the nearby wi-fi networks now let us move ahead and let's try to configure it in the access point mode and for that you just need to change here from station to ap that stands for access point okay so i'll remove all this thing because we no longer need this wi-fi scan as of now because we won't be able to scan wi-fi by using the access point mode okay so now in the access point mode we need to provide the sid name and password to our esp board that will be displayed in all other wi-fi enabled devices so for that you do type one command as wi-fi dot config into the bracket you need to type essid and here you need to provide the siri name to of your esp32 board so i'll name i'll give the name of this board as techie sms okay that will be great and after that here we need to provide the password okay so password will be uh let's just keep it very simple one two three four five six seven eight okay and here you type one more command which is this so this will enable the what you can say or rather this will tell the uh system that we are using it in a secured manner okay so you need to provide the authentication mode as well okay so with this we are we have successfully configured our esp32 board to access an access point and we have created the second name and password okay now to uh see the ip address we just need to type the command as print and into the bracket wi-fi dot if config okay so this will print the uh local ip address of this particular board okay so that's pretty much it about this access point mode let's just click on the stop button first and now let's click on the play button on the run button okay as you can see it already printed the uh what you can say ip address of our esp32 board and if i go inside my laptop and can see the wi-fi network as you can see the techie sms appeared here with this lock symbol which reveals that this is a secured network so if i click on this it should ask me for the password let's click on the join button and here i need to enter the password so i'll type it as one two three four five six seven eight and if i click on the join button uh my laptop should get connected to this particular board okay as you can see it got connected to the techie sms wi-fi or the access point okay so this is how you can use the esp board and do the access point mode and provide the i know wi-fi setting even password now if you don't want to provide the password and keep it open you just need to type this much line of code and it will be your open network okay so uh that was it about the access point more now let's just move on to the next step and let's try to connect that esp32 board to the wi-fi router and let us get the internet access from that particular router okay so for that you first need to change this into the station mode once again okay and after that here you need to type one command as wi-fi dot connect okay and inside the bracket you need to type the side name and password of the nearby wi-fi router which you want to get connected with okay i'll type the sid name and password of my wi-fi router in the studio great okay so with this command it should get connected with my uh what you can say wi-fi router and to check whether it is connected or not made to uh we haven't command here inside this micro python which is wi-fi dot is connected okay so this function will control if our board is connected with the wi-fi so let us give an if condition here so if it get connected let's just print the local ip address assigned to this particular board okay let's try to upload this code and let's see what we're getting in the results i'll click on the run button and as you can see we didn't get anything inside this particular terminal it's just because uh this command was no doubt uh executed but it it it like takes some time to get connected with the router and just after this command the micro by then jump onto this line and like it just saw that it is not at all connected yet and hence if this condition was not satisfied and jump out of this okay so here what we're doing to provide some kind of delay to give this command some time to get connected with the router okay so for that what i'll do is first of all i'll type in condition as while not off wi-fi dot is connected okay and here i'll just write pass okay that will do nothing okay so until unless the wi-fi is not at all connected will be staying inside this while loop only as soon as it get connected uh this condition will not be satisfied it will jump onto this while loop go inside the if loop and if it is connected which it hopefully should it will just print the local ip address let us try to run this code okay now as you can see it got connected and it also already printed the local ip address assigned to this particular board so this code is perfectly working now here is one a mistake still now if i provide some wrong side name and password you can imagine the code will be stuck here for info like this will be an infinite loop and our code won't be able to jump out of this file so we need to provide some kind of timeout period so that if in case the router is not at all available in case you have provided wrong credentials it will jump out of that while loop after particular time interval okay so for that i already written one code and that code is this okay so this is that code let's just try to understand this code line by line so network model is imported time model is imported and a time out variable is declared with the initial value zero okay pretty simple we have configured it in the station mode after that we are first of all you know uh deactivating that wi-fi and after 500 milliseconds we are activating it again just to restart the wi-fi that's a normal thing after that we have written this command to get connected with the nearby wi-fi router and as you can see here i've provided the wrong name okay so my real uh what outer name is sms underscore gu5 but i just remove this underscore to you know see what happens when you provide the wrong credentials okay so here what i did is first of all i provided if condition that if not of wi-fi is connected that means if it is not at all connected so initially this condition will get satisfied it'll just go inside this print inside this if condition and print as connecting inside the terminal after that i have provided while loop with two condition while not of wi-fi is connected and timeout is less than five okay so if both the conditions are true then only it will go inside this while loop okay so initially both the condition will get satisfied okay because it takes some time for wi-fi to get connected okay so uh it will go inside this while loop it will just print five minus timeout that means it will be printing uh in the reverse order like five four three two one okay so we're printing the timeout rate reverse order we're incrementing the timeout after interval of one second okay so what it will do is so now our code will jump out of this while loop if the wi-fi gets connected or timeout is greater than 5 okay so in both that condition that while loop will be you know not satisfied it will jump out of that while loop okay so in case the wi-fi is connected it will jump out and here this condition will be satisfied and it will just print as connected in case it is not at all connected then this condition will become false again the code will come out of this while loop and it will just print as time out okay so right now we have provided the wrong credentials and now let's just try to click on the run button and let's see what happens click on the run run button as you can see it started running it says connecting then five four three two one and timeout okay so as i provided the wrong wifi credentials it didn't get connected it waited for five seconds and it jumped out to the while loop and it says time not okay so we are not at all talking inside any infinite loop and in case if i provide the right wi-fi credential click on the run button as you can see it says connecting five four and connected okay so it took two seconds to get connected with the router okay so yeah that was a pretty simple yet useful code to get our board connected with the nearby wi-fi router without getting stuck inside the infinite while loop okay so yeah uh that was the thing now let's move on to the next step and let's just use some internet connectivity to do some real real life tasks okay let's try to request one website using the esp32 board and for that you need to write these line of code okay let us try to understand this code line by line so first of all network module and time models are pretty simple which are already included and here one new model is included which is you request now to check whether the u request is available or not you just need to type here as help inside the circular braces you just have to type modules and if i press enter as you can see the you request is somewhere here okay your request is already available so i can use it okay so this is kind of a smaller version of the request module available inside the python after that this line of code will just print the nearby wi-fi network which is optional i'll remove it just to make it less confusing okay after that we are connecting to the wi-fi router pretty simple again this while loop and if condition just to make sure it doesn't get stuck inside the infinite while loop so let's get connected will just print connected and here we are using the you request module which is you request dot get now after writing this command we'll be able to request any of the api any of the website and stuff like that okay so i have just requested the example.com let's just try to request this particular example.com from our browser let's see what we are getting okay so i'll request uh here i'll request it and it just print as example domain and this all line of code because this is all we are getting inside the example.com let's try to request it in esp32 and let's see what we are getting so with this you will be able to request it and we'll be storing inside this request variable and we can see its status code and the response in text format by writing these two commands okay so with this we'll be able to see inside the terminal and in case it is not all connected with the wi-fi it will just print as timeout and not connected which is pretty simple which we have seen inside the previous code okay so let's just try to run this code and let's see what we are getting it says connected and okay so we got the text response so first of all it says connected which is printed uh here after it printed 200 which is nothing but the status quo the successful communication status code is 200 okay and after that it just printed the text format of the web page that we have requested okay as you can see it says example domain which is uh written inside the web page here and after that this deck which is this domain is for use illustrator purpose which is written somewhere here okay so we got the response from this website and this is how we can use the get request method to request the actual website or we can also use it to request some apis to do some real life tasks maybe some api to get the current temperature current the weather status current date and time any any api you can use here okay but we'll be using it for some interesting purpose and we are landing on to the last code of this particular video which is to request the blink apis to control the home appliances which is connected to the playing cloud already okay so uh for that the code is this okay so this is the code which will be help us to request the api and to control the appliances let us try to study this code one by one okay so first of all here we have included all these three libraries which is pretty simple after we also include this pin from the machine model it's just because what we are doing is we are using the boot button of the esp32 board and as soon as i press the boot button it will request one api and the particular light will be turned on and off because that's why we have included the machine module as well okay after that some of the variables are declared pretty simple and here we have defined as pin 0 as input because the boot button is connected to the digital pin 0 of the esp32 board okay and now what i did here is i created a connect wi-fi function that is a user defined function which is responsible to make all the wi-fi related connectivity tasks okay that is like taking the credentials and uh connecting to it also waiting for the timeout period in case it is connected with print connected if not just as not connected okay so this is the user defined function and here i have to provide the proper sid name and password this is the wrong side name i'll remove it and write the correct name oh perfect okay so this is one of the user defined function second user second is the button i iq the button interrupt request it's just because i have also provided this button as an interrupt okay so it's a falling interrupt and as soon as this enter bucker it will call the function as button underscore iq now this interrupt is already discussed in one of the previous episodes so you should definitely watch it out if you're not at all understanding this okay so basically as soon as i press the button it will be calling this function and inside this function what we are doing is we are just toggling the status of button status so visit one it will be toggled to zero if it is zero it will toggle to one so that's that's what we are doing here okay so after that inside the while that means inside the infinite loop what we are doing is we are checking if the button status is true we are requesting one api link and in case the button status is fall we are requesting some other api link now what is this api link and how it will work let me tell you so this api link is provided by a blink iot company only i will copy this link and i will paste them here inside the main browser okay let's try to understand this uh api uh so first of all what we have to do is we have to provide the authentication token of our blink project here after that we need to provide on which bill number we want to send the data and here we need to provide which data to be sent so by using this particular api i will be able to send the data one uh okay sorry i'll be able to send the data one on the virtual pin v1 and let me show you this in action as well as you can see here is my blink project on the smartphone so right now as you can see the button one is already on and if i turn it to zero as you can see the button one turned to off okay if i change it to one it turned on okay and if i change the value to v or number to v2 i'll be able to control the second button so if i turn it to zero and press enter as you can see the button to change to zero and if i turn it to one as you can see the button to change to one so using this api we'll be able to send the data directly to the blink cloud and we will also be able to control the actual hardware appliance which is connected to the same blink project so in this particular video i'm using one of my previously made project in which i was able to control the home appliances using the actual blink iot platform okay and now i will try to control that same appliances with the help of this api links okay so here what i did is i just provided the value one whenever the button status is true and i have provided the value zero whenever the button status is false after that what i did is i'm just checking the status code whether it is successful or not so if it is 200 that means it's a successful request i'm just printing as request successful and here it should be light on and here also i check the status if the request successfully will just print as request successful and light off and after doing those requests we need to type this command as req dot close to close that particular what you can say uh request okay so that's that's the compulsory command to enter otherwise you won't be able to request the next api link okay so yeah that was the simple line of code okay i hope you got to you know understood all these things because those are very simple stuffs okay we are just connecting to the wi-fi okay we are just using the button as an interrupt we are just requesting a link based on the button status which is changing with the help of the interrupt okay let's just try to run this script here and it says connecting and it is connected okay so as you can see the smartphone screen is available uh shown in the video and if i press the boot button as you can see it says request successful light off and as you can see the button also turned off and if i press the boot button once again it says request successful light on and the button one turned on okay so it is successfully working here inside the blink iot platform and when i uh turn on the actual hardware project as you can see i'm able to control this tube light by pressing the boot button like when i press it once it turns on and when i press it again it get turns off okay so that's the actual iot project that we successfully made using micro python running on an esp32 mode do click that like button if you are loving this series if you're enjoying this series we're learning something new with every every episode okay so yeah that was the basic wi-fi thing still we are remaining a lot with wi-fi like using web server local web server okay so everything web sockets and stuff like that okay so we'll be learning this kind of thing in upcoming episodes so make sure you subscribe the channel if you don't want to miss out on any of the episode also share this video with your friend who you think should start learning about micropython from very basic to some advanced level and that being said i am just ending this video here and now just wait for my experience to explode and share with me techy sms
Info
Channel: techiesms
Views: 1,828
Rating: undefined out of 5
Keywords: techiesms, electronics, electronics projects
Id: MHJI5KqMcTE
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Sat Dec 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.