18/02/2017

Node-Red Phone Pinger - Home Automation.

Node-Red Phone Pinger... Home Automation

As part of my home automation project that I have running on the raspberry pi, I wanted to use some sort of system where I could also automate my home security too, which is also built into my raspberry pi. I wanted to find a way to do away with the process of remembering to press buttons before you leave the house to set the alarm system and then the same again when you enter the house. It needed to be as fool proof as possible


I started thinking about using an RFID approach and build this into a key pad, coupled with an Arduino Nano and linked to my raspberry pi via I2c with the an RFID keyring attached to my keys, but again this involved me having to do something in order to arm and disarm my house alarm, it wasn't the approach I was looking for. I needed something that I would always have by my side pretty much 24 hours of the day but wouldn't cost me a huge amount of money. So, after searching the internet I found an easier solution by using my phone.


As I'm already using Node-Red throughout all of this project I found a way for Node-Red to ping my phone every 20, 30 or 60 seconds, it's really up to you. As I'm still in the testing stages I have it pinging my phone every 5 seconds.There are a few things you need to do in order to get all of this working first though.

Firstly, you'll need your WiFi on your phone active all the time, don't worry about running your battery flat, WiFi on smartphones usually only accounts for around 5% of battery usage. It will also need to have a static IP address too since you will need a way for your ping node to always ping the phone you want it to, this isn't as complicated as it first might sound but does involve you logging into your ISP router, for me it's Virgin Media and quite straight forward.

The thing I like using this approach is that you can lock your phones identity to your home network via the IP address you assign it and the MAC address built into your phone. It's a bit like using the RFID approach with a built in digital signature but of course using something that you'll have with you all the time.

Let's make a start. For Virgin Media users who have the black Super Hub 2, once you've logged in to your hub you need to click on the advance settings button (bottom right hand corner), when you get to the next page, scroll down towards the bottom to where it says DHCP, you'll need to click where it says "DHCP Reservation". On the top of the next screen you'll see a tab that says "Attached Devices", somewhere on this list you will see the IP address and the MAC address of your phone.

If you're not sure which one is yours then you can find this one your phone. For Android users, go to Settings > About Phone > Status and on the list you'll see the "IP Address" and "WiFi MAC Address" of your phone. In my case it was 192.168.0.8 and the MAC address ended with b3.

As I've done below, looking on the DHCP page of you router will reveal that your phone and it's MAC and IP address are listed.




Underneath all of this you'll see the section where you can add the IP address reservation for your phone, this means that it's only your phone with it's unique digital marker (MAC address) that can connect to your homes WiFi with the IP address that you specify. Give it a name to easily identify it, in my case I've called it "Reena Phone", after you've filled out the MAC address details you can then click the Add Reservation button.

Underneath all of this you'll see the IP Lease Table. This gives you a list of every device connected to your home network and the IP address that's been assigned to it by the DHCP server built into your router, or in my case, the IP address that you've reserved for specific devices that you have connected. You'll see here that I have five phones connected and all of their IP addresses have been reserved. Don't forget to click "Apply" before you finish.

Now we've got all of that out of the way we move onto the Node-Red part of the process. Below you can see the simple flow that I've got set up along with the two nodes, Ping, and Function.




For the Ping node it's pretty straight forward, all you need to do is enter the IP address of your phone, specify how often you want it to ping and then just name the node








With the function node we just need to add a few lines to make everything work. Because I have five devices that are going to be pinged I need to know which device is either on or off, to do this we have to set a topic message, in this case it's "Reena". I also wanted the output on this to be either a zero or one to represent on or off. The typical debug output for the ping node is either "false" showing the phones WiFi to be off or out of range, or a bunch of numbers which I imagine represents the ping time, but this is no good for what we need, which is a one or zero. So we need a simple if and else command and then output this as a message to the next node. 




In the case of the phone connected to the home WiFi network the output would be topic: Reena and msg.payload = 1 as shown here
So there it is, a simple way to use your phone as part of a home automation project which is secure and safe, but don't just take my word for it, try it for yourself. You could add extra nodes to the flow for more connected mobile phones/devices and then pull all of this into a function node to deal with the logic side of things.

If you fancy giving this a go just copy the code below and import it into Node-Red the usual way.

[{"id":"3e290137.a4488e","type":"debug","z":"c5eb5e6e.d7c1b","name":"","active":true,"complete":"false","x":671,"y":72,"wires":[]},{"id":"a39c7032.c8cc6","type":"function","z":"c5eb5e6e.d7c1b","name":"Analyze Result","func":"msg.topic=\"Reena\";\nif (msg.payload === false){\n        msg.payload = 0;\n} else{\n        msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"x":324,"y":72,"wires":[["3e290137.a4488e"]]},{"id":"89cba4fe.3d2058","type":"ping","z":"c5eb5e6e.d7c1b","name":"Reena Phone","host":"192.168.0.8","timer":"5","x":116,"y":72,"wires":[["a39c7032.c8cc6"]]}]

I'll add more for this as I go along, but If you have anything to say or add to this then please feel free to comment below, thank you.

No comments:

Post a Comment