Home Automation at Home Part 1: ESP8266 & MQTT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the first part of my home automation tutorial series in previous videos of mine I've shown you how to get the esp8266 module up and running with the arduino ide and i've even shown you guys how to build very nice and quick power supplies for your modules as well in this video series we're going to take these modules and we're going to network these devices together to start the basis of a home automation system that you can build in your own house so to start today I wanted to show you guys how to connect your ESP modules to a Raspberry Pi using the MQTT protocol MQTT is a lightweight messaging protocol that allows embedded devices such as the esp8266 to talk to each other or to talk to the outside world it uses a publish and subscribed messaging model wherein one device publishes a message and all devices subscribed to that topic receive that same message it does require a broker or server that handles all the messages and handles how to distribute them amongst the various clients and that's what we'll be using this Raspberry Pi for today this tutorial assumes that you already have a Raspberry Pi set up and understand how to log into it so that you can install the MQTT software let's head over to the computer to see how to get everything set up so to use mqtt we first have to install the broker or the server on our Raspberry Pi I've chosen to install the mosquito broker it's an open-source broker it works really well it's my MQTT broker of choice this tutorial is going to show you how to setup and run using the mosquito broker alright so to get setup we first have to add the repository to a Raspberry Pi and then we can install it so I'm going to take the commands from this page and just paste them into my SSH terminal the right here I will put a link to this page in the description of the video so you can follow along with me now at this point we need to choose one of these two repositories to add here and I have the Jesse distribution of raspbian but you need to make the choice whether you have Weezy or Jesse and then add the correct depository to yours so now that I have the repository added to my list I'm going to update my package manager so you can see I already have the Jesse repository added to my list so it gave me a little error there but hopefully you shouldn't have any error at the end of your update so at this point now there's only one thing that we have to do to actually get mosquito installed and that's to install it from the repository so we're actually going to install both the mosquitoes server and the publish and subscribe services so that we can test out our server after we've installed it you so at this point now we can test out our mosquito server and make sure that everything is running as we want it to and this is the reason that we installed the mosquito clients application as well because it will let us both publish and subscribe from our terminal here so I have two terminals open split-screen and one of them is going to be subscribed to a topic and the other is going to publish to that topic and we can see how it works in real time so to subscribe to a topic on your Raspberry Pi all you have to do is mosquito sub - T for the topic and I'm going to make my topic test topic so at this point now we are subscribed to the topic up here on the top screen and moving down to the bottom screen mosquito pub - T for topic so again / test / topic - M for our message and hello and you can see up there at the top now we have hello has printed out and so you can see this is the basic model of publishing and subscribed services using mosquito so at this point now let's take a look at the esp8266 code that we will use to connect and talk to this mosquito server so here we have the arduino code for a simple light that we can turn on and off using the MQTT protocol so the first thing that we do is we have our esp8266 Wi-Fi library and then we are going to be using the pub/sub client library which can be gotten at this link here and this will actually allow us to create a mqtt client on our esp8266 to connect to our server so here are the only lines that you should have to edit in order to get this program up and running you need to put the IP address of your mqtt server so that's your raspberry pi that you have running and then you need to put your Wi-Fi SSID your network name and then your Wi-Fi password if you have one this program will be turning on and off a light on GPIO pin 2 so we define that there and then the topic for our MQTT server that we'll be listening to that will be subscribed to is slash test slash light one so when we send a one as our message to this topic it will turn our light on and we when we send 0 it'll turn the light back off so then we just set up our Wi-Fi client here and our pub/sub client and set up we set the pin mode and set our light to be off initially we enable the serial line for debugging and then we start the Wi-Fi subsystem at this point I call little function here called reconnect which I have down here towards the bottom of the program and it this looks complicated the majority of it is in two sections first is reconnecting to Wi-Fi and the second is reconnecting to the MQTT server so under the Wi-Fi reconnect it just prints out what we're trying to connect to and then loops and waits until we are actually connected and then once we're connected it just prints out our IP address so pretty simple chunk of code here and then down here we check to see if we're connected to Wi-Fi if we are we will continuously attempt to connect to our mqtt server so to do that we will generate a unique name by based on the MAC address of the ESP module and then once we are connected it will print out and then we will subscribe to our light topic and this will make it so that any message we send there will be received by the ESP module and here is the Mac - string function that we call up here to generate our unique ID so the only other part of code before we get to our loop is our callback so this function here will be called whenever there is a new message from our light topic here so what we do in our callback is we convert the topic to a string object rather than a character pointer and if we had a more complicated program this would make it easier for us to parse out which topic has been sent to us which message we've received most recently we print out a little bit of debugging information and then to actually change our light we check the first byte in our payload and see if it's a 1 or if it's a 0 if it's a 1 we turn our light on and we're actually going to publish to slash test slash confirm and print out light on so we are subscribed to slash test slash light 1 and every time we turn our light on or off we will actually publish back to slash test slash confirm telling either the light is on or the light is off inside of our loop all we do is we make sure that we're connected to Wi-Fi if not we attempt to reconnect and then we run our MQTT loop and this just maintains our connection and also looks for any new messages that will bring up our callback function here and then we have a small lay in here because delay actually tells the ESP module okay we're done doing whatever we need to do now you go and handle whatever you need to do on the Wi-Fi end and then get back to me so that's all the loop does so this program is very simple it has again a fair amount of setup here but it's really most of it is printing debug information and then our loop is very simple it just waits for message and when we receive a message it handles it in our callback function here so let's see this actually running in action with the server and the ESP module in real time all right so as you can see the esp8266 is hooked up and powered and has a little red LED connected on the breadboard the light that was blinking at the beginning of this video and we have our Raspberry Pi up and running and I have on the terminal window here the top section is where we will be publishing to our light topic and the bottom section is subscribed to the test slash confirmed topic so when we send a 1 or a 0 to test light one the ESP module will ping back to the test confirm topic either with light on or light off so let's test it out so I'm going to try to turn the light on Hey look at that the light turned on and it's sent back a message light on now if I send a zero the light turns off and it tells us that the light is off now I could send something else here just a string of characters and nothing happens that's because on the Arduino code it is actually checking to see whether we've sent a one or a zero so again we send one and the light turns on and it confirms for us and zero and the light turns off so that's the basics of getting your MQTT server set up and using it on an esp8266 now in future videos I will show you how to use this to have two esp8266 talking to each other via mqtt I will also be showing you guys how to wire up various sensors to automatically report back to your server and also how to set up a nice user interface so you can monitor all of this from your breth web browser you don't have to use the terminal to look at all of this so stay tuned for those videos I'll be putting them out as quickly as I can make them I love showing you guys this stuff this is what I do every day if you have an idea for a video that you want me to do let me know shoot me a message on Twitter and I will be sure to try to get back to you and make the video that you want to see so until next time guys take it easy see you later
Info
Channel: ItKindaWorks
Views: 175,484
Rating: undefined out of 5
Keywords: ESP8266, Home, Automation, IoT, internet of things, internetofthings, Arduino, MQTT, Tutorial, how, to, how to, WIFI, Networking, Network, Microcontroller, HAH, Teensy, Protocol, Raspberry Pi, Raspi
Id: PgsH43Tpqjc
Channel Id: undefined
Length: 14min 2sec (842 seconds)
Published: Tue Mar 01 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.