003-Python logic to control and read data from Microcontroller for STM32 or Arduino MCU using UART

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey wheels uh in the previous tutorial we have been looking how to read the data from the microcontroller using python and how to have a graphic interface but generally speaking reading the data that way was just with no control on the microcontroller and that sometimes can be a little bit frustrating so this time we're going to use python to have more um systematic or control on the microcontroller so first of all we will have the option appear only when we put the connect and you can see we will be able to toggle the pin whatever the pin but this one is just the one on the on the board and also control when reading or not so here you can see i'm moving the the potentiometer but nothing happened and after we put start reading the microcontroller will start reading and then i can put stop but if you would like to learn how to use a gui and controlling the using python and controlling my controller this is the right tutorial for you guys this is a weird tutorial and at the opposite of the previous videos this tutorial will be focusing on organizing and structuring the communication between the pc and the microcontroller so in the previous time what we have done is just reading the data directly from the microcontroller into the pc without really controlling nothing else so this time we're going to use two examples you control a pin status and also reading a data from the adc and as usual the code is available on github so you just follow the video and you don't need really to type also the language that we are going to use is c and python so we are going to use both even the level is a little bit an intermediate level um i will go through the details and try to explain as much as possible the code so you don't feel really lost within even if you feel a little bit advanced code okay so for the hardware as usual the stm32f1 or the aka bluepill also we are going to use the ftdi to load you out or the um serial communication with the pc and finally a variable resistance or a potentiometer to have the example and see how the variation of the voltage okay so for the tutorial we do have at the beginning a quick review so for people who just watching this video at least to know the background of the code we have created in the previous tutorials and you will understand quickly that will be a quick review then we are going to set up the control to pin control and that will be at the python level or the pc level after that we are going to set up on the other side at the mcu or the microcontroller so we will be using the c code and i hope i will explain this in details then four and five will be how to set up the adc reading from the python site and the mcu side okay so before we jump to the code and explaining it let's have a quick circuit review so first of all we will have the microcontroller here this is the sdm h2 f1 blue pill and it will be connected to potentiometer so we will be playing with the potentiometer and as we play with the potentiometer the microcontroller will be sending the data through a serial or uart ftdi to the pc but just a note for everyone if you do have an arduino or something that the circuit that have ftdi already included you don't need to add this element okay so from the pc perspective where we are going to use the python then we will have threads to control that and in python threads that's a kind of a parallel computing it's not a purely a parallel but it's a kind of a parallel computing because so first we will have our main thread and this is with just managing our gui and we will see it later on you don't worry so this is the normal thread when you run your normal python code and this will control just the bottom the appearance of the gui but after that for reading the data from the uat we are going to use a second thread and why a second thread because if we just skip reading that it will overwhelm the main thread and you will be having a frozen gui and you will not be able to use nothing and most of that plus on top of that a third thread where we are going to update the gui because when we have done in the previous tutorial and you can take a look it's really detailed when we are going to move the potentiometer there will be some animation in the gui and this is the third thread that will be managing this so knowing all of this and understanding that we are going we went through all of that let's go and take a look on the code that we have written before so here side by side both program we have been creating in the previous tutorial so the first one here the in in white is the program that we use to load into microcontroller and written in c so we are going to need only two variables so before that three libraries we have created in the previous tutorial so you can take a look on them i also added a link and also these two variables the first one is a array of char or what we call a string and this is that we are you are going to use to convert to an integer and the second one is a integer value that we will be reading from the adc so first we are going to read the the the adc value and then convert it to it quickly the systick init is the one the um the one that will initiate delay function and this is we need it inside other functions like the uart and the adc so initiate the delay ms function and it will generate small delay and after that we do have the uart in it and this is the uart initiation function so it will initiate you at number two and the baud rate of 115 200 after that we are going to initiate the adc or we are going to use the adc one and the pin the port a and the pin number zero okay so quickly then we are going to check if each time so this is the while loop of our main program and we are going to check if we do have a reading or the reading has been complete so within this program the microcontroller will be reading all the time the data all the time but there is a kind of like in the live or in within the time of the microcontroller itself it takes a lot of time to read the adc so it's some microseconds but for the microcontroller it can be hours like inside it so if you take it in a relative way so it's time to not getting in the loop and keep waiting so just it will be running and each time i do have ready data what i will have to do first of all i will be reading the data this is getting the data or the adc data after that we have to convert this data to a string so what we are going to do is to put this integer value inside the string of the numerical string after that we are going to use the uat using ur2 and send the string empty the string because we need to empty it else it will be keeping piling up on it then sending the end line which is all the new line which is a signal for the python to say okay we have sent the data so you can start your role that's a very simple program so first step initialization second step reading the data when it is ready and send it over here this is our python program and just to make things simpler let's run the code so this is the simple gui when you just run the python and we will try to take a look on it quickly so this is the python and we divided in many functions so the first one the signal handler this one are just you can forget about them this is really to manage the um the um the threading that we do have because when you run threading within python it can generate generate some issues or better to put some signal to manage this and then graphic is a class or an object and that the class will manage this animation here that we will see later on after that the connect menu in it it just this function is just to initialize this menu it's quite simple one where you put the name of each element that you are going to need and that's an initialization then this baud rate select is just a function that will generate a list of baud rate i put it doesn't function it can be just put it um with a something in it but just i put it here for make easy to understand after that update com is a quite interesting function because that's a function that looks for all the com connected to the pc and find this one and let when let's keep it like this for the moment after that we do have the read serial we will forget about this function for the moment but this is the function that reads the data from the microcontroller after that so we do have the connection and for the connection this this function is you see here the connect button is not working and because it's um disabled so if i select this one and select the baud rate then i can click into connect and that's quite a very important thing after that for the connection is when i push this button and connection function will call the serial where is the stereo one the serial reads when i push this one my program python program will start reading so this is the code so let me first yeah and we do have the um this is our microcontroller and this is the potentiometer let's load the code so let's just save build and load after that let's connect and see what will happen so you can see i'm having some here some value appearing and some position and when i start moving you can see this is moving accordingly and this is our main program so i hope you got it about it so this is the code that we created in the previous tutorials where you can have a simple animation based on the value of the potentiometer so this is the part where we start controlling the pin level between low and high and so we are going to send the data from the pc using the main thread to the mcu through the sdi and within this part we actually the python will generate a message and send it and we'll we are going to structure these measures so we can make this work perfectly so that message structure will be as below so first of all we are going to send a zero or one to say we are going to change the pin level to low or high then see the port name and after that the pin number of the port finally we are going to add the terminator which is not a movie and which is the slash n or the new line to the microcontroller based on that we are going to put some logic in the microcontroller so we can see how this code is working so let's take a look on how this function is going to change so as here at the right side we have the old code and the left side is a new code and let me drive you to the changes we applied for the code to to start making this update first of all just change it to through geometry from 500 by 500 here to 800 by 600 as we will need a little bit more space because we are adding further buttons after that first thing which is a little bit more useful thing is to remove the graph so this grid remove for the graph that you do have or the dynamic canvas that we use will not really delete it from the memory but they just hide it and what is good with this function is that it will keep reading keep out on in memory the grid parameters we put it uh previously after that so here we used to have just agreed and that's all then we are going to add and this is which is our previous code ends at the end of that code we are going to add let me make picker here a new button which is the toggle bottom and within this button which have pin high at the beginning and after that it will call a command toggle pin we will take a look later on on the toggle pin but we are adding a bottom here and let me run the code for it and show you the graph so right now we don't see nothing and we will discover but you can see everything in hidden is hidden within this graph okay so after that let me back again if we go to the previous code for the connection we just start by saying okay if my text is disconnect then we will do something that will disconnect else we will start the connection and start read serial which is just start reading the data for the adc however if we go here and we take a look on how things are going but we stopped the part where we are going to read the data immediately which is not the thing that we need and what we are going to do is actually we comment everything and we start showing the toggle pin when i will connect as you can here when i push connect i will see the pin high here and you can see again too if i click on disconnect we should see the pin the pin at the sorry the toggle pin button disappear okay so that's the first part after that let's go to the toggle pin function which is a little bit here on the top and doesn't exist within this code so within this toggle pin what you can see is that first of all if my pin high we print p9 just for debugging purposes and after that we get a message and which have one which which means i would like the level high and after that 40 and pin 13 to and after that new line or to say that's the end of my message and after that send it but careful guys you have to put a dot in code else you will have a trouble sending this one and this is to move from a python string to the string a kind of a non-binary string but one made of chars and changing the pin a name from pin high and below and if we are not pin high we are going to print pin low because we are sending a command to set the pin low and after that the pin off message will be 0 c30 n and send also with the encode and after that toggle the name of the pin if we go here again and push this button we will have this pin high and the pin name will change as soon as push on this toggle so far simple and easy so let's go then to the c code and take a look at how things are going on first of all i'm commenting this help function and function because i'm adding them directly in the message function and this is the function that will manage the reception then there is a library related to uh that i'm using at its full potential and if you are interested to see how we set up all this there's a video detailing really all the steps that we have made as we are going to hear interrupts yes we are going to use interrupts because it's important to minimize the impact of keep waiting for the data we are not going to wait for the data but generate a signal each time we receive interrupt data so this is the uart manager which is the array that we do have here and we're going to read few examples like sorry not for example details to understand more so this table is used with these parameters so we are going to focus on the signal on terminator and terminator chart so the signal means that my message is complete and i can start using it terminator is the option where i choose it's equal to one then i will be receiving a message based on the terminator zero is a different thing that we used before for the esp a266 we are not going to use here and terminator chart is the defining which chart we are going to use to say this is the last chart i should receive before saying that this is the whole message okay and then let's go at the end when you can see i have set up the url to irq handler and this is the function that will manage and read everything okay good after that quite very simple and easy just take your what in it which is totally normal and inside the uart you need you do have the whole activation of the iq and finally the digital output as we are going to blink the led inside the board which is the port c pin number 30. then at the end what we are going to do our main program first of all if we do have a signal so if this one is equal to 1 and this will be equal to about as soon as we found out there is um this terminator here so what we are going to do first of all put it zero so we don't get again within this loop and after that reading the data or the message that we are going to use because every message that is sent will be written here after that we are going to empty the message so we can receive again let's take a look on this pin on off that we are going to use so this one is inside the message drive inside this one is very simple so this one would check if message is like the first chart of the message is equal to one or equal to zero because this is a controlling for the pins and then so we get then the port the port from the message one and as we have seen together on the um the code that we have sent inside the um the python let's go back again so this is a good we are going to send first this is high or low and after that the port number or the port name so this kind of a double check if this is not a port this is not a product already characterized or defined which is here get port if it's a b or c to return the number here then it will not enter the loop and mess with it if it's good and this one this function would return as you can see here at the moment get bored it will return report if it's correct char else it would return zero and after that we get the level so message zero so the first character of this message is zero but this is a char and if you would like to have the right better the number not the char we have to put minus 48 or the 32 hexadecimal if i'm correct okay and we just run our normal function which is the right energy so we get the port from the uart message one and then get the pin from the uart message also too so the get the pin function is also a small function that will read based on specific location and finally level zero or one and return one means everything is good because we can use this function later on as even with a kind of conditional if you would like to have more um sophisticated response from microcontroller and that's all so this is the function it will read it will carefully read the message and change it after that the best thing to do is just to write and take a look on how the code works okay so this is the microcontroller here you can see that we do have the led low this is the led low because it's a um this is the pin is made like this so if i pull it high it will stop if i put it low it will um be high again as you can see this is juggling with no issue and working quite properly so we we have seen how to toggle a pin using python and also how to interpret it using c so this is the previous way where we created the message so you you could guess now if we would like to set up the adc reading we are going to use the same logic so the pc will send the data to the fcti to the microcontroller however this time it's a bi-directional um communication so the message that the pc will be sending is will be like this so exactly almost same at the previous one so at the beginning here it will be a to say hey we are going to enable the adc or checking the adc mode and this is a port name and then the pin number after that so we add if we would like to start or stop the adc then finally the terminator so this is a logic and we are going now to check how this will go to work let's get back to our program and as usual the one on the right is the old program we have made in the previous video and the one on the left is the new one that we we added some update if i go to the end here to check my program you can see i added here compared to this one where i did not have under the get adc button so let's make this one bigger and you will see on this one this is a gate adc a button that will re i will call a command called gate adc and also we will put it in the row number five but we are going to remove and this is because as soon as as far as we are not connected we don't need to show this button after that let's take a look while we updated the connection function which is one of the main function that you have let's go to connection here and start take a look on it but for this one let's cite by the else where we are going to connect i added here at the end the graph the canvas and the adc button so when we connect we add this one and when we disconnect we remove them so let's run the program quickly to see what's going on by running this so if i go this one and choose this portrait and connect you can see my pin high button that we checked later on previously and the start adc and also the animation for the campus let's disconnect and close and go after that so this is quite a very simple one but we still have threading and this threading is the one that will be reading all the adc data so let's go up and we put everything on the gate adc which is here so this gate adc which is totally not in this function so we can get more space here so the get adc will be focusing on first of all checking if it's get adc then it will ask here to stop adc or toggle what is written print adc just to see it from the terminal perspective and then it will send this message which is pin on a for analog the a port a 0 0 means for the pin 0 so i would like to have the pin 0 as analog pin and start the reading and then after that encoding the code encoding the message so it will become an array of char and can be read by the microcontroller and put serial through means this is for the loop on the threading and start reading after that if we do not read so we do have the adc off written so we toggle and we put serial data false after that we send a message to the microcontroller saying that we are we would like to stop and go to stop it so this one should be zero not one actually and we stopped reading the data and we send it and that's all this is what we would like to uh actually actually it's not e this one is a okay and by stopping this one so we can start the um toggling this one and we do have this one like here to stop the animation so we have taken a look to how the python will be sending the data now we can jump to the to the kl and check what's going on if we go here first of all i would like to share with you i added this gui control variable this gui control variable will control a very important element which is reading the adc and send it so what will happen when we will have a signal as we have seen and we get the message so as this one is a pin on or off will be as we've seen here where is it so this is a pin on off we'll have some checking on this one yeah it will check if it's zero or one then it will accept elsa with your return zero so it will not care then it will jump to this adc on off function and in an on off function they will check if the first char of this array is equal to a so it will go inside and manage the the adc communication that's what we'll do and we will get inside it later on so if it go inside it it will return the number here zero or one that will control the reading the data and sending it let's go quickly here so first of all get the port is exactly the same function and after that you check if it's equal to one or equal to zero but if it's equal to one then it will start and initiate the adc and that's what we will do so that's why also we put it here so let me first we put it here the adc libraries don't need to have it inside here and that's why it's commented after that after reading that we will initiate and putting gui control the first digit to one we can start the loop of the reading and sending else if it's equal to zero we put the loop control here to zero and then it will not be reading anymore and it will stop this loop that is reading the data and then sending it as a basic message so you can see here basic message function which is very simple just controlling it's a function together it's the one changing the integer to char or the string after that send you what for the string and finally you go and send it the uh the slash n which is a terminator that we will use for the python as we have seen in the previous video example okay so we have seen all how this is working i think it's a good time to start seeing the code how it works so let's go i think it's already running i know we closed it let's go and open it we are reading com3 and this is serial that we the the portrait that we need we are going to connect and better to put the camera on so let's perfect so let's change we can toggle and let's start reading so you can see here i'm moving this one and i can change so let's stop and see it's not moving anymore from the graphic perspective i can still play as i want with toggling the pin and also running and playing with this one so you can see both are working perfectly and also just to let you know this is a very simple example but you can expand the pin because this one is only one pin you can add whatever the pin with this configuration that we have created and also whatever the adc depending on the channel by reading here now this is starting so now i'm starting back and you can see everything is working well i hope you enjoyed your video and you can find you could find it useful and see you in the next one bye
Info
Channel: WeeW - Stack
Views: 1,446
Rating: undefined out of 5
Keywords: python, threading, thread, Curses, curses library python, arduino, arduino serial read, Python Pyserial, Python arduino, Read data from arduino using serial, Using serial data and python and thread, Python UART communication, Python MCU UART communication, STM32 PYTHON, PySerial, Python GUI, tkinter, MCU
Id: tBgTSdzkSGM
Channel Id: undefined
Length: 30min 8sec (1808 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.