hey guys welcome back in the last few videos I was showing you how to do TCP data transmission a trivial example but since we’re trying to catch a cookie thief and we need to monitor our temperature and let the people in the house know when the temp temperature in the kitchen isn’t right we need to take temperature readings and turn on an LED a practical example of using this and so that’s what I’m going to show you how to do here so the equipment you’re going to need to do this is of course two raspberry PI’s one here and I have one here and they’re both connected to the Ethernet through Wi-Fi this here above this imaginary line is the circuit that detects temperature you’ll recognize this chip in this wiring configuration from the analog sensor tutorial that I made earlier and this here is a TMP 36 temperature sensor that will be read to detect what the temperature is in this like fake kitchen setup over here lower is another circuit completely isolated that is turning on an LED and I did a tutorial on how you can do that earlier as well so the idea of this project is is when this temperature sensor detects that it’s too hot this LED comes on in blinks for a few seconds before turning off that’s that’s the idea that’s the project and so we’re going to set that up now if you’ve been following along there going to be some there are going to be some scripts that have already been written and we’re going to be using those scripts in the interest of speed and kind of like taking them apart and modifying them a bit and so we saved all our scripts in the cookie folder so right now I’m on my Play pi this is the PI that is connected to my sensor in the first one we’re gonna open up is cookie client now because I said that I would say I baked all of these files available online I’m just going to save over cookie client right now as cookie client – so this is like our new and improved version so that if you want to revert back to the trivial example the original cookie client file is still there for you to access so we are already setting it up with a host and a port and all that one additional package we’re going to have to or is or sorry I should say one more function we’re gonna have to import is from time we’re going to import sleep because we’re going to need that as well to do this so we’re gonna have to do quite a bit of changing here so we have our host and our port that are specified and then I want to define a function called setup socket and you can probably guess what setup socket does it well sets up the socket so the these are the lines here where we set up the socket so we’re going to set up our socket and then we’re going to connect to it just like before and then out of this we are going to return s which is our socket then we’re gonna want to create a new function and that new function is called send receive so this is where we send our data and we receive our data and into it we want to pass the socket and the message that we want to send so I’m just gonna rewrite this what we want to say is here let’s get rid of this wild true stem and we’re gonna delete that later but with oh my goodness in send receive we want to take our message that was passed into it and through the socket that we also have passed into it we want to encode that message and send it out once we send it out we’re going to get a reply back this might look familiar and our reply back is going to be receiving and I’m gonna have a buffer of 1024 here and you can change yours to whatever you feel is appropriate and then dummy statements just saying we have received a reply and then we’re going to because we want to shut down the connection when we’re not using it this allows other PI’s to connect or whatever it doesn’t take up resources and if the program were to crash doesn’t crash the server so we’re going to want to disconnect so we’re gonna send closing message and remember on the other script we already had a command written called exit and that exit allowed the client to disconnect safely so we’re going to send that message and that message is exit and then finally we are going to close our socket because we no longer need it and then we are going to get a we have our reply right so we have a reply and we want to decode our reply and we want to decode it and form UTF or not WTF

