Raspberry Pi LESSON 42: How to Build a Simple Client Server System with Raspberry 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 42. in our incredible new tutorial Series where you're teaching your Raspberry Pi who's boss what I will 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 excellent series of video tutorials and in this class we will be using the sun founder Raphael kit for Raspberry Pi the sun founder Raphael kit now most of you guys probably already have your gear if you don't have your gear yet look down in the description there is a link over to this kit and you can pick one up for yourself at Amazon and believe me my life and your life are going to be a whole lot easier if we are working on identical Hardware okay enough of this Shameless self-promotion and advertising let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in lesson number 41. and that homework assignment was to create a simple client server system based on the Raspberry Pi the Raspberry Pi is supposed to sit there and make temperature culture and humidity measurements at the command of the client so the Raspberry Pi is the server it is able to make temperature and humidity measurements when the client asks for a measurement the Raspberry Pi Runs Out makes the measurement and then sends the data back to the client let me start by asking how many of you guys were successful if you were successful leave a comment down below I Am Legend double chest bump and if you are not successful leave a comment I folded up like a cheap Walmart lawn chair so you guys let me know how many of you were successful this should be pretty straightforward because in lesson 41 I showed you how to pass data back and forth using UDP and so I sort of showed you the skeleton the nuts and bolts of how you actually pass the data and now with just a little thought you ought to be able to create this simple client server type relationship okay so if we're going to do this the first thing that we're going to need to do is we are going to need to hook up the raspberry pod to the dht11 and I will remind you I will remind you that I showed you how to do this in lesson number 25 in this class and this is how I did it and then this is the schematic over here and so it's a very simple just very few number of blind very few number of wires that you need to get this thing hooked up you should be able to look at this and do it if you need more help or more explanation go back to lesson number 25 but I don't want to spend too much time on that because I want to jump over here and I want to jump over here and I've got to do one little I got to do one little bookkeeping thing here to get my windows right okay I need to come over here this is the PC window and so we're going to start with the server the servers on the Raspberry Pi so we're going to code up the Raspberry Pi first I hope that makes sense now you you see that we're using the DHT 11 and if you guys have not done this already you need to install the DHT 11 library and you can do that in a terminal by just typing pip 3 install and I want to install install the DHT 11 like that and then when I do it it's going to say that it's already installed because I did this before but when you do it it will take maybe 30 seconds to get the library installed and then you will be right where I am okay so let's start over here and let's start coding up our server well if we're going to create a server what library do we need we are going to need the socket library because the socket is what connects the client to the server so we are going to need that Library I always like to import time because it seems like I usually need to put a few delays in here or there we're going to be interacting with the DHT 11 through the gpio pin so we will need to import RP little I Dot gpio as as gpio like that and then we're going to need to import the DHT 11 library that allows us to make the temperature and humidity measurements now we are going to be interacting with the DHT 11 through the gpio pin so we need to set those up gpio dot set mode and what do we want to do we want to do gpio.bcm and that way we can use the BCM numbering system why do we do that why do we do that because we do that because this is labeled the breakout board here is labeled in the BCM numbering system and so that just makes life a little bit easier so we're going to do the set mode and then I need to create my sensor object so I will say my DHT my DHT is equal to DHT 11 the library DHT 11 the method and then we're where I am I if I look closely at mine I'm connected to BCM pin 27 like that okay so now the sensor is set up now let's go ahead and do a little bit more work in getting our server set up so first thing is buffer size and so we need a buffer to put the data to send back and forth let's make it plenty big it doesn't have to be exactly the size of the data but just make sure it's big enough to hold the data so we were oversize it to 10 24. now we need to have the server server IP what is the server IP the server IP is the IP of your Raspberry Pi why we said the Raspberry Pi was going to be the server so I can do a if conf fig here if config and I am hooked up over Wi-Fi and so I can come down here and I can get my IP address there I wonder if I can even copy it and then come over here and then I'm going to put it in single quotes and I will paste and close the single quotes this is my IP address you don't use my IP address you use your IP address that's a little Pro tip there okay me believe me there have been people that have failed because they tried to use my IP address instead of their IP address okay we're also going to need to set up the server Port just use a port that's not in use I'm going to use 22 22. and I want you to see that no quotes around the 222 all right now I'm going to go ahead and create my server object so I'm going to say RPI server is equal to socket the library above dot socket the method and now I've got to set this socket up for UDP and I showed you that last week we do it with the command socket dot a f underscore i n e t Okay AF underscore I uh AF underscore i n e t and then socket dot s o c k underscore d g r a m like that that should set up our Raspberry Pi server now once we've done that we've got to tell the server where it should be listening so I need to take the server RPI server and then I need to dot bind it and where do I bind it will I bind it to my server IP and my server Port but understand that it wants a tuple here so I need to come back and I need to open the parentheses again to start the Tuple and then I need to close the Tuple and then I need to close the command so this will tell my RPI server to look at its own IP address to be listening there at Port 22 22 if that makes sense I hope that makes sense so now my server is up and pretty much ready to go and so I think I'm just going to print at this point I'm going to print uh server up and listening [Music] okay at this point it's all set up and it's going to be waiting for data so what I want to do is this time I don't want to just go out and get one command I want to constantly be listening for a command if there's a command read the command and then do what I'm told to do so what I'm going to do is I'm going to set that up in a while loop I'm going to say while true one is true true is always true so we're creating something of an infinite Loop here and then I'm going to go out and I'm going to try to read the command CMD and then when I get that command from the client that client is going to send me his address this address is the address of the client that the client is sending me with his request so I'm getting the command what he wants me to do and I'm getting the address where he is all right and so how do I actually do the read well I go to RPI server like that and I do a recv receive from receive from and then how big of a buffer b u f f e r s i z e okay and so that is going to sit at that line of code until some client on my network asked or sends a command and so it's going to sit here until it gets a command when it gets a command then it is going to read okay when it gets a command then it is going to read now once I read it I need to decode it so CMD is equal to cmd.d code and then how do I decode it well I use our old friend utf-8 which we've talked quite a bit about [Music] now I have the command in easy to use in easy to read mode format and so let's just print it because that might help us with the debugging later so whatever command we got let's just go ahead and print out that command foreign okay let's also print out the address so who this is what they want command and what who is it that's asking well let's look at uh let's look at the client address so I'm going to say client address this is who did the asking and that is going to be that address that I read and it is going to be at location zero so location zero is the IP address and location one is the port but I'm just going to look at the IP address all right Thief so now we've got to decide what to do well if the command is go I'm going to go out and I'm going to make a reading of temperature and humidity and I'm going to send it back to the client so if the CMD is equal equal go I'm going to do certain things the first thing that I am going to do is I'm going to read the data so I'm going to say I'm going to read the sensor so I'll say you the result of the read is going to be DHT it is going to be it's going to be my DHT dot it's going to be my dht.read like that so that goes out and does a sensor read now what I can say is data is I'm going to create now the data so when I do that read I have got the uh I when I do that read I uh I have red temperature and humidity from the dht11 but now I've got to decide is that valid data or is it not valid data because about 25 percent of the time the dht-11 returns a you know not good result so I only want to use the data if it's valid so I'm going to check that if result dot is underscore valid if the result was good then what do I want to do I want to create the data string that I returned to the client so the data is going to be the string value of what result dot temperature right it's going to return a number like 26 a number 26 but I want to send a string back it's going to say the data is the string value of result.temperature and then I also want result dot humidity but if I'm sending two data pieces if I'm sending two pieces of data in this packet I need to have a delimiter so I'm going to use a delimiter of a colon so I'm going to add to my string the character colon and now I'm going to add to my string the string value of result dot humidity like that okay so I've created the data packet that I want to send back now I got to convert it to bytes so I'm going to say data is equal to data dot in code we encode it with the U with the u t f Dash 8 like Dot okay I've got the data now I'm going to send it RPI RPI server dot send to what do I want to send that data that encoded packet of data who do I want to send it to whoever asked and I got the askers address up here so I just send it back to who to address [Music] all right so that takes care of that now we've got to ask ourselves the other case if result dot is valid is equal equal false what happens if the sensor tells us hey I just sent you bad data what do we do in that case I'm in that case we're going to say that uh the data is equal to bad measurement [Music] because we want to send something back we want to send something back to the client and what would just send back is hey I didn't get data so I would say uh I would send back that let's go ahead and print that out on this side just so that we can see it now let's data is equal to data dot encode with the [Music] utf-8 like that and now let's send it our RPI server dot send to and then we're going to send that data back to the requesting address so it might get data back or it might get the string that we didn't get data we got bad measurement then the client can decide what to do when there's no data there all right now the the other possibility here is those are the two possibilities if the command is go but if CMD is not equal to go well goes the only command that we've got and so if the command does not go then we just need to say data data is equal to invalid request now later we could give it a different command like instead of just you know go we could ask for pressure or we could ask for the internal core temperature of the pie we could ask for something else and it could do that but right now the only valid request is go so if the command is not go then the data is invalid request and then data is equal to data dot in code UTF dash eight not forgetting the quotes around that like that and then what we are going to do is we will come in and we will send that so RPI server dot send to we're going to send it to data and address so what are the two cases either it got a good command or it did not get a good command if it got a good command either it made a good measurement or it didn't make a good measurement and I think this is pretty much what we need before we jump over and start working on the client side let's see if this thing will run and so I'm going to just run this just checking and what you can see here is if I turn off the title ever so briefly what you can see is it says the server is up in listening so at least I didn't get any grotesque errors and so at this point I think that we can jump on over to the client side and so now this window that I'm opening up this is on my PC this is Idle or whatever whatever integrated development environment that you want to use maybe you're using vs code I'm just going to do it in idle because I just want to crank this thing out so we are over here now on the client side and so we're going to need to talk to that socket so we're going to need to import socket so we are going to come up and import socket just like on the server side the client needs to import socket always a good idea to import time now I need to set up the server address [Music] and I'm going to go ahead and set it up as a tuple and the server is who the server is Raspberry Pi so whose IP address should I put in the Raspberry Pi's IP address which was [Music] 192.168.88.41 like that and then I also need to give it in that Tuple I need to end foreign in the IP address and then I need to give it the port which was 22 22. all right now this is where people got get confused because they want to do the analogy on the client side to what they did on the server on the server side the server put its own IP address in on the client side you don't put the client IP address in you put the server IP address in and I think that's where a lot of people get hung up but if you do it this way this is the right way and I promise you it will work so now we're going to come in and we're going to say buffer size and what did we make that we had made that 10 24 and so so let's get that matched and now I'm going to create the client so I'm going to say the UDP client object is going to be equal to socket the Library dot socket the method and now the same thing we did while ago socket and then dot a f underscore i n e t like that and then socket Dot sock underscore d g r a m okay so now that should set up the client you don't bind on the client side you just go out to the IP address you don't bind this client to a specific server you can talk to whatever server you want as indicated by the server address [Music] so the server is bound to its IP address the client is you don't do a bind on the client side Okay so we've got the buffer size we've got the UDP client set up so now I think we're ready to begin to ask for data so I'm going to say while true when is true true is always true and now I'm going to create the command the command is going to be what's it looking for go and so I'm going to create the command go and then I am going to encode the command and I'm going to encode it with you guest utf-8 close it close it it's encoded now u d p UDP client Dot send to and then what am I going to send I'm going to send the command that I just encoded who am I going to send it to server address like that which we defined above all right now I've sent a command now what do I do I sit and I listen and I wait for the data to come back and so I'm looking to get my data and the address that the data is coming from even though I know it it's the server I've got to give a I've got to give a variable there for the day to come into I won't need to use that because I already know what it is but what are we going to go to UDP client like that UDP client and then what am I going to do I am going to uh I am going to go to UDP client and then I'm going to Dot let me get that out of the way I'm going to Dot recv from received from and then again I just need to tell it the buffer size like that okay now I've got to decode the data so data is equal to data.d code using utf-8 close it close it so I now have the data decoded now what you need to see is I sent temperature colon humidity so I'm going to have this long string I need to kind of parse that string now and so I'm going to say data array is going to be equal to the data that I have but I want to Dot split it and what do I want to split it at the colon and now this will give me an array the first element of the array will be the string value of temperature and the second value in the array will be the string value of humidity so I want to split on the colon so now I have an array [Music] now if it was invalid data if it was a bad measurement or an invalid command there will be one element in this array so I'm going to say if the length of data if the length of data array if the length of that is equal to 1 then what do I do I just print for whatever reason there's no data [Music] like that [Music] if the length of data array if that's equal equal to that means it sent me temperature and humidity everything is good so in that case what do I do well I need to print it out so I'm going to print temperature Temper Temper tear in the string comma and then what data array element zero and then I want to print and then I want to print humidity as the label and you can't see that can you okay so then that is humidity and then comma data array Z is it data array 1 will be humidity close the index and close the print and then let's just see who it again well I already know I know that it's coming from the server so I'm not going to print those things out and then what I'm just going to do is I am just at this point going to do a Time dot sleep of one second just so that we're not getting the data coming into quickly okay does this make sense who is in control the server is in control the server is going to come in it's going to create the command go it's going to encode that command it's going to send that command to the server the client is going to send that command to the server and then over on the server side it was just sitting here waiting for a command it was sitting here waiting for a command and then when it gets the command it breaks it apart if it is a valid command go and if it is able to read the temperature then it is going to send the temperature and humidity back to it is going to serve that data up back to the client and then the client is going to read it break it apart and print it okay so let's come back over here to the server side and then I'm going to stop it and then I'm going to run it I don't get any errors on this side on the server side so I'm going to move back to the client side and then I'm going to come up here and I'm going to try to run this now I could have all types of typos on this side so I have no idea what's going to happen yeah let's go ahead and save it and it says I've got an error in line 9 UDP client send to uh bytes like object is required not string so let's see what the problem is here in line nine it's as if I didn't it's as if I didn't uh in code command ah I see did you guys see that uh very very silly mistake I needed to say CMD equal CMD dot encode all right so now we are going to try this again we'll come up and we will run it okay so it's running I'm getting no data and boom look at that temperature 27.1 humidity 62. okay look at that now if we come back over here on the client on the server side and we turn the title off what you can see is is that it's getting the go command and then it is taking the measurement and if it's a bad measurement it says bad measurement but then if it's good it just goes ahead and sends it but like I say you can see that about a fourth of the time you can see about a fourth at the time it's not getting a good measurement I am really not sure why we aren't getting better uh why we aren't getting uh more good reads from the DHT 11 but uh you can see that it recognizes so at least it recognizes when it's not getting a good read okay this is just pretty darn cool you have created your first client server system or relationship the client is the PC it sends its request to the server the server receives the request and then responds back now what you can see here is is that the approach that we took is to just have one command go it just has one command go and then it responds with the temperature colon the humidity all right but really you can think about it is you want to probably in a lot of things you want to have more than just one command and so what your homework is going to be for next week is is to take this code and do some tweaking on this code and what I want you to do you can keep the same circuit as we have down here but I want a different client server relationship where on the client side it's sitting and it's asking the user what do you want to do and if the user types in temp then the client sends the command temp to the server the server goes out and gets temperature and just returns temperature if the client is sitting there asking the user what do you want to do any type sent humidity or Hume or whatever you want the key to be then the PC the client will send that to the Raspberry Pi the Raspberry Pi sees that humidity is being requested humidity is measured and humidity is sent back so now we have two commands the command could be to do temp or to be do Hume temp or Hume and the measurement measurement it makes in the data that it sends back depends on what the command is does that make sense I hope it makes sense okay guys man I hope you're having as much fun taking these lessons as I am making them and this is really pretty big deal stuff this is pretty big boy stuff that you can go in and with your Raspberry Pi starting from scratch create a server out of it and then access that server from clients that you are creating on your PC I think that is just really neat you guys don't forget you need to be posting your homework solutions to YouTube when you do that and you leave a comment in the you know you leave a comment a link in the comments below over your homework assignments your homework solutions I check I go in and I look at every single homework solutions that you guys submit and I'll make comments on them and so make sure that you do that when you post your homework make sure in your description you leave a link back to this video and so people will know they'll have context and sort of know what you are doing that's your homework for next week in next week's lesson I'll show you my solution to that I'll give you a few extra things to think about about how to make good sort of a little bit of fault tolerance in the way that you do this okay so we'll learn a little bit in next week's lesson as well as well as I will be showing you my solution to this homework guys I hope you are having as much on taking these classes as I am making them if you enjoyed the video make sure that you give it a thumbs up if you have not already subscribed to the channel when you do make sure you ring that Bell so you will get notifications for future lessons and share this with other people share this with your neighbors Young People your family member because the world needs more people doing coding and fewer people sitting around watching silly cat videos Paul mccorter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 8,048
Rating: undefined out of 5
Keywords:
Id: HB0mIJkkH04
Channel Id: undefined
Length: 35min 28sec (2128 seconds)
Published: Thu Dec 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.