Discovering ESP-NOW in MicroPython with Asyncio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to donkey Tech in the last video we talked about how to get started with ESP now in micro python in this video we will talk about the topic of using asynchronous IO or async IO in our micro Python program running ESP now to make our iot application more responsive so this is my current setup where I have two esp32 with multiple components connected to it this is my center and this is my receiver the sender is my remote control and it controls the component on my receiver using ESP now I can turn on or turn off the LED from here or adjust the brightness of my LED through pwm I can also do it simultaneously where I can adjust the brightness of the LED and turn on and turn off the LED using ES speed now I can even enable or disable the whole system by clicking this button so if I click this button and then click this button right now then it does not turn on or turn off this led anymore or it does not change the LED pwm settings of this led so right now if I enable it again then I can again turn on or turn off the system or adjust the brightness or change everything at the same time the control message is sent through ESP now and it uses async IO a synchronous programming to make it responsive this video is sponsored by PCB way PCB way is a manufacturer specializing in PCB prototyping low volume production and P simply assembly service all under one [Music] okay so what is async IO and how will it help us in our micro Python programming I think IO is a python library that will allow us to write asynchronous non-blocking code to help you understand what asynchronous programming is then let us compare it to synchronous programming using an analogy let's say we are a chef in a restaurant an order came in for three different dishes so let us see how will this be processed using synchronous and asynchronous programming in asynchronous programming the chef would cook each dish one by one the amount of time spent is equal to the sum of each dish preparation so if one dish equals 15 minutes then the total would be 45 minutes in an asynchronous way the chef would first start with the first dish while waiting for that dish to boil he can start working on the second dish and then while waiting for the second dish also boil we can start working on the third dish and then at the same time you can switch back and forth between the three dish the time spent would definitely be less than doing the dishes one by one this is a synchronous programming in action now let us understand everything using Code I think IO has three important Concepts that you need to understand first is the event Loop this is the async io overall engine the python async IO event Loop is typically single threaded this means by default it runs all asynchronous tasks and core routines within that single thread think of this as the chef in our analogy about our restaurant who manages the cooking of our dishes it is his job to switch working from one dish to the other and then we have core routines core routines are special functions with the async and the await construct Coral things can be pause and resume allowing the other tasks to run in between this is important because our event Loop runs in single thread which means that each color thinks that he that runs should be possible or resumable so that we can switch between the different core routines pass pass our units of work in async IO and they are created or they can be created from core routines you create tasks so that you can schedule them to run concurrently in the event Loop to help you visualize micro python async IO please look at the diagram below the tasks are the core things that our event Loop will execute and since each task can be resumed and pause then our event Loop can schedule what task to run at a time you will see in the code section later how we could take advantage of this in our ESP now micro python application to know more about python async IO then you can try to follow these links now let us get ready on how to use async IO in our micro python ESP Now application by showing you how the code works so before we continue I just wanted to show you the code that I have regarding the differences between using synchronous and asynchronous IO in our micro python application if you remember correctly in the diagram earlier we have the chip that is cooking so I have here two example files python files they are actually found in my GitHub repository so if you wanted to take a look then it is in the description of this video so as you can see this is the cook synchronous food so in this case we're going to use synchronous programming in cooking so I have here a function called cook order so in the cook order there is the it will cook the name of the dish and then the duration so for example the duration is how many seconds will it take for the code to be cooked so I have here this function book order and then I have the function here called serve food and I'm just going to add some timing mechanism so we start with the timer and then I have here the three orders that we have which is the which is the pasta the pizza and the stick and the time it takes is for example the pasta will take two seconds the pizza will take four and the steak will take three so if we're going to cook it in a synchronous manner then we will start with the pasta then the pizza and the steak so in this case here as you can see there is a order of the We're looping this lease and then we're going to call the function to order and then let's see what will happen so I'm going going to run it so I'm connecting connected to my esp32 right now and then let's try to I'm just going to make this a little bit bigger and then let's try and as you can see it now starts cooking in pasta next it goes into the pizza and then it waits until everything is cooked so as you can see in a synchronous manner uh everything should be done uh before you can continue with the next so for example the pasta should be cooked before you could cook with the pizza so as you can see the elapsed time is almost 9 seconds and that's because if you try to add this value here so 2 4 and 9 that will be equivalent to nine seconds so this is how the synchronous is being done so let's try to see what will happen if we run this in an asynchronous way so this is my code here so as I have mentioned we call this thing as protein so in accordion there is the a sink and then there are weight here the function of the async and the away is that so that the event can pause or resume this particular function since the event Loop is single credit so the task that it is need it needs to execute should be possible or assumable so that's why we have this await option here so it's similar the only difference is that there is an acid here similar also I have here the a sync for the surfboard it's almost the same the only difference that you can see in here is that we have here the order pass so this order pass what we're doing is uh we're going to put it in a list and then once the function or the coral things are in the list then we need to call the event Loop so what I mean is we're going to play it uh the pass here from this list by using this async io.gather function so anything here in the task they will put they will be placed in the event Loop power program so right now if we call awake async io.gather then let's see what will happen in our program so I'm just going to clear first this one and then let's try to run and as you can see the cooking starts for the pasta the pizza and the steak so this is what I mean by the using of the async io so our program or our code just needs four seconds which is almost equivalent to the highest uh food that we have which is the pizza so this is the example of how powerful async IO is in our micro python application so that's basically how the uh the difference between the synchronous and asynchronous 4D writing and schematic just follow the wiring here so this is the schematic for the sender so there's just three components which is the LED control bottom the potentiometer and the enable and disabled pin so just follow along with the table here for the potentiometer just connect the one side to the 3.3 volts and the other to the ground so for the receiver just follow the same principle that we had in the earlier video so there's dpwm LED connected to the gpio 23 and just always connect a current limiting resistor which is the 221 so in order for us to use async IO in our program then we need to install the async io version of the ESP now fortunately there is a module called AIO ESP now that we can install to our esp32 file system so the way that you can install this aiosp now is to you we can use empty remote or MIP or me so right now as you can see I'm connected to form 12 and there is no light folder currently so I'm just going to disconnect right now then we can install the MP remote by using this command clip install MP remote [Music] and then once the MP remote is connected then we can execute the command MP remote and then MIP install and then the name of the module is a i o ESP now and then click enter now it will download the library from the micro python live folder and it says that it is done installing the AIO espnow.mpy if we now try to connect to our esp32 so here let's just connect again to our esp32 and as you can see right now I have the AIO ESP now so if I click import t i o ESP now then there should not be any issue and as you can see there's no problem which means that we are ready to use the uh async i o version of ESP now I would like to mention that the in the code that we have right now uh I just used the usual handling of the button clicks but there is a library written by Peter hinge and it's called the micro Python async and in this Library you would find several classes that you can use in your micro Python Programming where it is already very useful when you're using the uh asynchronous programming like this push button which you can use when you want to handle the push button switches and it already has some API like the Press function the release function that you can use uh whenever you wanted to have an event handler for your button switches but for Simplicity sake I did not add this as the document would become a little bit bigger and you for beginners you will have to take a good breathing here because the guide by Peter hinge contains an excellent resources for beginners to learn also so if you're going to be to use this in serious micro Python Programming then I highly suggest that you take a look at this Library by Peter Finch so the next thing that we need to discuss is how the code works so let's start with the sender so I'm going to complete this one as I've mentioned this code is available in my GitHub repository so you can check this out at the description of this video so let's open and then let I'm going to scan how this program works so the very first thing is to import the necessary modules especially the async i o so I have imported it here then we need to enable the wireless LAN and then I can add the peer that I wanted to add in this line of code as you can see the ESP now is using the aiosp now class which is the ESP now with async asynchronous IO support next we have this button pin the button pin that we need to do to press and to enable or disable the LED and I have here the potentiometer pin also and the enable and disable button now I have created here an event so this event will be used for signaling so for example I can enable or disable this event whenever the event is set then it means that the the system will work and if I clear it one then I would disable the system so even if I click the buttons or the potentiometer then nothing happens and as you can see this is one of my four routines so the send button state is just pinging everything the one thing that you need to check is this one because I am checking the event if it is executed or not sorry if it's set or not if it's set then I can read the value of the button so I have just some debouncing mechanism in here and after adding the button the bouncing here I will just send the message if it's LED on or LED up so that's how you can send command to your receiver esp32 through ESP now for the potentiometer value it's almost the same the same qualities so in the whole routine section I just check also if the event is set and then if everything is okay I just read the value of the potentiometer so this is the value of the potentiometer with reading and the last uh core routine is the enabling and the disabling so in the enabling and the disabling I just exit the event can be disabled or enabled so this is how whenever something is set can be set or not so this is the color thing that can respond to you to that bottom click so as you you can see that these three quarter things they are the one that is running side by side using the event i o do and I have the main class here so in the main class I just gather everything all of the core things that you're seeing here I just added it here in the main and I run everything in the loop using the async IO Data so that's how you can control your different components using async IO next let's try to discuss the receiver for the receiver oops yes so this is my receiver if the code is async control receiver so in the receiver section it's much simpler so we follow the same protocol we import and then enable the wireless LAN and then at the same time we use the AIO ESP now which is the ESP now with asynchronous support and I have here the pins the LED pin and the pwm pin so there is a function here for the map value this is used for the potential maker because the value coming from the potentiometer is 0 to 4095 I think and then we map it into the duty cycle of this pwm pin so as you can see in the pwm PIN we just continually wait for message if an ESP Now message is received then it clicks LED on then we turn on the LED it beats off then we turn off the LED if it's a potentiometer message with a value then we just extract the potentiometer value and using the duty cycle and set the duty cycle here so basically that's all how the code works so whenever everything is uh running or when you run these two projects side by side then you will be able to see the output so I will be showing you how to run the project so right now you only need uh two Tony instances to run and the first thing that we need to do is we connect to our Tony is to our esp32 so I'm going to connect the receiver here and sender here also and then at the right hand side I will open the async control receiver that py so this is the receiver so it maps to this one to my other esp32 and the center is the one in here so this is the center so I have a cancel button here or an able and a disable button so to run the project we first run the receiver so I'm going to run so right now it's now running so the next thing is we need to run the sender so I'm going to click run here and as you can see everything is running now so let's see if it [Music] so okay so I have been able the button and then after enabling the button uh let's try this one and as you can see in the output I was able to change the LED to turn on and turn off and let's try the potentiometer and let's see what will happen here and as you can see I can manage to change the brightness of the LED so right now you would notice that I can change it according to this potential motor value and I can also change everything at the same time so I can click this on and then at the same time my program is still very responsive so right now I can read two sensors in my circuit and as you can see I can turn on and change the pwm at the same time so after which then we can have this button which is the enabled and let's try and disabling and as you can see there's a message that is it is clicked and even if I uncheck it click this one and change the nothing is happening anymore so I'm going to enable re-enable Again by clicking this button and then after enabling I am able to change everything or adjust the video pwm settings so that's it and that's it so in this uh video uh I am showing you how you can run your ESP now uh micro python project with the async io support the right app for this project is available at the description of this this video and also the code available in my GitHub repository so you can learn and check it out as usual if you have any comment or suggestion then please write it here and I hope you could subscribe to my channel so that you could support me uh in doing this project that's all I hope you learned something up exploring
Info
Channel: DonskyTech
Views: 1,145
Rating: undefined out of 5
Keywords: espnow, micropython, esp32, esp8266, thonny, aioespnow
Id: mJlBJ1h9A0U
Channel Id: undefined
Length: 23min 9sec (1389 seconds)
Published: Wed Sep 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.