Home Automation with Node.js and MQTT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm Michael Dawson I'm a senior software developer with IBM my day job as I work on node core out the community but at home I like to experiment with IOT and I built a few things which I'm going to talk to you about today what I want to do is I don't have a lot of time but what I want to leave you with is a little bit of knowledge about IOT MQTT and how node is a really great language to do your your IOT type projects in and enough knowledge and hopefully excitement to get started in building some of your own projects so just to start out with you know a little bit of terminology if you've been in this session and track all day you may not may have already seen this but for those who haven't I'll start out you know first of all what is the Internet of Things it's really a network of connected devices so the things are just you know actual little little devices that can run code these devices can provide you data so they can be sensors like temperature sensors actuators that tell you whether doors are opened and closed and so forth and they can also be controlled so you can send messages back to those devices so for example you can say I want to turn on and off a light switch something like that and if you want to read it read more there's a good website they'll give you a good definition of the Internet of Things MQTT is the MQ primary telemetry transport and it's a nice lightweight publish and subscribe system where you can publish messages and consume messages as a client and it makes it really easy to communicate between those devices it's a really nice lightweight protocol it has very small footprint so you can get easily into small devices I've heard you know things like you can have an mqtt implementation something like 80k so pretty small footprint the messages themselves can be very small so if you have low bandwidth it's also good for that kind of environment based on this map that spec the smallest message could be as small as 2 bytes depending on what you sent of course you send more data like a larger message you're going to have a larger overall message as well and it's pitched if you go to the MQTT org site it's actually pitched as a way to connect up the Internet of Things so I just want to start out by telling you a bit about MQTT itself so there's a few basic concepts the first one is topics so topics are the way that you register your interest or you publish messages and those topics are really just the set of strings separated by slashes so for example in the example that I'm going to show you which is a home alarm system that I built using node mqtt raspberry pi docker you know I've got a few of my topics here and so for example one house alarm status is simply a topic that will publish messages to say what's the status of the alarm is the alarm activated is the alarm on as you'll arm off similarly you know something like house alarm zone if you think of an alarm system they have one or more zones and so it's a topic that when the zone status changes you know the zone is triggered or the zone as we set you're going to publish a message to that particular topic over here the house 2 to 6 2 / 1/2 in the project that I've done I use 433 megahertz devices and there's a standard protocol the 2 to 6 - 2 to 7 / 2 protocol which is often used to communicate those so I just map those directly into my topics so that when a particular device sends a message wireless message I'll end up publishing a message to that particular topic the other interesting thing you can do these with these topics you can see that there's one with a plus and there's one with a number sign they're a hash mark the plus basically says I want to match one element so I could do you know a + b / c and I'll get everything which you know has that as a and C in those positions but has anything else in the middle one and the hash tag is basically match everything down below so if you want to say I want to have listen to all the messages on a particular topic under a particular tree you can do that as well so topics that's the first part the second part is the messages themselves again MQTT is very lightweight protocol not not very complicated those messages can be strings and they can be anything that you want the two examples that I have here again map back to my alarm system the first one is something that you would see in you know when you're using that 2 2 6 2 or 2 - 7 - protocol it's actually a tri-state protocol where you can have 0 1 or floating and basically the device just sends out a message that says you know there's I think it's eight bits or twelve actually twelve twelve that say you know floating floating floating 1 1 0 0 0 and that identifies a particular device and if you'd opened up the device you can actually set a bunch of jumpers which will actually set what's going to be floating you know each bit is gonna be floating 1 or 0 but you can also have other things and the other thing I do in the arm system is I actually have a service listening on a particular topics I'm publishing two topics to take picture so when the alarm goes off you can take pictures of what's going on hopefully catching the your intruders and so your messages can be something like that which is basically a the identifier for a picture which you then want to display in the GUI when you're saying okay let's take a look at all the pictures that the alarm is captured so those are the two key key concepts the topics and the messages the last one is the quality of service so mqtt provides three qualities of service level 0 is basically just do your best effort you're going to publish your message it may or may not get there most of the time it will and for things like you know temperature sensors that's perfectly fine if you're sending out the temperature every 30 seconds it really doesn't matter if you're going to lose the occasional message it's got the lowest overhead the lightest weight and so you know it's it's good to use unless you need one of the higher levels QoS one is deliver at least once so that makes sure you're going to get your message but you could get your message multiple times and again for some cases that's okay but there's other times where you don't want to have the same message more than once in which case you can move up to quality service - which guarantees you're going to get your message once and only one time so those are the different levels you can get the other thing of course is that if we're connecting these devices we need to be able to do it securely we don't want just anybody to be able to connect in to overall overall network so MQTT does provide you know the standard kind of security features that you'd expect there's user IDs device IDs passwords more importantly for me though is you can use TLS you can encrypt the tunnel and you can use certificate so you can actually say only devices that have these particular certificates installed can actually connect into my network so that's pretty secure it's important to be able to have clients and brokers across different platforms so M teetee uses the concept of a broker which is really the the mediator so you have client multiple clients that connect your particular broker clients can publish on particular topics or subscribe to particular topics and it's the broker that manages connecting up you know the people who are publishing and the people who are listening on the topics it's easy to get a client for MQTT across all sorts of platforms so because we're interested in node of course there's the mqtt NPM package you can just install and use and I'll show you how to use that a little bit later on and in some of my slides but follow is also another project which gives you native application ativ clients written in c and they run across a whole bunch of different operating systems in platforms so it's easy to get clients similarly it's easy to get brokers Mosca again is one that's written in node and you can connect up to it I've shown up here the you know a screen capture of me just starting it up so it's again an NPM installs startup Mosca and you've now got a MQTT server you can connect to publish and subscribe to message messages and even if you don't even want to do that there's actually IOT services out there like on ibm's bluemix there's an IOT service where you can register devices and then publish and receive messages so you can very easily get set up so you can publish and receive the messages between your devices so now we'll talk a little bit about the example I'm going to take you through to illustrate how much fun and how easy it can be to do all this with node you know my approach was to glue the devices together with mqtt so the devices may speak different protocols but in the end what I wanted was something that ends up just publishing a message to an MQTT topic so if my door window sensor goes off I don't need to necessarily worry about in my main application interfacing with that piece of hardware I'm going to get a message posted to a particular topic and then I can do the right thing and so that the MQ MQTT is kind of the glue that glues all the pieces together and then I built the smarts and note itself and notice a nice fit to go together with that it's all open source so if you're interested in the various projects that I put together to glue all these pieces together I'll talk a bit about more about the overall architecture on the next page it uses raspberry pi I'm under a number of different devices and even the last project the RX TX temp board you know there's actually a ad gerber file so you can go and put and sort of build your own PCB boards here if you want to if anybody has a Raspberry Pi I'm willing to give one away so you know at the end you can come by may put your name in a hat or something like that if you want to build your own and in addition to the the home alarm system I'll talk a bit about there's also some other projects there like the Home Dashboard where it's you know just a very simple node application that shows you what's going on in the house in terms of temperatures how much power your house is using those kinds of things so the overall architecture in the home itself the alarm system is a Raspberry Pi but none of the smart Cesare actually there it really just acts as a relay so there's a number of processes running in the raspberry pi PI for 33 TX and rx so there's those are basically processes written in C that use the file clients and they talk out through 433 megahertz through some transmitters and receivers and I'll show you the circuits for those fairly straightforward and their job is simply to either listen for you know you know RF messages interpret them and then publish that that message happened on a particular topic or the the PI 43 TX basically takes a message in MQTT message and says okay I'm going to send out a message over RF based on that there's also connection to a USB camera so there's a process that talks to that USB camera again connected through MQTT T so that when it gets a message on MQTT it says please take a picture it goes off and takes that picture for you when it's finished it publishes back on another topic saying hey I've just taken your picture here's the name of the picture which lets you then later find it on your your website and then you know there's a number of devices which we'll talk a little bit more about that are communicated through through through 433 megahertz out in the cloud so this is all outside of the house you know over an internet connection there's an MQTT server running in a docker instance there's a web server running in another doctor instance I think it was Apache - that's used to store the pictures that alarm system takes and when it does take a picture it just SCPs them up to the particular web server there's the MQTT connection of course that publishes in and basically lets us get the messages from the house to the the overall controller that's again over through MQTT and secure of course with TLS and certificates so not just anybody can connect in there's another docker instance that is running the alarm system itself so the thing written a node that I'll show you a little bit more about and of course I wanted things like notifications so if the alarm goes off they can actually send me an SMS message on my phone to do that I go out to Twilio which is a service that lets you very easily send SMS text messages to yourself and then finally there you know the the node application also provides a GUI through WebSockets that lets you connect sit connect in and actually get a panel much like a regular alarm system that you'd be you know familiar with with the ability to arm disarm what's going on for each of the zones the status and so forth and that browser also reaches back to the web server to get the pictures as part of the overall UI so you know one of the things I want to highlight is that you know Nodine MQTT there's just a really nice fit and it's one of the reasons I think it's one of the best languages to do your IOT systems interaction with MQTT they're both very asynchronous just like the real world you know like an alarm system we're based on event so something happens the door opens we sent an event note is a great fit because if I can very easily basically say register for something to happen when that event happens and when you look at the code here you can see that you know I just start out by basically saying I want to connect to the mine qtt server so that's the very first line I just really need to provide a URL a number of options those options can include things like my pointers to my certificates and so forth once I've done that I want to basically say you know now that I've connected I want to register that when certain things like I actually connect I want to subscribe to particular topics so as I mentioned before those topics are just strings and they can be things like the status of the alarm they can be particulars own is going on and off so I just register by saying you know climbed up to subscribe to the alarm status topic their zone prefix topic and I'll talk a little bit more about that one because there's a little more going on there it takes pictures so there's a topic that says hey a new picture has been taken and then the the zone mapping topic that says okay I want to actually subscribe on all the zones that I've registered for the alarm system and so you can see it's really easy to set yourself up then when the next part this says client on message it just says you know when a message happens on any one of those topics I'm going to run a particular function very nice asynchronous and a really good fit for node similarly on the send side I can really easily just say I want to publish to a particular topic this particular message so that's just a string and then I can make that that the message whatever what in this case it's like I'm just publishing take to say please take a picture and we can go on so how do we actually interact with the physical world itself in the particular arm system so you can see just two very simple circuits these can be easily wired up to your Raspberry Pi it doesn't take more than a few pins and you can see here I've got you know the overall look here is my first prototype where I wired things together and then here's you know after actually sent off and had the PCBs built it's just a little bit neater but does exactly the same thing and then you know the software on the other side has a few interesting things like you know you're you're reading pins you're using Linux to reading the state off of some digital i/o pins as the people mentioned earlier topics the resolution isn't quite as as much as you would on a small my controller so you have to be a little bit more careful in terms of not doing all of your work on the interrupt handler but trying to pass that off to you know another thread which is actually going to process the messages once you perceive them this gets into show this this code then shows a little bit more specific like I said within terms of the zone mapping so what happens when one of those zones goes off and one of the reasons I used 403 through 433 megahertz is because it's really easy to get devices so this is a simple device it's the door/window sensor you know you've probably seen something like this you open the door the window it'll go off you can get something like this for three or four dollars off the ebay you can easily also get most detectors you can get even smoke alarms that are going to send messages and you know you can very very easily then set it up so that when you know you get a message that's published on a particular topic so this is saying okay when I get a particular topic if it's defined in my zone mapping so it's corresponds to one of the zones that I've set up for my alarm I'm going to then republish it saying okay well that that particular zone was triggered and then you know the next piece says oh and if the you know if the alarm is actually in the armed State and I've got something that's triggered when my zones I'm going to say I'm going to publish to the topic that says the alarm status has now been triggered and you know that message will then be captured and processed to say oh well the alarm went off I'm not going to do things like send messages and so forth and down here shows a little bit of the mapping so in this case you know I've mapped into say okay well the particular topics that I have you know house 2 2 6 2 / 1 2 3 4 5 I've actually used the jumpers in my devices to map to one through five and they map to zones you know 1 2 3 4 not necessarily in the same order and they're the names that I've assigned to particular those particular topics the other thing with the other thing the alarm does is it takes pictures so there's a USB camera connected to it and again it uses the same thing so here basically you know when the alarm goes off and we want to start taking pictures one of the other things is you know your house is probably if you're not there it's dark so I want to be able to take pictures in the dark I got an infrared illuminator unfortunately there weren't any devices which would actually allow you to turn the the illuminator on and off so I built one myself it's actually a pretty simple circuit that you can use there's chips that understand these protocols and you just hook up a receiver and so that's what I did and so now I can actually call this IR on function which basically says you know publish message to a particular topic it sends out the wireless message to the light turns it on then you then you publish to the topic saying please take a picture it does and then it publishes back the name of the picture that it's taken the note application itself and you can go you know you can go look on the it have repo - look at the code itself but this is the UI that it provides through WebSockets you know you can basically see that it you know you can arm disarm you can ask it to take a picture it gives you links to the recent pictures that were taken it shows you the status of all the zones you can see a log of what's happening you can take a look at those pictures in fact you can connect up multiple cameras to have different views one infrared one not infrared and of course because it's web-based you can you can connect to it on your your cell phone as well a native client would be nice I haven't written one if everybody anybody was interested in writing one that'd be great but I haven't quite gotten there the other thing on the on the the cell phone is you can get SMS messages so it sends you the messages Twilio is a really nice client that lets you send yourself an SMS message with very very little work this is basically the code that all the code that you need a node and you can send up messages which end up showing up on your alarm so it's kind of neat - every time you open it open up your door you end up getting a text message saying hey somebody just entered your house so when you put all these pieces together now you know if hopefully I've got you a little bit interested if you want to deploy your own where are you going to deploy that the cloud components well you can easily do that for free now one example is where you can do that as ibm's bluemix which is our platform as a service and docker container and virtual machine offering are basically our cloud offering so you know in the the cloud foundry app which is basically that the platform as a service you can easily publish the node components so the node controller the broker equally you can publish those as docker instances in the docker container option you can also publish the web server there and if you don't want to do that and you can also use the iot service which basically provides you the pieces to glue devices together without having to worry about the actual individual servers and stuff like that yourself so I think I'm actually now at exactly my time I'm just going to show you the live so these are a few you know the ones which I typically have on my desktop I'll see it didn't arranged so nicely but you know there's the the the alarm console which you can get from anywhere here's the like you know the the data is coming from my house right now how much power is currently being used at temperature in the basement humidity furnace room and you know it's easy to extend these things too I'm working on you know an Arduino based device so you can use the same RF communication but then have it turn on and off your air conditioner all those kinds of different things um so that's all I had if there's any questions sure I do know we were a pet free house the sensors the the motion detectors claimed to be pet safe but I don't have any pets to be able to test them my kids are big enough that they definitely set them off oh sure incoming messages so all the messages the the 433 megahertz is actually it's it's it's if you collide you collide so the device has all send out multiple messages so they'll actually send the same message out ten times in a row and multiple times so you will get collisions and you'll basically ignore those and but otherwise each individual message as long as it's time separated is different and and you know different different protocols have different shapes of the pulses and so forth they'll have check sums on them and those kind of things okay if that's it thank you for coming
Info
Channel: node.js
Views: 79,441
Rating: 4.9163589 out of 5
Keywords: Home Automation, MQTT, IBM, Node.js, Node.js Interactive 2015
Id: 80DxfDmoZUI
Channel Id: undefined
Length: 21min 56sec (1316 seconds)
Published: Wed Dec 09 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.