Arduino Uno with WiFi - Arduino to Arduino Communication Temperature Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
please remember that the complete information for the class that you are about to view is at eli thecomputerguy.com not only do we have our videos there but we have part lists diagrams pictures and even complete code examples so if you are watching this video and you want more information please go to eli thecomputerguy.com welcome back as you know i am eli the computer guy and in today's class i'm going to be showing you a project where we're going to have one arduino uno wi-fi talk directly to another arduino uno wi-fi so we have done projects previously where we've had an arduino uno wi-fi that will post data up to a server we will use php and mysql database basically to parse and store the data that's been sent up to the server and then the server will then send commands down to another arduino uno with wi-fi uh and so that is great for a complicated system right if you need to store a lot of data values if you need to be able to do real complicated processing based off of those values it is definitely a great idea to have a server that your arduino devices are going to be communicating through but what if you have a simpler system or for whatever reason you don't want to have a server involved well one of the cool things that you can do with the arduino uno's with wi-fi is you can actually have them communicate directly with each other no server in between and then have actions happen based off of the information that is being sent back and forth so in today's project what i'm going to be showing you is we're going to do a very simple project we're going to have one arduino uno wi-fi that is going to be using an analog temperature sensor so kind of like that default uh project that we always do it's going to have an analog temperature sensor on it and then based off of what that temperature sensor is reading it will then send a command to a different arduino uno wi-fi which will then turn on either a green or red led based off of the command that it's received the important thing to understand here is that there is no server in between there's no php there's no python there's no node.js anything like that basically we're just simply using one arduino to communicate directly with a different arduino all you need is the wi-fi network to be able to connect to so before we dive deeper into the concepts that make make this particular project work and actually take a look at the code let's go over take a look at the demonstration so you can visualize what we are trying to accomplish then i'll explain to you what's happening and we'll take a look at the arduino code so here we are at my workbench um and this is the complete project so although it's in a small area here uh you can see that this is the complete project so the important thing to understand here is both of these arduinos are being powered on their own so they're not connected to any uh outside computer basically they are connecting to my wireless network and we have the sensor basically this is the sensor arduino this is sending commands over to the little dashboard arduino over here with the sensor arduino what i've done today is i've simply used a basic analog temperature sensor again this is an easy way just to do some testing with a dynamic arduino code and then over here all i've done is i have a green led and a red led plugged into digital pins nine and eight and uh based on the commands that they that this arduino will receive from the sensor arduino the color of the led will change so since this is our temperature project we now see that the color of the led is currently green so basically it's saying that right now we have a good temperature if i simply you know hold on to the analog temperature sensor here after a couple of seconds the temperature will go up and now we can see that the red led has turned on so the green led has turned off the red led has turned on i want to take my finger off of the temperature sensor to let it cool down again uh in a couple of seconds the green led should come on and basically that way you can see that this this is pulling in information from the analog temperature sensor depending on what the value of that information is it is then sending a command basically to this little dashboard arduino and now we can see it's a green led so again this is where you can have a sensor depends on what sensor you want again all this is a sensor so this could be a humidity sensor this could be a water sensor this could be any number of sensors uh basically the information is coming in this arduino is processing the information uh based off of if else statements it's then sending a value over here and basically that is then lighting up either the green or the red or you can have any number of other actions happen so to make this project work today we're basically going through and we're cannibalizing code that we've already used in previous projects so in previous projects we have had arduinos who collect information from sensors and then send that information in a post up to a php server and we've also had projects where that php server then sends commands down to the arduino essentially what we're going through is we are cannibalizing a component of the code we have used before and so now uh instead of having the the sensor arduino sending information or values to a server we are now sending those values directly to the other arduino and those that other arduino is performing actions based off of that so in order to understand how the sensor arduino works essentially what we're going to be doing is we're more or less going to be using the get method of posting data like we would use in many ways if we were posting to a server so normally when you're using git to post to a server you would get you you would give a variable name and the value for the variable uh today basically we are stripping that out and we are simply just giving a value so only a single value is going to be sent over uh to the receiving arduino so essentially we're not we're not giving a variable name and a value all we're going to be doing is sending a value and so that value today is either going to be red or green so all of the processing that will be happening today uh basically will be happening on the sensor arduino so what's going to happen is it's going to read the temperature from the analog temperature sensor that we have and then based off of the value that that temperature sensor is giving you know whether it's 60 or whether it's 80 basically it'll determine is it too high or is it where it should be if it's too high it will then send the value of red so basically we're not do we're not doing that variable equals red all we're doing is we're sending red so basically red will get sent here and then when this picks up red then it'll go okay then then we're going to turn the red led light on if the temperature is below a certain level this will send green it will just send a text green basically this will grab that green out out of what's being sent to it and then it will turn the green led on so basically all that processing for the temperature value coming in and then what to do about the temperature value that's actually happening on the sensor arduino now over here basically the code that we're going to be using that we're going to be cannibalizing today is where we have sent commands to the arduino in the past and essentially what we're going to be doing is we're going to be reading the string of the url path to this arduino so when you access an arduino right so this has an ip address this has an ip address of 10.0.1.16 and then you can have a forward slash and then you can have other stuff you know after that forward slash and what you can do is you can have your arduino parse or be able to read what is past that forward slash and determine what should happen so essentially all that's going to be happening today is this is going to go to 10.0.1.16 forward slash red this is going to read and if there is red in that url it will turn on the red led or if this is green 10.0.1.16 forward slash green this will be able to read the value passed to it that there is green there um and then it will turn the green led on so those that's basically how this project works so with that let's go over and take a look at the code itself so here we are at my demonstration system i have the arduino ide open and we have two different sketches here so this is the arduino to arduino sensor so this has the analog temperature sensor connected to it and this is the arduino to arduino receiver this is what will be receiving the values sent to it uh it is important to understand that in the basic setup there are differences so there is some difference between what how the sensor will be initially configured versus how the receiver will be initially configured uh so the first thing for both of them is we're going to do the include the wi-fi nine the library so this is an arduino uno uh with wi-fi version two so we need to uh basically have that light include this library so all of this stuff works uh the next thing we're going to do is we're going to define the sensor pen is going to be a5 so this is the analog port that the the analog temperature sensor is going to be connected to uh past that we have the information that's required for connecting to the wi-fi network so the ssid is basement the password is zero one two three four one two three four five six seven eight nine so whatever your ssid and password is what you'll put here we create this status variable simply for the while loop while you're trying to connect to the wi-fi network basically just a status there nothing much to worry about the next thing that we're going to do is we also we're also going to be creating a variable for server so the server is where we are going to be sending our values when we try to connect uh to a system and to send the values this is what the variable is and so for this it's going to be 10.0.1.16. so 10.0.1.16 is the ip address that i'm going to be statically assigning within the receiver so it's important with this part it doesn't matter what the ip address scheme is could be 192.168.1 it could be 10.10 or 10 whatever whatever your scheme is basically here is where you put the ip address of whatever it is that you're trying to connect to so it's important to understand there's no forward slash there's no additional information here this is simply uh the ip address then we're going to go down we're simply going to create the client so this is for the wi-fi client and then we're going to go into the initial setup now one of the things i've started to do in the initial setup is just to visually show me when my little device has actually connected to the wi-fi network i i now have the built-in led light turn on so one of my problems is when i disconnect my little arduino devices from a computer and i'm trying to connect them to the wi-fi network how do i know when they're actually connected basically what i do now is i simply turn on the built-in led pin after the while loop right so this is the standard while loop of connecting to the wi-fi network so essentially what i do here is i set the pin mode and then after after the while loop is finished basically after the device has connected and the arduino has connected the wifi network i then turn that little led light on just as a way to tell me that it's on so something to think about there uh serial number again 9600 like normal this is the while loop to connect to the wi-fi network it's the same code we always use uh then past that we come down to the serial.print so we'll serial.print out ssid and what the ssid is what the ip addresses so on and so forth so this is what you would be reading in the serial monitor same stuff like we always do then we're going to get down into the loop itself and again frankly most of this loop is the same kind of stuff you've seen before again that's one of the things you have to understand about dealing with arduino is once you start building code that works the way you want it to one of the great things is is you can start cannibalizing old code right once you once you've created code that you like then you just copy and paste a bunch of of components out of different projects that you've created right you know customize it a little bit so it all comes together and that's that's one of the nice things so here again this is the same code that we use for our temperature sensor normally so this is how we actually get the value from the analog temperature sensor and turn it into a value for temperature fahrenheit so that's here basically so we're just getting that value then we're going to serial print so out to the serial monitor for troubleshooting purposes temp f and then whatever the temperature fahrenheit is past that this is where we actually come down and we start sending values to the other arduino that we have so what we have here is we have an if statement if temperature f is greater than 80. so if the temperature is above 80 degrees right do everything in here we then do another if so basically the important thing is if we can connect to the server at port 80. so when we say server server is this variable up here so if we can connect to 10.0.1.16 at poor 80 then um what we're going to do is we're going to uh serial print line so on the serial print serial monitor we're going to print connected just so we know what's happening then to client so client this is where we're printing out to the other arduino the other arduino the other server basically whatever is here at 10.0.1.16. so there we're going to simply print line and get so we're going to be using the get method but here all we're going to be doing is we're going to say forward slash red so you have to have the forward slash so basically this this separates between the ip address and whatever whatever other information you're sending so you have to have the forward slash and then here all we're going to be doing is red so again we're not doing color equals red we're not doing temperature equals red so a normal get a normal get you would have the variable name equals value for the variable we're not really doing that we're just sending a value we're just sending the value so basically we're just sending red right http 1.0 just to say what what type of information it is then we're doing client print line basically blank just to say okay there you go that's all of your information so if the temperature is above 80 we are simply going to be sending red basically using the get method to the other arduino else if if temperature f is less than 80 basically the same code is up here connect to the server at port 80 say that it's connected here we're going to be again be using the get method again make sure you do the forward slash and then we're simply going to be sending green so send green over right http 1.0 and then print line uh then else now again there shouldn't be any reason for any else again it should be either under under 80 or above 80 but you know when you're writing code who the hell knows what's going to happen again so for a troubleshooting procedure basically this will simply do pre serial dot print line problem and then you can try to figure out what is going on uh past that you know if client is still connected then client stop and then simply delay for a thousand milliseconds or for one second so this is the code that is required to send from one arduino to the other the important thing here is basically we're using that get method like we have used in the past but now instead of sending a variable with a value essentially all we're doing is sending a value so now let's go over and actually take a look at the receiver code so if we take a look at the receiver code we'll notice again basically we're just simply cannibalizing a code that we've used in the past so we're going to include the wi-fi and in a library then we're going to define digital pins uh green led is going to be pin 8 red led is going to be pin 9 same thing like when you normally attach leds uh then we're going what we're going to do here is we're going to do the ssid as basement the password here 0 one two three four five six seven eight nine like normal but the thing now is we have to assign a static ip address again remember because one device has to communicate with the other device in order to have that communication be possible it has to know what the hell it's communicating with so we're going to assign the static ip address do remember when you're assigning static ip addresses in the arduino world you separate the numbers with commas not periods that's a easy way to fat finger things and then screw things up then the status is going to be uh basically idle like before and then here we're going to be setting up the server so this the wi-fi server is going to be at port 80. so if some reason you want to modify what port that is you could do that that here but basically we're saying wi-fi server port 80. uh the next thing that we're going to do is we're going to create a variable string and then we're going to have the the the read string variable so it's going to be a string variable that's going to be called read string and so essentially what we're going to be doing is we're going to be parsing this to see whether it says red or whether it says green and then doing something based off of that past here we're then going to do the pin modes like we normally do so this is the pin mode for that built-in led then pin node for the green led pin mode for the red led serial.begin like normal uh then we're going to have that while loop as it tries to connect to the wi-fi network once it has connected to the wi-fi network we're then going to turn on the built-in led so that we know that that has happened we're going to wi-fi config so basically we're going to feed a configuration for the ip address so whatever ip address you have put here that is now going to be the ip address for this particular device then we're going to do server.begin so we're actually going to begin the server service on the arduino serial.print it will print out the ssid and the ip address just so you have that on the serial monitor for troubleshooting purposes then we're going to come down here and here's the mess of how we are going to actually parse the values that will be sent to uh the arduino uh so again uh most of this is copy and paste so everything here uh you should have seen this before and you basically copy and pasting this from other projects where we have sent values to an arduino so wi-fi client equal server available if client basically run a serial dot print line so we'll print out to the serial monitor new client while the client is connected if the client is available and this is basically where we go through and we parse the string that is being sent to uh to the the device so basically 10.0.1.16 forward slash something that's how this all gets turned into that read string variable and then what we're going to come down here this is where all the interesting stuff is so if read string and then there is a function of index of so basically you have read string so what this is going to be this is going to be 10.0.1.16 forward slash red forward slash green forward slash billy bow bob right so basically the index of the index of function an arduino looks for whatever you get if give it in the string so basically it's going to say hey in this string that you've created 10.1.6 10.0.1.16 forward slash red is green there right or is red there so if it's forward slash green it'll say green is there and so what we're going to say is we're going to do digital right green led high so that turns a green led on a digital right red led low right and then we're going to delay for one one microsecond else if so basically we're going to say else if read string index of so in that string if there is red basically we're saying greater than zero so if there is red then turn the green led off and turn the red led on again delay for for one millisecond then past that we're going to have an else so what if somebody sends a value other than green or red at this point we're going to turn both the green led and the red led on to high then we're going to serial print a problem so we're going to simply print out the text problem and then we're going to serial.print line whatever the value of read string is so you can see what somebody is sending to it then we're going to blank out read strings so once the basically what's going to happen is here this is this is going to assign the values to read string so the url that's being sent this is what actually creates the read string so you can read through it then we're going to parse is there green is there red or is there something else then we're going to blank it out so once that's been used we need to blank it out so there's nothing in there we're going to delay for one millisecond then we're going to stop the client then we're going to simply serial.printlineout client disconnected and then we have a we close a whole crap ton of different brackets here so basically this is how the code works again the read string it's going to be looking here for red or green you could put whatever you want you could be put in green yellow blue again notice this is an else if so you can have a numerous different else if statements here so you could have else if else if else if so again like for colors let's say you wanted red and you wanted green and you wanted yellow and you wanted blue and you wanted purple and you wanted blah blah blah right you could just simply do additional else ifs with looking for different text that is going to be sent to the arduino device now just so this kind of hits home let me show you again how how the command can simply be sent with a url and that might solidify how this project works for you a little bit better okay so here we are back at the project again so again this is the receiver arduino this is connected to the leds and this is the sensor arduino so basically what this is doing right now is a temperature down here in my my lab environment is below 80 degrees so this keeps sending green green green green if i grab the temperature sensor so i artificially spike the temperature up once it hits 80. what we'll see happen is the red led will turn on right so this is now saying red red red red and then if i take my finger off after a certain amount of time the temperature will drop and then it will start saying green green green green again but it is important to understand how these commands are being sent especially if you're worried about hacking right everybody's worried about hacking so to understand how the system can get hacked let me simply uh unplug this so basically at this point this is no longer sending any values uh to the receiver and you can also see that this might be a problem for you so if the if the sensor is disconnected from the power or for whatever reason stops sending values to to the receiver arduino one of the problems with this project that you'll see is you'll notice that this green led doesn't change so this is based off of the last value received if this dies if this burns up in a fire or something else this will stay green so that that's one of the things to be thinking about with problems for this particular project now the other thing is again to understand how you can actually send commands to to this little receiver arduino so remember we're basically just sending the commands uh using an ip address so 10 10 10. was it 10.0.1.16 right so this is the ip address of this receiver arduino forward slash and then let's say we type red right so then we do that and what we'll notice here is now that value has been sent to the arduino again if we want it to be green we could simply type in green and now we see it's green remember when we did the the if else if else statement we also said if you receive anything other than red or green so let's say we just sent billy bob send billy bub we hit enter and now we see that both green and red are going off so that's basically how this is working so i'm showing you how this works using a web browser but essentially that's what we're doing is we're simply trying to connect to that ip address with the forward slash again red green whatever whatever value and then this particular arduino is doing something based off of that so that could be a vulnerability with a system realistically the real big vulnerability here is again that the arduino will simply show you whatever the last value it received is and so if the sensor arduino loses power for some reason again this could be burning up in a fire you could have a bonfire in your server room but the last value that this received was green so everything looks good so something to think about there so there you go now you know how to do arduino to arduino communication over your wi-fi network and you don't need a server in between you don't need to worry about php or python or node.js you don't need to worry about mysql databases or linux permission or any of that kind of thing basically you simply have your one arduino communicating directly with another arduino again it's important to understand you could create a very complex system you could have 20 or 30 arduino uno's with wi-fi all communicating back with any number of other arduinos you could come up with some complex system so basically you could come up with a system where you have the sensor so you have the sensor arduinos communicating with some kind of uh let's say call it a server arduino and then that server arduino could parse values and then send commands over to a different arduino to turn on a fan or turn on a pump something like that so basically with this very simple level of communication you could actually create a very complex infrastructure is it how i would create a complex infrastructure i know probably not i would probably use a server with some kind of backend language that would most likely be a hell of a lot easier way of doing things but it's important that you can create a bit of an infrastructure this way the important things to think about with this particular project is again from an ip address standpoint your sensors basically anything sending information can have a dhcp basically a dynamically assigned ip address because nobody's talking to it it's talking to somebody else it is important to understand though is any any of the server basically the receiver arduinos they do have to have a static ip address because those sensors have to know who the hell to talk to past that again from a security standpoint from the rest of this you do have to be thinking about how you write the code what do you want to have happen if if the receiver hasn't received information in a minute or two do you want some type of alert to go off do you want to build in some kind of security some kind of password type system so not only is red and green sent but maybe some kind of password some kind of authentication scheme so somebody can't just you know get on a web browser and just start typing in commands and start dorking with your system again how you build that type of thing out you know that's something that you have to think about um and just again consider you know that's one of the things like with security one of the problems we run into with things like security is you can build all of this stuff relatively simply and it'll work forever right with these little arduinos you plug them in as long as they've got power that work for the next thousand years so there's a one of the problems you run into in the real world is there's what will work and then what is secure and what has a level of security that you care about right you know if you're if you're sending information from let's say your garden so let's say you have a garden moisture sensor and you're sending information just into a dashboard in your kitchen you may not really worry about security that much oh no somebody hacked my moisture sensor right that may not matter so you're not worried about security on the other hand if it's temperature or environmental sensors in your server room or something else at that point you may worry about it getting hacked or basically getting uh erroneous commands in and therefore you may want to build some more complicated you know authentication and that kind of stuff but that's things to worry about for other projects so with this essentially all we're doing is we're more or less using the get method as we've used in other projects to be able to send information using a url basically the receiver arduino reads the url that it receives based off of what's in there red green billy bob whatever else it then has its own little if else statements and then does things such as turn on the green or red led that's basically how the project works so as always i enjoy doing this class i look forward to seeing the next one if you like the content that i create please think about going to eli the computerguy.com and becoming a member or donating please understand that all the educational videos are in front of the paywall that includes the videos that includes the notes the diagrams and the code example all of that is freely available and in front of the paywall but if you want to watch opinion videos or if you want to be able to comment you do need to become a member membership is five dollars a month or sixty dollars a year and gives you access to those opinion videos and the ability to comment if you don't want to become a member you just want to give a one-time donation there is also a donate button where you can do that please understand in order to provide the education that i am it does cost money servers cost money equipment costs money travel costs money all of these things cost a reasonable amount of money and the fact of the matter is is youtube's advertising program no longer supports creators the way that it used to so if you want to these classes to continue to stick around and you find them to be valuable please think about either becoming a monthly member or donating a few dollars for this project
Info
Channel: Eli the Computer Guy
Views: 12,552
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: 0wWvZtWypo8
Channel Id: undefined
Length: 30min 22sec (1822 seconds)
Published: Wed May 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.