utf-8 and then we’re going to return out of this are decoded reply so there we go excellent so now we have a function that we can call on that will quickly send out the message get back the receiving message send out the clothes message so that we we cut the cut the line so to speak we close this close the socket and then it passes back the reply finally we’re gonna create one last function here called transmit and transmit is going to be the function that is called on when we import this as a module cookie client two is going to be imported as a module and the message here is whatever we’re passing into the module so that like this is what’s being called on so now this module is just like a trivial send receive kind of thing or it opens it up sends it out receives back and then closes down that’s the idea so I have to get our socket and socket is going to be equal to setting up our socket and then we’re gonna have a response that’s given back out of send/receive and that response is equal to receive the pass in our socket and we pass in the message so now we have a response and then we’re going to return our response and that’s it so I’m gonna delete everything that’s left over from the old cookie client and there we have it so assuming that there are no typos here we’re going to and I’m sure I’ll find out when I start launching this program and it starts crashing on me we now have this file open called cookie client to that we can call on the transmit function to send the data out receive the data and then cut the connection the next thing we’re going to want to do is we’re going to want to write a script for our analog sensor that we can also call on and this will be called on from our main function so I’m going to open up the cookie analog here and because I use this in a previous tutorial I’m going to again save over it and write it as cookie analog too so that you guys can download it and download the original cookie analog so I’m going to save it as cookie analog to now there’s a few things here we can get rid of because we’re only worried about temperature now and we’re not worried about anything else like humidity or or light or whatever else I had connected to this we don’t need to sleep we do need to open up the SPI bus what sensor is where well this particular let me just kill my phone here this particular configuration that I have here I have the temp Cannell set to zero we no longer need our light channel because we don’t have a light sensor and we’re no longer going to be needing to sleep so we can get rid of our sleep time here we’re going to need to continue having our get reading function we don’t really need convert voltage anymore because you don’t care about that in this particular setup but you might convert temp will definitely need because we need to return that temperature so what we can do now is instead of having this like while true loop we can find a new function called get temp and what get temp will be is we’ll get a value called temp data and this is a lot like before temp data is going to be equal to get reading and then we toss in temp channel just like before so now we have our temp data the next thing we’re going to need is our temperature because that’s what we’re concerned about and our count and our temperature is calling on the convert temp which is passing in temp data finally we’re going to return out of this temperature so just leading the everything that was in the wall true loop we no longer need that that’s no longer relevant so if you want this system to work on your voltage you can change it so that you have voltage but because I’m concerned about temperature this is all we need for our new analog sensor from Python module so that’s called cookie analog to now to be able to use these two modules we’re going to have to have our main function call on them so I’m going to open up what was cookie main now because in another tutorial I showed you what cookie main is again I’m going to open this up and I’m gonna save it over as cookie main to so that we are you know we’re moving one step at a time and you can look at the old files all right now that we have our new cookie main – saved the next thing we’re gonna have to do of course is set up

some new imports and these new or new packages to import I should say so we’re going to first have to import sleep from time import sleep alright right okay we’re also gonna have to import from we what did we call it we called it a cookie client cookie client – yes we called a cookie client – we’re going to import transmit so that was that function that we made and from cookie analog – we’re going to import what did we call it we called it get ten so now we have all this stuff here that we don’t want to use in this particular tutorial we’ll put it all together in another video we’re just interested in getting the communication for lighting up this LED happening so we’re going to want to define a new function here and our new function is going to be called temp monitor because we are monitoring our temperature so let’s get some dummy statements in here just so that we know that it’s working we are about to take reading and so if we want to take a reading we want to save our tenth value is something on our tenth value is going to be the float of calling on that function get temp and then we’re gonna want to print again a dummy statement our temp is and then we can say plus string temp so now we we’ve taken our temperature reading and now we can make a decision and I’m going to make this decision in this function where we say if the temp is greater than let’s say float 22 so if it’s about 22 degrees Celsius we are going to say we’re going to create a message and this is the message that we want to transmit so our message is equal to LED underscore on so turn on the LED is the message we’re going to print transmitting data and then we’re going to say our response which is what we get back out of the program is going to be whatever transmit data or transmit our message returns to us and then we’re going to print that we’re not going to print message we’re going to print response right okay so what we’re doing is this function gets a reading so it calls on get temp gets that read and compares that reading against 22 if it’s above 22 we then send out the message to the other PI and say hey yo turn on your LED scrolling down here to our while true because we are not concerned with the overall performance of our program just yet I’m going to comment out all this stuff we wrote in the past and instead what I’m going to do is I’m going to take a reading from our temperature sensor so we’re gonna get temp monitor and then we’re just going to sleep because we don’t want to go crazy we’re gonna sleep for sleep time length and I didn’t specify what sleep time was yet so we’re gonna have to add that variable and I’m gonna add that at the top and so our sleep time is equal to 3 seconds all right cool now I’m gonna get that save so there we have it for the play PI side and this is the side that reads the temperature now what we need to do is get it running on the other Raspberry Pi the RO PI we need to get our server set up and we need to write a program for it to blink an LED so moving over to our row pi which is this is the Raspberry Pi connected to the LED we’re going to open up our desktop going to open up our cookie folder and we’re gonna open up cookie server dot pi and this is the file that we created in our last video but actually hang on a second before we do that let’s create a new one because I don’t want to use the cookie server to turn on the LED I’m going to create a new file that will turn on the LED and this will be a module that we import so I’m going to save this as cookie LED dot pi I don’t know if I’ve used this file name somewhere else but I’m going to call it cookie LED dot pi and this is going to be the script that

