Raspberry Pi LESSON 41: How to Send Data to the PC over WiFi or Ethernet Using UDP

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 41 and 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 most excellent series of video lessons and in this class we will be using the sun founder Raphael kit for Raspberry Pi now most of you guys probably already have your gear if you don't look down in the description there is a link over to Amazon you can pick this kit up and believe me your life and my life are going to be a lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and let's talk about what I am going to teach you today and what I'm going to teach you today is really a follow-on to the material that we've been talking about in the last two lessons and the theme that we've had in these last two lessons is how to start creating projects and prototypes that we can actually deploy and to deploy our project or our prototype we need an ability to remotely connect to the Raspberry Pi two lessons ago what I taught you is you can ssh in with putty and you can open up a terminal on your PC and you can connect to the pi through that terminal so anything that you could do on a terminal on the pi you can do remotely on a terminal then some of you guys enjoy the comfort and ease of use of having a graphical user interface so last week I showed you how to run a remote desktop for the Raspberry Pi so the Raspberry Pi desktop appears as a window on your PC and to be able to connect up through Ethernet or Wi-Fi so we've learned those two methods but in reality you really want something that's a little cleaner connection you don't want to run necessarily the overhead of a full remote desktop and also you would like to do things more than just fire up a window and so really what you would like to be able to do is run a program on the Raspberry Pi and then exchange data back and forth between your PC and your Raspberry Pi and this is kind of what you would call like this this is this would be like remote data exchange and I'm going to show you how to do that very simply and very robustly today does that sound good I hope it sounds good so let's see if we can come over here to this view okay and so what I will need you to do is fire up your Raspberry Pi and then after firing up your Raspberry Pi I need you to go ahead and open up thoni or whatever IDE you like to use use on the Raspberry Pi I like to use thawny and let's see if I can get this thing down here give me just a second I've got some multiple window business going on okay there it is and what I'm going to do is I'm going to call this program RPI server.py you can call yours whatever you want but the dot py is kind of important so this is the way it works this is we're going to initially be setting up a client server type relationship and then I'm going to show you in today's class and in the next couple of classes how to do that now today's class we're just going to be passing the data back and forth I'm going to show you how to send data from the PC to the Raspberry Pi and then how to receive data back from the Raspberry Pi to the PC so I'll show you what commands are needed to send the data both ways okay now once you know that mechanism of transferring and receiving the data then we want to set up together a little bit more of a formal client server type relationship now usually the Raspberry Pi is taking data monitoring it's doing something so in most cases your Raspberry Pi is going to be the server okay and your PC is going to be the client your PC is going to want something it's going to order it and then the Raspberry Pi is going to serve it up so I like to think it think of it kind of as a McDonald's the person that works at McDonald's behind the counter they are the server and me the customer I am the client I come in I say I would like a Big Mac the person working there gets the Big Mac and hands it to me they're the server I'm the client that's the way I like to look at it and as we're developing the software you want to kind of keep that model in mind and so we'll have a Python program on our PC that is sending commands or sending things that it wants to the Raspberry Pi and then the Raspberry Pi will send those things back and so you've got to think of that client server type relationship okay how do we do that we do that with something that's called a socket I'm going to plug into the socket at the server side with the Raspberry Pi and then the client is going to plug into the other end of that socket and they're going to talk to each other through the what through the socket so if we're going to do that we are going to need to import socket and I can see that you guys are not going to be able to see that so I need to come in here and do options and let's see we're going to come up to editor where would that be we need a bigger oh there it is okay let's make this about 26 yeah I think you can see that good sorry for that but I know it's a lot easier for you if I just take a second and uh and get this set up we also might want to put some delays in and so we're going to go ahead and say import time I just do that because usually I end up needing it okay now we're going to be sending packets of data back and forth so I need to set up a buffer size now your buffer size is just the space that you reserve for the message or the data the data doesn't have to be this big but I like to make it big enough that I have plenty of room to have a holding spot for the data that I'm going to send or that I'm going to receive so I'm going to go ahead and say buffer size like that buffer size is going to be equal to 1024 that would be a nice large buffer plenty for anything that we're going to be wanting to do now I'm going to have a message a message from server okay so I want to send something there will be something that goes from the server back to the client so let's just go ahead and set that up you can imagine in a real application this message from the server might be data information measurements any number of different things but I'm just trying to show you how to get a client to ask for a packet and then how to have the Raspberry Pi send that packet so we need a packet so it is going to be message from server is what we're going to say and the server is going to say howdy client because the server is talking to the client and happy to be your server I really like to make polite servers and clients I think computers should be polite to each other so I think howdy client happy to be your server is a pretty polite thing to say now I need to say what port what port that this server is going to be listening on Okay so there's all types of ports on your computer which Port is the Raspberry Pi going to be listening to so I will say server port server Port is going to be equal to 20 to 22. now you just don't want to use one that's already being used but you know 222 is a reasonable thing to you know to use okay now I have to put in what is my server IP well how do I learn that I come over here and I open up a terminal I'm gonna put in ifconfig I have config we've done this before so that should be familiar to you and you can see that my IP address is 192.168.88.41 you need to look and see what your IP address is you need to use your IP address not my IP address okay so let's get that out of the way and now I'm going to say server IP server IP like that and now you need to put these in quotes okay like that and so now the program knows what its own IP address is okay now I have the message that I was going to send but now I need to convert that into bytes so that it's something that will just slide right down that socket and so I need to create a bytes to send a bytes to send like that and that is going to be the message from server that we created we created that up here write the message from server okay but what I need to do to that is dot encode to make it the right byte format and I'm going to go ahead and just make it utf-8 you don't have to worry about that but I'm just giving it a good format and then as long as we're using the both the same format on both sides it will work so I have the message to send I have the byte to send and now what do I need to do I need to create the socket so I'm going to say the RPI socket the RPI socket is going to be equal to socket that is the Library dot socket which is the method and then uh it is going to be socket again the Library dot we need to tell it that it's a f underscore i n i t and that's just setting up some parameters there and then and then socket Dot sock underscore d g r a n okay so what I'm showing you how to do here I'm showing you that the easiest way to just send chunks of data back and forth over Ethernet or over your local network is UDP you could do rstp you could do HTML you could do RTP you could do all these different rmtp you could do all these different protocols but for little discrete individual chunks of data coming back and forth UDP I think is the best and the way you set up the UDP is with these parameters now if you wanted to do other methods you would use other parameters but it's the AF underscore init and it is the dot sock underscore d g r a m for UDP now if you want to do this this would be years you could spend understanding what all your different options are but I'm showing you one way that will work and it's simple to do okay now I need to take that RP I socket that I created and I need to bind it okay I need to bind it where do I bind it well I need to bind it to my server IP and I need to bind it to my server port okay so now what does this do when I talk to RPI socket I am going to be to be talking to my the socket at my IP address and at that Port so now I know where I should talk to because I have bound my RPI socket to the IP address and the port remember all of this we're doing on the server side now I have connected so what do I do I'm just going to say print uh server is up and listening okay so what this means is what you have to see is what does a server do the guy in McDonald's what does he do he sits and waits for a customer to place an order he's sitting there listening to see if someone is going to order something so what do we do we are going to sit and we're going to wait we are up and listening and how do we do that well we're going to look for a message and we're going to look for a message from an address so they're going to be two parameters that we're going to be listening to a message and an address where do we get it from RPI socket and then what do we do we receive from like that receive our EC v f r o m and where are we going to receive from we are going to wait for up to buffer size okay now how does it know where to be listening to the message it's listening on the RPI socket and the RPI socket has been bound to the server's IP at the server Port does that make sense okay so it's sitting there listening it will sit at this step and pause until when until it hears from a client does that make sense now once it hears from the client then what it is going to have is it is going to have a message that's the message that was sent and it's going to have the address that it came from because you've got to remember the Raspberry Pi might be sending here as the server and I might have 10 computers around the network each one who is placing orders okay so typically you have one server the guy behind the cash register and you have a lot of customers you have a lot of clients and so what this address is here what this address is it's the address of the client that is placing the order or making the request I hope that that makes sense so let's go ahead and we have the message but now we're going to have the we're going to we need to decode the message so I'm going to say message is equal to message dot the message is equal to message dot d code and then how do I want to decode it as UTF dash eight okay so I encode my message and I send it and then when I get the response back I have to decode it and I need to be talking in the same uh in the same format here okay so now let's just see what the message was so I'm going to print what I'm going to print the message does that make sense hope it does okay and then uh I'm also going to print let's see who is talking to us so I'm going to print the client address that's whoever just sent the message and then what would that be that would be address and then it is going to be zero all right and so the address comes in two parts what the IP address and the port address and so I'm just going to print the IP address of the client that sent the message by doing this right here and now I've gotten the message I've gotten the message from the client I need to respond so I'm going to say R I'm going to say RPI dot I mean RPI socket okay RPI socked it dot send to and then who do I send it to lights to address does that make sense I mean I'm sorry bytes to send and remember I set that up uh way up here the bytes descend that's the encoded message and then who do I send it to address all right now there's one thing that I want you to see here you notice how I'm not typing in the client's address I don't know what the client's address is but when the client makes a request he sends his address and then I send back to them so really the IP address that you need to know is you need to know the server IP address and the client ID address can be anything because whoever you are when you send the request then the response goes back to you and so now what have I done I have set up the socket I bind to the socket I listen to the socket when a message comes in I decode the message I print the message and then I send a response back does that make sense now this isn't going to work and why is this not going to work because there's not going to be a client that's doing anything but let's go ahead and run this and just see if okay I've got an error here already and this is quite annoying okay where did I make my error line eight uh AF init did I make a mistake there so socket is equal to socket i n e t like that okay AF and knit that's why I like to run the program as I go and I can find these simple little mistakes and now let's run it okay and again it is not happy and let's see uh line nine takes bind takes exactly one argument and two were given and so let's see ah you send this as a tuple and a tuple would be one parameter so you see I sent it as you know one thing and I said it as another thing but you have to send only one thing so we have to make this a tuple by putting another set of parentheses about it okay so now I'm sending one parameter that one parameter is a tuple that has two values now let's try it okay server is up and listening boom so that's good and I guess you can't see that because it is behind the title let me just show you that it's working okay so you see server server is up and listening okay so that is really really good news so on the Raspberry Pi side I have a server set up that server is sitting and listening for data so what do I need to do now I need to write something on the PC side so what I'm going to do is I'm going to switch over to the PC and here we are so this is my PC you see now I've got to change my keyboard so that I'm typing on the PC and what do I need to do I need to write the software for the client okay you now know how to set up a server you now need to know what you need to know how to set up a client and it's very very similar and you'll recognize a lot of this from what we did earlier and it is just as simple and there's only a few different little things so the first thing I need to do is I need to import socket again okay and then this time I'm need a message from a message from client okay and the message from client let's see the message from client is is going to be uh howdy server from your client okay and I write it this way just so you can kind of be thinking of the difference between a server and a client so so the communication is going to be initiated by the client and it's going to say hide howdy server from your client then what do I need I need to make that bytes to send I need to encode it and so that's easy equal that's going to be equal message from client message from client dot What DOT encode and then how are we going to encode it with you t f Dash 8 like that okay utf-8 now the client will automatically put his address his IP address when he makes the request but the client needs to know who to talk to so the client is going to need to have the server address the client is going to need to have the server address and then what do we know the server addresses we know that is 192.168.88.41 use your server address don't use my server address okay and this is the Raspberry Pi address this is not your PC address it is the server address and that's something people get confused about over on the client side they start trying to put the client IP address in no we're only dealing with one IP address that is the server IP address okay and then what else we need an equal sign there don't we the server address is equal to that and then that should be in our quotes that should be in our quotes and then what else do we need we also need our port and so we're just going to do the 22 22 again because I need to be talking on the same port that the Raspberry Pi is listening to so that's the server address [Music] yeah and I better spell this business right server address like that okay does that make sense now again we've got to tell it the buffer size and that's going to be 10 24. because that's what it was on the other side now I'm going to create a UDP client a UDP client and that UDP client is going to be equal to socket the Library dot socket the method okay and then what I'm going to do is sock it dot a f underscore and knit like we did on the other side and socket Dot sock underscore d g r a m so you can see the socket I'm creating on the client side is the same type as the socket that I am creating on the server side does that make sense I hope it does and they're both uh you know they're both kind of the like The Identical thing the server and the client you set the socket up the same and I hope that I hope that sort of is intuitive and makes uh and makes sense so now I've got it set up and now I need to send a command to the server why because the contact is initiated by the client so the client needs to do something so what the client is going to do is a u d p okay it is going to do a UDP client a UDP dot client okay or udpclient Dot send to and you've seen this send to before right because we did a send to on the other side as well but this time what are we going to send bytes to send that was our encoded message and where we're going to send it to the server address all right now what is the difference between the two send twos on the client side on the on the client side the server address is hardwired in as the Raspberry Pi's IP address is the server okay but when we come back over to the Raspberry Pi to the server side on the server side the IP address the address that we send to here the address that we send to we get here okay it's part of when the client sends a request the client sends his own IP address and port and then we get that and whoever it came from we send back to them does that make sense and that's why it never visibly really comes out what the client's IP address is and so pause the video and make sure that makes sense because that's kind of the important thing in the distinction between a client and a server okay so now we send to the server address we send it bytes to send which is the encoded message okay and then after we do that we wait for a response so let's work wait for data and then let's wait for address and so again the the server is going to send us the data and it's going to send its address back okay even though we already know it we've got to have a place to catch it and then what is that going to be it is going to be UDP client and then what do you remember do you remember what it is recv from and then we're going to receive what buffer size Now you kind of would think in here you would want to put like the address or something no the address is included already because UDP client UDP client was set up to UDP client was set up to talk to the server address and that was because we sent to it there it already knows where it should be listening to if that makes sense so here we send to the server address and then therefore it knows to be listening at that same address okay so now I have data and address and so then I'm going to convert data so I'll say data is equal to data dot d code and then a data is equal to data.d code and then how do we decode it we need to decode it as the UTF -8 like that okay so we data decode that looks good and then what I'm going to do is I'm going to print and I'm going to print data data from server and then what data and that should be the message okay and that message should probably be a string and then I'm also going to print I'm going to print uh server IP address and then what is that going to be that's going to be address in position 0 the first element of the address array and then I'm going to print server uh port server Port like that and then that would be address one close that okay so what happens the client is sitting and listening then the server I mean the server is sitting and listening then the client sends a request the server reads the request and then responds and then the client gets the response back and prints it does that make sense look at the code of these two programs and make sure that you understand what's happening so if we come back over here give me just a second uh if we come back over here I need to get a good view of this and it'll take me just a second to get my Windows management taken care of there okay I think you'll be able to see that so if I come over here I'm coming back to the Raspberry Pi and what you can see on the Raspberry Pi side the server is already running it's up it's running and it's what listening it is waiting for data so now what I will do is I will come back over to this side okay I will come back over to the PC side to the client side and now I'm going to run the program now when I run this program what is it going to do it's going to send a message and it's going to wait for a response and then it's going to print the response so if I say run module okay must be saved okay this is quite annoying uh we will say client UDP dot p y like that looks good save okay so we get an error uh so it says uh oh you can see I made a typo hopefully you guys were yelling at me when I do that but it was message to send uh is a message MSG like that okay let's try it again and this time it doesn't like line six it I not it hopefully you guys saw that so one two three four five six socket like that okay you guys have probably been screaming at me okay line seven UDP client is not defined what what oh L uppercase cell okay and guys prob part of the problem is in my studio here the window that I am programming on is way in my periphery vision and so I don't want to turn my head sideways and so I'm not getting a good view at it at least that's the excuse that's my story and I'm sticking to it okay now let's see run run module okay okay now this time what is interesting is it seemed like that we got not a crash but we didn't get anything back okay so it says uh okay guys as I'm looking at this things really look right I am looking here at the client side and I come over here and I look at the server side and the only thing I can really figure out is when my program crashed maybe I'd like left that socket locked onto or something like that and so what I think I'm going to do is I'm just going to come and re-run the server so I'm going to stop the server and I'm going to run the server all right now I'm going to come over to the client and then I'm going to run the client run module and boom do you see that okay so what do we have here we have a message data from the server howdy client happy to be your server and then it gives me the server IP address and the server Port which I already knew but this is you know I sent to that address it sent me its address back and the address it sent back is where I was talking to that's so that's just kind of a sanity check and then what we can do is we can come back over here to the server side and you can see that the server had the message down here the server got the message from the client which was Howdy server from your client and then a client address is is the the client addressed that was the address that came from the client and so that was very very good I'd put a little line in here to help with my debugging so I will take that back out okay and let's just try this one more time we will run the server all right now we are going to switch over to the client we're going to switch over to the client and now we are going to run the client again okay boom all right now let's go back and look at the server and the server is working correctly okay so this has been a little bit tedious but with these two you know kind of skeleton programs it shows how you can send a message to the server from the client and then you can respond back from the server to the client so let's look at how we might do this because you you've got to kind of get a little bit more action going than what we have here so let's come back and let's just write a simple program where the client would instruct the server to either increment or decrement a counter and that should be something that would show us a little bit more of having a little meaningful conversation going and so what we're going to do is we still need to come up here and do all the stuff that we did before we're bonding uh we're we're bonding the port and we're saying that the that the server is up and listening I need to go to my other I need to go to my other keyboard and so what we're going to do is see if we can send a command to increment encounter or or decrement a counter so we will be listening to listening for something and then I'm going to do a while true when is true true true is always true and then I'm still going to look for the message and I'm still going to do these things all right now what I'm going to do is I'm going to be looking for the message but I'm also going to have a counter so up here I will set the counter to zero so CNT is equal to zero all right now I think that I will come here and I print the message and then I print the stuff as I did before but now what I'm going to do is that should be tabbed over as well now what I'm going to do is I'm going to say if uh if message is equal equal to Inc [Music] for increment then what am I going to do then count is equal to count Plus 1. all right but what do I do If the message is equal equal [Music] decrement DEC [Music] in that case C and T is equal to CNT minus 1. like that all right now what do I want to send back now I don't want to send back I don't want to send back bytes to send because that is that message up there so what do I need to do I need to say uh message that I'm going to send back is going to be the string value of count okay and now I need to con I need to encode it so MSG is equal to MSG dot encode and then how am I going to encode it utf-8 like that all right and then what am I going to send I'm going to send message because it is encoded MSG like that okay let's just run this and see if this thing will run if we didn't do something crazy okay server is up in listening that's good now let's see if we can come in and do something smart over on the client side and so here similarly I am going to uh I'm going to come up here and I've got the socket set up and so what I'm going to do here is get the correct keyboard I'm on the client side I'm on the PC now and so again I'm going to say while while true one is true true is always true all right and then what I will do is I will say CMD is equal to input and I'm going to input and it'll say what do you want to do with counter Inc or deck that's my prompt that's my prompt and I'm sticking to it all right now what I want to do is I want to say CMD is equal to CMD dot encode in utf-8 like that and now what am I going to send I'm going to send command to the address right now I'm going to wait for a response back I'm going to get my response and then I'm going to decode it and then I'm going to print it all right so there's probably an error in here but let's just run this so right on this side I'm going to be telling it increment or decrement okay let's say I want to increment okay data from server is one okay that makes sense okay what do you want to do I want to increment two Shazam it's going up what do I want to do I want to increment boom it goes to three and let me make sure you can see that okay so python on the PC the client is sending a command to the Raspberry Pi the Raspberry Pi the server does what it is commanded and then serves the result and so let's uh let's come back down here okay this time let's ink again okay and then let's just say if I didn't do it as I was instructed and I typed in something like that it takes it but nothing happens now let's see if deck Works Dex goes to 3 that goes to two that goes to one okay this is what you guys wanted to see this is how to send data one way how to get the data back all right guys this is pretty darn exciting okay so I need to give you guys a homework assignment so I think I've shown you the core skeleton that you need to create a client server type of system I've sort of explained the client server to you I've shown you the three or four commands that you need in order to make it work properly and so this is what the homework assignment is let's do something a little bit more realistic now what I want you to do is to take the Raspberry Pi the server and I want you to connect it to the DHT 11 temperature humidity sensor from the Sun founder kit and then I want to I want you to be sending the data from the I want you to be sending the temperature and humidity data from the sensor back to the Python program the client that is running on your PC okay now you've got to think you need to do it as a client server type relationship so you really got to be thinking what should the Raspberry Pi really be doing and you've got to really be thinking about what should the PC the client really be doing and you've got to set this up thinking correctly thinking how thinking into terms of a client server relationship all right guys I hope this wasn't too tedious but you know really it's just you've got to know you know you've got you you've got to know that you've got to bind you've got to bind on the server side to that port and you've got to know these three or four a little bit tedious commands but once you know those things then the world really is your oyster and guys I hope you're really enjoying these lessons hey I've got a new camera I've got a new River View you love you guys let me know this is the New River cam again live here and you can see it's a little bit wider angle View and it shows my yard it shows my front yard and it shows the river I kind of like this because you see the greenery and you see a little bit more but then I also kind of like the classic cam which is looking at the same uh at the same area but it just zoomed in more and so this classic cam here is looking uh at that you can see the uh you can see the palm tree it's just kind of zoomed in on that little area there around the palm tree you guys let me know which camera you like better for my background okay so you've got your homework assignment your homework assignment is to go in and what you need to do is you need to have the Raspberry Pi taking temperature and humidity measurements which it sends to the uh to the PC and you've got to do it in a client server type relationship better figure out first of all who's the client who's the server and then you better set things up in a logical fashion okay guys Paul McWhorter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 14,534
Rating: undefined out of 5
Keywords:
Id: S7Yle8clJ30
Channel Id: undefined
Length: 44min 31sec (2671 seconds)
Published: Thu Dec 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.