Raspberry Pi LESSON 43: Building an Improved Client Server Connection to the Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number 43. in our incredible new tutorial series for your teaching your Raspberry Pi who's boss what I'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sun founder Raphael kit most of you guys already have your gear if you don't look down in the description there is a link over to Amazon where you can get this kit and believe me my life and your life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to show you is really a follow-on to the lessons that we did in class number 41 and class number 42 and there I showed you how to transfer data back and forth between your PC and the raspberry pi over the Ethernet or over the Wi-Fi using UDP last lesson I showed you how to create a simple client server relationship where the uh where the Raspberry Pi was the server and the PC was the client but the client was just sitting there going go go go go go go and so the client would send the command go when the Raspberry Pi got the go command it would go it would measure temperature it would measure humidity it would create a data packet and it would send the data packet back and so it was sort of like go the command was done the data was returned it was read and it was sort of like go send return go send return it was just going in a loop like that well that showed you mechanistically how a client server relationship works but I want to show you a little bit better one today because really if you think about it there's usually going to be a lot more Nuance with the setup that we had we could do what we could measure temperature or we could measure humidity and so what we want to do today is we want to create a little bit more sophisticated client server relationship and also make you just start getting more comfortable with these con with these Concepts but what we're going to do today is on the PC side your PC in a Python program is going to prompt you for a command and you could have two commands you could have temp for temperature or Hume for humidity then it's going to get that input from the keyboard the PC is going to send it over to the server the Raspberry Pi who is SWAT sitting there waiting for a command the Raspberry Pi will read the command if the command is humidity it's going to measure humidity if the command is temperature it's going to measure temperature it's going to create its data packet and it's going to send it back and then over on the client side you're sitting there waiting for the response and then you get the response back and then you decode and unpack that data okay now another thing that another concept that I want to give you because you've got to remember you could have more than one client asking for data and you could have all types of things going on on the server side and so there is opportunity for various things to go wrong that you might never anticipate could go wrong and so let's say that you're you're doing temperature and humidity and let's say it's a very dry day so you say okay you send the command temperature and then you get back the number 85 then you send the command humidity and you get the number back 10 okay and then you do that a few times and then you ask for humidity and you get the number back 95. what clearly has happened there somehow some way between you sending the command it doing something it may be servicing other clients and it's sending you data back somehow you would ask for temp you would ask for humidity but you got temperature back and so really a better way to configure these handshakes these relationships between client and servers is something that we learned way back in first grade and that is always answer in complete sentences so if the client says what is the temperature how should the server answer the temperature is 95. now if you if the client says what is the humidity and then in the clients or the server came back and said the temperature is 95 you would know oh I might have asked for humidity but for whatever reason I got temperature I'll ask again or something like that but you want to tag the data so that before you go off and make a decision based on the data you got back you're sure that you got what you asked because this might be like something really important like maybe you know if you got the wrong one you would turn the sprinkler system on where already the humidity was too high okay so you see you want to make sure that you're making decisions based on good data so you want to absolutely limit the ability for there to be any confusion or any mistakes and that you didn't actually end up with what you asked for does that make sense I hope so hmm little activity back over here on the island okay I always like to see a little activity back over on the island but what we're going to do now is we are going to move over to the Raspberry Pi to the server and before I do that let's just talk real quickly about our setup here and I showed you how to do this exhaustively in lesson number 25 I have the DHT 11 temperature and humidity sensor going to the breakout board hooked up to the Raspberry Pi that is not sitting perfectly straight we got to correct that we cannot go through a lesson with a wampy jot Raspberry Pi that looks much better this looks pretty straight I think it needs to come down just a scooch perfect perfectly Square okay so you can go ahead and hook this up I give you a quick schematic here you can see that there's just three connections over there it's very very simple the one thing I will show you is don't forget that 10K resistor between the center leg of the DHT 11 and the power rail very important for you to put that 10K resistor in there and then you can come over to like pin 27 or pin 17 on the breakout board either one of those is fairly convenient but you can go ahead and hook this up the one other thing that I could should tell you if you are jumping into this lesson and have not taken the other lessons to use that DHT 11 you do need the DHT 11 library and it does not come installed with the Raspberry Pi operating system but you can do just a simple pip 3 install DHT 11 and that will pipe pip 3 install DHT 11. okay okay and it's going out looking for it and for me I already have it and since it says already satisfied it just goes on but you might take you know 20 30 seconds to get that installed and then at that point we are ready to come up and start writing our program and so we're going to open up the most excellent Thani okay and some people call it phony I think I like to call it Donnie and uh let me bring it down here all right there it is okay close that assistant now what are we going to start with we're going to create the server on the Raspberry Pi now a lot of this will be familiar to you from last week in the week before last but I like to do it again along with you just so that you're getting familiar with it so if we're gonna do communication over ethernet we communicate over a socket so we need to import we need to import socket we need to import time we're going to be talking to that DHT 11 over the gpio pin so we need to import RP little I dot g p i o s g p i o so now we can talk to those pins let's go ahead and set up the mode of the gpio pin so we'll say gpio dot set mode and we are going to be operating with the gpio.bcm numbering system why that breakout board has labels and it's labeled in the BCM protocol okay so BCM it'll if you do this it'll match the number labels on that breakout board and now I want to go ahead and create my sensor object and so I'm going to say my DHT is equal to little d h t 11 which is the Library dot pigdht 11 which is the method and now how am I connecting to that I am connecting through pin 27. you put whatever pin you use but if you use 27 make sure that Center leg is connected to the breakout board on pin and 27 as labeled now let's start working on the server so we've got to tell it how big of a buffer we want to use so I'm going to say buffer size let's make it amply large so let's make it 10 24 and then I need to go ahead and set up my server IP and that is going to be the IP of my Raspberry Pi so I come over here if config like that and you can see that I am on Wi-Fi operating at this IP address let's see if I can just grab that and then come back over here that we need to put in in single quotes and so it is going to be like that okay that's the IP address now I need to do a server port and the server Port we can use 22 22 it doesn't matter just make sure you're using one that's not being used and make sure you use the same one on the client and the server hopefully that makes sense Okay so we've got our Port set up let's go ahead and start our server so we will say RPI server is equal to socket the Library dot socket the method and then we've got to tell it that we're doing UDP so we've got to give it two parameters so it knows to use UDP one is Socket dot AF underscore i n e t a f underscore INE T and the other one is Socket Dot dot sock underscore d g r a m like that I think that looks good okay now what we are going to need to do is we are going to need to go go ahead and bind that server to the Raspberry Pi's IP address so that the Raspberry Pi the server knows that it wants to be going through its own IP address and in fact it sort of lets it know that it is the server so we are going to do an we are going to do a on we're going to do uh RPI server dot bind and what are we going to bind to we are going to bind to a tuple so I need to open another I need to open another I think it would be kind if I made this a little bit bigger for you guys let's see if I can fix that real quick here let's see 24. does that show up a little better yeah I think that'll be a little easier for you to read so I open the bind and now I open a parenthesis for the Tuple so there's two and I think I already I think I already or I can just put in here now server IP and then comma server port and so now it's going to know what to bind to both the IP and the port so now you close the Tuple and then you close the bind all right does that look good I think it does and now your server is running so I'm just going to print on this side I'm going to print on this side server is up and running like that close the print okay now we are going to want to sit here and we're going to want to listen and we're going to get a command do something and then we'll listen again so this should be in a while loop so it just sits there acting as a server so we're going to say wow true when it's true true is always true so we're creating an infinite Loop here and so what do I want to do I want to sit and wait for a command and when I get that command then I'm going to read it so I'm going to read it into CMD and address when a client sends me a command it sends me the command and it sends me its address so I've got a what I've got to read the command and I've got to read the address and then I do that at the server RPI server and then what's the command recv like receive from recv from and then how big buffer size now how does it know where to go how does it know where this is well it knows where it is because we did this bind up here so you see we don't have to give it an IP address because we bound RPI server we bound it to this uh to this connection here okay so that looks good so we received from now now we have a command now we need to decode it so I'm going to say CMD is equal to cmd.d code and then we are going to decode it with UTF -8 and then we're probably going to use that again we're probably going to use that again so let's just see if I can copy that all right so it would be easier next time and now I'm going to print the command and that is just going to help me in my debugging on this side I'm going to run it and see you know to see that it's working so that's just kind of a little print statement to help debug foreign okay so we're going to print the command now what three possibilities are the command that we would get would well one would be that we get the command temp which means we want to measure temperature the second possibility we get the command humidity that our Hume that we want to measure the humidity and the third option is we get some other command and that would be an invalid command anything other than those two would be an invalid command so we need to deal with those three possibilities so let's see if CMD is equal equal to temp that will be the allowed command temp then what am I going to do well what I'm going to do is I'm going to want to get the result from the dht11 so result is equal to my DHT mydht dot read like that so I'm going to read now sometimes you don't get a valid reading so if the reading is not valid so if valid reading is false meaning that you didn't get a valid reading you want to read again and so what I'm going to do is I'm going to say while result dot is underscore valid like that while read underscore is valid open close while that's equal equal false so it's false the reading is not valid which means the reading is not valid and so what I'm going to do I'm just going to put a print just so that we sort of see that that happened that we got down here and I'm going to say bad reading trying again so we know this is going to happen so we're not going to panic we're just going to say oh we got a bad reading and then what do we want to do we want to do another read so we'll say result dot is underscore valid okay or no I'm going to say uh result is equal to my DHT 11 dot read so we're just going to do that read again now what you have to see is what you have to see here is is that if this reading here is bad it's going to stay in this Loop so it's going to stay in this Loop until it what until it gets a good reading and then once it gets a good reading then it is going to drop down here if that makes sense okay now I've got what I've got my result there and so now that I have my result I need to go ahead and start building my data string that I'm going to send back well I'm going to send back the command that I just did because I thought you said temperature so I'm going to return to you data that includes the temperature tag so I'm going to put that tag back on just so the guy on the other end can see that he got back what he asked for I hope that makes sense so it's going to be command and then I'm going to add to command delimiter or a separator of a colon and that way on the other side I can split on the colon again and then I'm going to add and I'm going to add what the string value of result dot temperature so now I've got a problem up here you see where it was green I did not end doesn't count as a mistake if you catch it before you run it okay so now I've got my data packet put together and that looks pretty good and so now I need to include encode it for send so I'm going to say data is equal to data dot in code and then let's see if I actually ah yeah I'd copied that correctly and so we encode it with the utf-8 again so it's Inc encoded and so now let's send it so we'll see RPI server dot send to who do we send what do we send we send data who do we send it to address and who is address addresses whoever asks for it so whoever sent me the command also sent me his address and then here I'm sending him the data back to that same address that really I think should make a lot of sense shouldn't it now we're going to do the same thing for humidity so I'm going to live dangerously and take this whole thing and come down here and paste it only this time instead of the command of temperature we're going to have the command of humidity and then we're going to read it that all looks good that all looks good here though instead of result.temperature we are going to send back result dot humidity I think that's the only thing that I need to change there and now finally I need to do that third case which is if I got something that I don't know or I got an invalid command so I'm going to say what are the invalid commands well if CMD is not equal to humidity and CMD is not equal to temp what have you got you got yourself an invalid request and we're going to sternly warn him about that now we won't we won't be too Stern but we will we will let the user know that that was not valid and so what we're going to do is we're going to say data is equal to CMD so that command he sent us we're sending it right back to him and then what are we going to send with that plus the delimiter again like that and then what are we going to send I'm just going to send the string null and anytime the client gets the null response back he knows that he sent something that I did not know what to do with so does that make sense okay so we're going to have that and now we're going to data is equal to data dot in code and we're gonna see if I can still do that ah no no no no no no no I messed up bad there I messed up bad I forgot that I copied all that code that was a terrible mistake if you live with copying and pasting you're going to die with copying and pasting [Music] okay now what we're going to do is we're going to send that back to him RPI server dot send to what are we going to send that data and who are we going to send it to address like that okay guys how many mistakes have I made along in here that I didn't see okay so we're going to go ahead and run this now just to get find any gross errors that I made ah humidity what is that nonsense okay didn't like my Hume okay and that's in line 26 if command equal equal him maybe I've got an indentation yeah okay you see when I pasted I didn't get the indentation right so I take everything out and tab over I told you if you live by the copy and paste you die by the copy and paste dht11 not defined what is that nonsense that is in uh that is in line six okay my DHT is equal to oh I did not import DHT that is terrible okay so up here I needed to import the library d h t 11. so that makes sense let's run this you guys get to see a little bit bit of real life okay server Port I can see that I made the O I made the O wrong there so that is in line 13 here that was a large o and it needs to be a little low okay and now what it says is I can see that you guys unfortunately we're not seeing all of that but I can see here that it says server is up and running so at least I got rid of kind of the gross errors there so our server is up and running and so we need to move on over here to we are moving on over here to the PC I have a little sip of my jet fuel and I need to switch keyboards so I get my keyboard PC and this is actually going to go pretty fast because the uh the server side I mean the client side is really a lot easier than the server side and so what we are going to need to do here is [Music] okay so we are going to need to import the socket again we are going to need to import time again I'm going to go ahead and set up the server address now guys this is something that a lot of people get confused about like because on the server side you put in your own IP address the server IP address and so what people sometimes do is they come over to the client and they put the client's address no on the client side you also put in the IP address of the server okay it's just you've gotta you've gotta not try to change that and it's it's a little bit a little bit confusing to people so I put in the server's address 192. [Music] 1.168.88.41 Pro tip you use your server's IP address don't use my server IP address right you use your Raspberry Pi's address so okay so I have that and then I created a two pull there with that open parenthesis so I'm going to go ahead and put in here the port number which is 22 22 and this needs to match whatever we did on the other side now also we need to tell it the buffer size and this needs to match 2 and that's going to be 10 24 like that that looks good now let's create our client UDP client is is equal to socket the Library dot socket the method and then again those same things socket Dot AF underscore and net and that tells it that we're doing UDP and the other part parameter to tell it UDP is sock underscore or d g r a m like that okay so now we have created our client and that looks pretty good socket dot socket that looks pretty good now we need to go out and get ready for a command so again we're going to put this in a loop Paul true one is true true is always true so we created an input Loop now we're going to get the command now from the user so I'm going to say command is equal to keyboard input so I put input what is the prompt The Prompt is what is [Music] your command and then like that what is your command is it as simple as that let's see I think it is as simple as that oh no no I know what I need to do I need to give it its choices okay because right here you could do a whole lot of error checking but we're just going to assume that the user is adept enough to enter the choices that he is given so we're not Idiot proofing it if you will okay so now we need to uh we we've got the command now what do we need to do we need to encode it so CMD is equal to CMD dot in code we gotta encode it for sending and then we'll do it with UTF -8 again close and close and then I think I will copy that just for fun now we've got the command the command is encoded and so what do we want to do we want to send to so I'm going to do UDP client dot send to okay UDP client dot send to what am I going to send the CMD and then who am I going to send it to the server address now why do we have to put that in because there could be 10 servers and we got to tell it which one do we want to send it to server address which we set up up here so we're going to send the command to server address now once we do that what do we need to do we need to now listen for the response to come back and so with that we'll do a UDP okay a UDP uh uh no I've got I've got to read it into something so now I'm going to get I'm going to get data back and then I'm going to get address back so I'm going to get data and address two things back and that's going to be UDP client dot receive recv from and then just buffer size okay and then where is it going to be listening to well whoever it sent the data to it is going to be listening for the data to come back from there and so this server address on the send is what Clues it in as far as what to listen to on the way back this address here is the address that it gets coming back from the server it doesn't have anything to do with where you're sending it's a value that you're reading hopefully this is starting to make a little bit of sense now okay so now we have the data and so we've got a data is equal to data dot d code and then hi I got that from copying quala go and now what I want to do is I want to say my data array I want to take that that's like a command a colon and a number and I want to split I want to split data so I'm going to say data.split and where do I split it I split it at the character colon and so now I should have an array with two elements the command in the first element and the value in the second element okay I'm just looking making sure that this is making sense I think it does now there are three possibilities if data array the zeroth element if that is equal equal to temp then what do I do well I print temperature [Music] and then what data array one Now understand that this is going to be a string but we're not doing any calculation on it it's not going to be the number 75 it's going to be the string 75 and that's okay it's just if you're going to do some math on it make sure that you convert it to a float okay so that is going to give me the temperature now almost the same thing if data array of zero is equal to equal to humidity Hume then print humidity and then what date data array same thing one because if it gets here to this point because of the if statement that number will be that number will be what the humidity I gotta close that I didn't close the parentheses [Music] okay so then what is the other possibility if data array of zero is not equal to Hume humidity and data array I better get that [Music] where it doesn't go behind my head and data array of 0 is not equal to what temp if it's not one of those two things it's what it's invalid okay and then what I'm going to do is print invalid response and then what I'll do is I'll go ahead and print data array 1 which just should say null there just to kind of confirm form confirm that the world is operating as I expect okay over on the other side we had left the server running I do believe on the other side we left the server running so let me see here I just need to make sure that you can see this all the way I hate this thing where it pops it to full screen even when you don't want it to okay I think that you can see that pretty pretty well I hate it when it does what it just did I'm not going to fine tune it too much this time because when I fine tune it what happens it goes and it pops it full window okay let's run it and see if we can find our errors so we're going to say run module okay it's going to save it and then I'll just say CS2 dot py like that okay ah look at that at least we got a I missed I can't I can't do it without missing with that missing parenthesis let's go ahead and fix that so uh right here I'll put a temperature humidity okay so now let's run it again it's kind of encouraging I didn't get a gross error anyway okay let's say t-e-m P like that okay so now I'm not getting a response back the first thing I'm going to do is I'm going to come over here and I'll make sure that I've got that server running on the Raspberry Pi side so let's uh let's stop things make sure it's stopped make sure it's running okay now let's come back over here let's kill this okay I killed it let's try running it again over here run module okay so I'm going to say temp okay and it's hung again so the question is what did I leave out so let's make sure so I said I got temper humidity that's good I encoded it I sent it and then when I sent I didn't get anything back and so let's go over to the side over here I'm also 192 168 88 41 that looks good over here so ah I have found my error it is over on the server side I created my DHT as the object but then when I called the object I called it as I called it as my DHT 11 right like that and so right there is where I got the problem and then I copied and pasted so I better come down here and I better fix it there and then up here I did get it right I hope probably a lot of you guys found me I saw that I'd done that and so I will try this again okay so I'll try this again I will come over here I will run and then it says server is up and running down here uh server is up and running let me show you I'm sorry that title is kind of in the way but you can see Server is up and running and so that makes us happy and then we're going to come back over to the client side okay and now I will run that and same thing I want to get this where you can see it okay so what do I want I want temp and then when I say temp Shazam look at that 26.2 temperature humidity let's try temp again giddy up 26.3 my excitement radiated a little bit of heat that was detected by the dht-11 let's try humidity [Music] boom 65 humidity that sounds right Hume Hume Hume temp and now let's see our error checking let's see if I asked for pressure and then it comes back invalid response null and then I can go back to Temp boom that works okay I'm going to try one other thing here real quick I want to see if this is robust and so I want to see that if I just keep hammering this thing if it keeps working so I'm going to take this com I'm going to take I'm going to comment that out and then what I'm just going to do here I'm just going to say that CMD CMD is equal to Temp like that and then down at the bottom just so that I'm not like going too crazy fast I will put a uh time dot sleep of 0.1 just something like that but I want it to go fast but I don't want to kill it so let's just go run run okay okay there it goes so let's just let that run a second and see if this thing looks pretty uh pretty rugged now in reality when I'm setting up something like this I would like to let it run for two or three days okay I would let like to let it run for two or three days to really make sure that there's not some unusual condition that would cause me to lose that lose that socket but this is looking uh this is looking really really good the thing is just whirring on by okay guys I hope I haven't bothered you man it's been like Five Lessons right we learned how to SSH into our Raspberry Pi with putty so that we could have a remote terminal window and make the Raspberry Pi do whatever we wanted through the terminal window then we learned how to use VNC for you guys that are a little bit more comfortable BNC for you guys that are a little bit more comfortable with a graphic user interface I will be honest with you I really don't like using via and c I can do pretty much whatever I want from the command line so I usually if I'm going to do things that way we'll just use putty to ssh in and then three lessons ago I showed you how to just do a simple connection over UDP then last week I showed you a simple client server and then today I showed you a little bit more robust method of creating a client server where you're a little bit more deliberate in making sure that you are getting making sure that you are getting the data that you think that you think you need or make sure that you got the data that you were asking for okay guys what's coming up next I think is I actually want to go in and on our next lesson I think that I am going to want to show you how to use the Raspberry Pi camera from your kit on the uh use the Raspberry Pi camera from your kit on the Raspberry Pi and so what would be great is and I think I mentioned this last week but really the only way that I know how to make it do what I want it to do is for us to be on the Buster operating system and so it would be great if you guys now preparing for next week went ahead and flashed an SD card for the Buster operating system so whatever the full graphical interface not the light version but just the full version of Buster on a SD card then next week I'll show you how to install opencv how to get gstreamer working and my goal in next week's lesson is to just have a video window that pops up and shows you the video coming from the Raspberry Pi camera and so that's kind of what my goal is for next week next week might be a little bit of a long lesson because we've got to install several different libraries and so forth and the actual code that we'll write will probably just be like you know eight ten lines and so the code will be easy but we just got to go through a process to get your Raspberry Pi configured like mine is so that when we try to do this the stuff on the lessons going forward will work properly and I will say I will let you know that we're going to do quite a bit of of lessons on the camera because there are just so many things that you can do so many exciting things you can do when you add a camera to your project okay guys I hope you are enjoying taking these classes as much as I'm enjoying making them if you're enjoying them be sure to give us a thumbs up also it's a really big help for us if you leave a comment down below that helps us with the old YouTube juice subscribe to the channel if you haven't all already and most importantly share this video with other people because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos Paul McWhorter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 4,980
Rating: undefined out of 5
Keywords:
Id: 79dlpK03t30
Channel Id: undefined
Length: 42min 21sec (2541 seconds)
Published: Thu Jan 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.