turns on and off the LED so just like in the introduction video that I made earlier we’re gonna have to import the GPIO as GPIO and then because I want to keep the LED on for a certain amount of time before it turns off we’re gonna from time import sleep I could use some of right now we’re gonna specify our sleep time and I’m going to say 5 seconds so that we leave the OED on for 5 seconds and our LED pin is equal to 40 as you could as you saw when I wired it up yours may vary we’re then going to define a function called LED setup we are in the LED set up we’re going to GPIO dot set mode and we’re gonna say GPIO dot board so it knows what configuration of pins we’re going to be using and then we’re going to GPIO dot setup and gonna pass that the LED lowercase led pin and we’re going to pass it we’re going to specify that it is a output so now we’ve set up our LED and then we’re going to want to specify a new function here called LED on and in this one you can probably guess what’s going to happen we’re going to say set our GPIO output for the LED pin to be true that is make it on and then we’re going to sleep for the amount of time that we want to leave it running which is our sleep time and then finally because we are concerned about safety we are going to clean up our GPIO board so our header so that all the pins are neutralized and go back to a safe state versus putting it low so where it’s kind of like we’re turning it off versus we’re just temporarily or indefinitely keeping it in a low position which is something we don’t want to do and then we’re going to create a function called call LED and it’s just going to run the LED set up and then it’s gonna run LED on and that’s it so the idea is we can call LED and then it runs through the setup and then we have this here working so great so now that we have that okay guys just a quick correction to my cookie LED one we’re not actually wired up to pin 40 we’re wired up to in twelve I knew I’d make a typo somewhere and I think this is because of my script having 40 on and I didn’t even think about it so we want to make that correction let’s just minimize this to avoid confusion and in our cookie server file I’m going to save this as cookie server 2 so that you can access the files because I’m going to be over writing some stuff now if we’re gonna want to call the led we’re going to have to from what did I call it LED cookie led from cookie LED we are going to import call LED cookie LED we’re gonna call call LED all right excellent now that we’re on top of this so this hat we don’t have to make too many changes here but what we do have to do is add in a command here in our statements where we say else if our command is equal to LED underscore on well you can probably guess what we need to do we need to call our LED and then we want our reply to be equal to LED was on right so that’s what we need to change relatively small changes because it’s the same server and because we wrote a completely different script we can just call on it right nuts that’s the reason why I chose to set it up like this so we’re going to be importing the Col LED function from cookie LED module that we wrote on the server side which will turn on the LED when we need to do that now theoretically we have a server side that’s set up properly and we have a client side that’s set up properly so the first thing we need to do this is our play PI so on earth row PI now it is time for the moment of truth we have our watt sensors wired up and we have our script files written so now what we want to do is we want to call using our sudo command because we’re going to be accessing the GPIO with cookie LED we need to use sudo python 3 to call on cookie server 2 dot hi so this will set up the server and there we have it the circuit socket is created and the bind is complete and still didn’t fix that oh my goodness and on the play PI we’re going to want to call cookie main so Python 3 cookie main 2 dot pi so now this is going to start reading this sensor and it was somewhere like 22 or 23 I can’t remember why they specified is when we

flip so here when I touch the sensor the temperature should go up there we go and there we go transmitting data and the LED comes on waits for 5 seconds goes off we then take a new reading 25.8 transmitting the data LED comes on isn’t that cool guys come on that’s so cool you know guys that is cool so now we are communicating over the Ethernet over Wi-Fi to raspberry PI’s where one is taking sensor readings and the others reacting to those sensor readings somehow and this is non-trivial this of course can be extended to doing it over the internet if you really want it to be cool for example I don’t know I don’t know what you would do over the Internet but yeah you could off of your smart phone you could send a message to your PI through socket transmission to say open up the front gate at your house or turn on the lights or turn on the air-conditioning or something like that and so and that’s all there is to it and now we see our temperature reading has dropped back down to below whatever that float value was that I specified and the LED is not coming on so I hope this kind of Illustrated a practical example of how to use socket transmission and to raspberry PI’s to actually accomplish something that’s pretty cool in my next video I’m going to show you how you can transfer a file to another Raspberry Pi through Python automatically because if we’re gonna catch our cookie thief and our cookie thief sees our little breadboard system there on the counter he might steal it right and then the pictures are stolen and then we have a problem so we want to be able to automatically send a picture over the Wi-Fi over Ethernet to another Raspberry Pi to act as a safety repository and I’ll see you in that video I hope you have enjoyed the series so far thanks for watching you