Home Automation at Home Part 2: Relays and Lights

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to this the second episode of home automation at home where I show you guys how to build your own home automation systems at a fraction of the cost of the professional units out there in the last episode I showed you guys how to setup a Raspberry Pi and an esp8266 to utilize MQTT to control your devices in today's episode I'm going to show you how to take two esp8266 --is and use them to control the lights in your house one of the modules will have a button or a switch on it to toggle the lights on or off and the other module will have a relay connected to it to actually control the lights in the house now this is going to be building off of last episode because the two ESP modules will be using the MQTT protocol to communicate with each other now this project isn't limited to just a single lamp you can actually have as many lamp modules as you want be controlled by our single switch module so this is going to be a really interesting and useful tutorial for automating the lights in your house we will also be taking a look at a Python script that will allow us to log when we turn our lights on and off and how long they've actually been on for so let's take a look at the hardware for this project so as you can see we still have the Raspberry Pi that we used last time and we haven't modified anything at all it's still just running the MQTT server and now we have not one but two esp8266 modules now I have these both on the single breadboard but I want you to note that they are not actually connected to each other through any sort of data link except through the raspberry PI's mqtt broker so on the right here I have a single ESP module with a button on it and on the left I have another ESP module and this is using an opto couple to control a relay module which is connected to my lamp in a previous video I showed you guys how to wire up one of these relay modules using an opto isolator to control anything in your house so make sure to check that video out as well on the breadboard here we also have the quick and dirty esp8266 power module that I also have a video showing how to build so if you're interested in that check that video out as well it's a really interesting and quick build and it will be great for powering all of these home automation projects that I show you in this series so now let's go ahead and take a look at the code that makes all of this run alright guys so here we are with the Arduino software and because we have two separate ESP modules doing two separate functions we actually have separate programs one for each module so here's the light node now this is the code that we actually use on the module connected to the relay to our light this is more or less the exact same code from the last video so if you're wondering how this works please go and check out part 1 of this series it goes through basically all of this code and how it works and what it's doing there are a couple of changes that I've made so I've renamed my light topic string 2 / house / light 1 which is a change from slash test / light 1 and I've actually added a separate string here for my confirmed topic which is house / light 1 confirm so that's more or less all that added to the light node here it's basically the exact same program again go check out part 1 of this series to learn how this works and now we'll take a look at the button code alright so here we are with the code that will be on the controlling ESP module that is the one that has the button or toggle switch on it so in terms of setup this code is very similar to the code that runs the light module we include our pub/sub client and our esp8266 Wi-Fi library we're adding the bounce to library and this will allow us to detect pushes on our button that we have wired up and then again the parts that you need to fill out here are you or mqtt server IP address so that's generally going to be the IP address of your Raspberry Pi your wife eyes SSID name and your Wi-Fi password so here we define the pin that the button will be connected to so this is on the GPIO pin of the ESP module and then we have our light topic again but instead of subscribing to this light topic will actually be publishing to this topic and the light module will receive that message and interpret it to turn the lights on or off so we'll be using the bounce library to detect presses on our button on the controlling module so here we actually create an instance of the bounce class that we'll get to setting up in just a moment again we create our Wi-Fi client and we create our pub/sub client and this is our MQTT client and we call it client and this is what we'll be using to actually publish messages to our MQTT server so in setup it's very similar to code that we've seen in the past I set my button pin as an input and I attached it to my bounce object and give it an interval of 5 milliseconds so that's the D bounce time I start up the serial line for debugging and I initialize the Wi-Fi subsystem and attempt a connection to the Wi-Fi network now if you curious how this code works check out part 1 of this series and we go through how all of the setup works for connecting to a Wi-Fi network on the ESP module inside of loop again it's very simple just like we've been keeping it we make sure that we're connected to the network and if not we attempt to reconnection we run our MQTT loop and we have this one function here that I've created called check button and that's the majority of the code and that that code is right here that's this function here we have a boolean here which keeps track of whether or not the last time we toggle dwee were on or off and then we have an if statement here that checks to see whether the button has been pushed and if it has been we check to see if we were off and if we were we tell the light topic 1 indicating that we want to turn on and we set our is on variable to true otherwise else we tell our light node that we want to turn off and we set our current state to off so other than that there's not a whole lot going on again very simple loop all we do is we check to reconnect we run our MQTT loop and we check our button and we give a small delay just let the ESP module has time to run the Wi-Fi back end so between these two pieces of software that I've just shown you this is all you actually need to get up and running with this tutorial today now I'm also going to show you a Python script that will allow you to keep track of when the lights were turned on and off and how long they were on for so this should be useful for any of you guys who actually want to keep track of the lights in your house and how long they've been on and off for it's a really useful little script in my opinion all right so here's our Python script so again this is not strictly necessary to have your light turning on and off it's not needed but it is a useful little script to keep track of things up at the top here we import a bunch of packages the one that you probably don't have at this point or at least is not standard package is the PA hom QT t client and I'll put a link to this package in the description of the video so you can find it and install yourself so we have a variable here that keeps track of when we actually turned the light on last and then here this is all it looks complicated it's not that complicated it's really just creating a log file and setting up the format for longer so if you want to understand in detail how this works just look up the logging package and it'll tell you exactly what all of this is so I'm going to skip over this chunk of code here just for a moment and we're going to look at our MQTT setup here so right off the bat we create a an instance of our mqtt client and we call it MQTT and then we need to tell it what to do when we receive a message that we're subscribed to so I have this function called on message which I've skipped over for a moment this function will be called every time we receive a message - anything that we're subscribed to so it's not unique to the subscribe topic it is just a generalized function it's the same as our callback function on our Arduino code so I'm assuming that you will be connecting to your MQTT broker from your raspberry pi from the device that is running the broker so I'm assuming that you're connecting to it from localhost and it's the port for them qtt broker is 1883 and then we subscribe to our light topic so we're actually subscribing to the light confirm topic because I could press the button and if the light module isn't plugged in then the light won't turn on so I don't want to log the light being turned on if the lights not even plugged in so I have it actually linked to the light one confirm which is our little ping back and that will only make it so we log information that's actually happened that the actually the light has actually turned on or off not just that we wanted it to turn on or off but that the light wasn't plugged in so it didn't turn on or off and then we start the mqtt loop in a separate thread and yes I know this is maybe not the best way to have my script to stay open but because the mqtt client is running in a separate thread I need some way to keep the script running so I have it just in an infinite while loop so up here we have our on message function so this is what will be called every time we receive any message that we're subscribed to in this tutorial we're only subscribed to one topic but this will get called for all the topics if we were subscribed to more than one so the first thing that we do is we define our global variables here so these that are things that we'll be changing as we run this this function so on start time like I said that's our our flag for when we turned the light on last and then we have our lager and this will just allow us to actually output to a log file from this function and yes I know that function is the wrong term for Python but I'm a C programmer so deal with it I set I column functions so we have our local time and I just make this up at the top this is just a neatly formatted string of the current date and time and this will be appended to our log file every time we logged something this next line here just prints out the topic and the payload that we've just received just that we can see in the console what's happening as its as messages are coming in then we check here to see whether or not our message topic was our light one confirm we're only subscribed to one topic here but I figured that I would have this just to make things more clear that this should only happen when our light one confirmed topic is sending the message so here we check if our payload was equal to on and what we do is we take note of the current time and then we log it and we just say what light was turned on at and print out the time and we put into our log file as well otherwise if the payload was not on I'm assuming that the payload was off and I could be more explicit and say Elif message dot payload equals off but this should work well enough and it does so now we have to actually take note of how long the light was on so to do that we take the difference of now versus when we turned it on which will return back to us the number of seconds of the light was turned on and then we print and lug the time that the light was turned off at and then we print and log the total number of seconds that the light was on for that's basically all there is to the script it's a very simple little script it doesn't do a whole lot but it does get you up and running with MQTT on Python so you can use Python scripts to control your home automation as well using this method and it's just it's a really useful thing to have around to be able to log when things are happening so I hope you find this useful as well again you don't need it but it is a nice script to use now that we have all of the code and we've seen how all of it works let's get it uploaded on to our ESP modules and actually test it out all right so now that we've seen both how this is wired up and the code behind it let's see it running in action so I have over here on the table I have my ESP modules now remember they are not actually connected together physically they will be talking via MQTT and I also have the terminal window up so you can actually see the Python script logging and running as we turn the light on and off alright now that the script is running let's try turning the light on so I have my ESP module over here with the button attached when I click the button and look at that the light turned on and our terminal window registered that I turn the light on and print it at the time that I turned it on and now if I click the button again light turned off and not only did our our terminal window here register the time that we turned it off but it also told us that the light was on for a total of 11 seconds now this is logged here on the terminal window but it's also being logged to a file that's being generated each the Python script is run so you can actually save these log files and parse them out and understand just how long your lights have been on and off for throughout the day so that's all there is for the tutorial today I hope you guys have been enjoying watching this series as much as I've been enjoying making it if you do like this series and you'd like to see more videos like it definitely subscribe to my channel and make sure to LIKE this video if you have any special requests for videos that you'd like to see me make please definitely let me know in the comments or shoot me a message on Twitter I'd love to hear what you guys want me to make as well alright well that's it for this video I'll see you guys next time take it easy I'll see you later
Info
Channel: ItKindaWorks
Views: 40,071
Rating: undefined out of 5
Keywords: Home Automation, Relay, Lights, MQTT, Raspberry, Pi, Raspberry Pi, Raspi, IoT, internet of things, tutorial, how to, howto, how-to, Arduino, ESP8266, Python, Paho, make
Id: AUbFWSz8PjA
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Fri Mar 04 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.