Node-RED: Presence Detection

Presence Detection

Huge thanks out to Martin Harizanov (https://harizanov.com) I don’t know the man but sifting through his very informative blog has allowed me to get this code (and many others) up and running. You can find his original post on the subject here https://harizanov.com/2014/03/presence-detection-using-phones-wifi-and-node-red/

This is basically his work with a few tweaks. I decided to send all my sensor logs to my home servers MySQL db. The RPi uses an SDCard that has limited write life, and I think there are some performance issues there too (I don’t know, never bothered to look it up, I’m talking out my ass). I have a home server that runs MySQL and Apache so I was going to just offload that work away from the RPi anyway.

To get the presence detecting done I ping for my phone every 30 seconds or so then that result goes to a function to get analyzed (by Mr. Harizanov’s code), then that result is given to another function that UPDATES a MySQL database then finally on to a MySQL node to make the connection.

Ping -> Analyze Ping Function -> Update MySQL Function -> Connect to MySQL Server

I noticed that it took about 2-4 ping results before it would change status, and that it didn’t matter if it was a 30 second or 5 minute delay. So I went with 30 seconds, it doesn’t hammer the Pi too bad and I don’t have to wait too long to test it, hah. I did not thoroughly test this theory, I just noticed it once or twice so I could be way off.

Hopefully this helps someone else in their time of need. One is glad to be of service.

Presence Detection

If you DO NOT have or use a cellphone (or a device that uses WiFi that goes with you everywhere) then this WILL NOT WORK for you.

It works by utilizing the fact that almost everyone has a cellphone and that they usually always have it with them. The idea is simple, Node-RED looks for your cellphone. If it can see your phone it assumes you are home along with it. How do we see your phone? Ping it. Basically ping the IP address of your phone every so often and it the ping comes back you are home, if it times out you are assumed away.

You will need node-red-node-ping installed for this to work.
(http://flows.nodered.org/node/node-red-node-ping)

Nodes Required

  • Ping Node http://flows.nodered.org/node/node-red-node-ping
  • Function (Analyze ping results)
  • Function (Insert data payload into MySQL db)
  • MySQL Node to make the connection http://flows.nodered.org/node/node-red-node-mysql

You may want to add a debug node after the Update DB function and two inject nodes before the Update DB function, set as 1 and 0 to simulate your phone home/away.

Ping Node
This one is simple, just add the IP of your cellphone and set the timer. I used 30 seconds, although I will probably change this to 5 minutes. It helps to have control over your router so you can set static IPs.

Function Node (Analyze Ping Result)
Don’t forget the global variable, this is needed in the next step.

Function (Update MySQL db)
You will need to change the db settings to match yours of course

Connect to MySQL node
This is pretty straight forward, setup with your servers credentials

That should get your Node-RED to detect if you are home or not based off your cellphones Wifi. Pretty cool, yeah but what do you do with that info? Log it? Display? Yes! Take the info from my last post and apply it here and we can check the status any time we want! http://itsalllost.com/node-red-requesting-sensor-data-via-http/

Check status via HTTP

Here’s the code to check the status of the global variables (make sure they all match!)

Here is the flow for presence detection in all it’s glory!

Node-RED: Requesting sensor data via HTTP

Fo as cool and useful as Node-RED is I am surprised at the lack of “useful” documentation and examples on the web. Sure http://flows.nodered.org/ has some very useful flows and nodes, no doubt but they don’t really help a beginner slap some IoT goodness together. I have some knowledge but I by all means don’t have “skills”, at least I don’t think so. Anyway enough rambling on to the code…

Screen Shot 2015-11-17 at 10.36.29 PM

DISCLAIMER: I by no means really know what I am doing, I know just enough to be dangerous. I am not responsible for what you do or what happens, by you or someone following anything mentioned on this page (or site).

 

Display sensor data on a webpage with Node-RED
This should allow you to take data from a sensor or another node and display it on a webpage.

Nodes you will need:

  • HTTP request (set as GET)
  • Function (Get current values)
  • Template (HTTP)
  • HTTP Response

Get Current Values Function

HTTP Template

That is the bulk of it, now all you have to do is declare the global variables you just created. Add a function block directly after your sensor (or edit the one you already have). Now you just need to set the sensors payload to equal the global variable.

Set each global variable for each sensor you have like this (I added it at the very bottom, before “return msg;”.):
context.global.variable1 = msg.payload;

Refresh the page button

This should allow you to get a button on a webpage to refresh the page for you.

Nodes you will need:

  • HTTP request (set as GET)
  • Function (Return to page)

Return to page Function:
msg.res.redirect("/checksensors");

Well thats it, I hope this was enough straight forward code and enough direction to get you going. I had to scrape this all together. One is glad to be of service.

For your pleasure and in the chances I may help cure some headaches, because I hate headaches, here is all the code (minus setting the global variables):

This post is were I found the code needed to get the above results:
https://groups.google.com/forum/#!searchin/node-red/gpio/node-red/ycho_15kJvM/bTOY9tbwrmYJ

The Node is Red Long Live The Node

The Node is Red, long live the Node! I got back into the Pi recently after a few months off of fiddling with it. I had already made the sub board for the alarm, I decided to remake the temperature board as well (it didn’t work either). So I had the boards ready now I needed the software. I could try to put back together the original scripts I had, or I could just start fresh. I decided to start fresh. While Googling something (I don’t remember what) I came across Node-Red. Completely unfamiliar with it. I have seen Node.js here or there but thats it. Looked a little further and then I installed it. It’s awesome really. I am nit the best at Javascript so it’s a curve to me but the GUI makes up for that. I got Node-Red up and running on the Raspberry Pi in about 15 minutes. I won’t go into install details as they are out there already. What is not out there, or at least is very hard to find, are examples of GOOD flows, or tutorials on how to get things done. Like what code is needed to do the simple things, but I guess they are expecting you to know that already.

Getting Node-Red installed was easy, besides not finding much help on the web I also didn’t realize that you need to install all the modules you will be using before hand. So things like Twitter, Pushbullet, BMP temp sensors won’t work until those modules are installed. Installing was no problem, but it took the little RPi forever to install them all. I would say it took the better part of half a day to search and get them all loaded.

Because of the lack if examples (and my lack of JS) it took me a few days to figure out how to grab part of the URL and pass it to a page to spit it back at you. Simple right? So here it is, copy and pasted in all its glory (taken from http://sharpk60.blogspot.com/2014/10/first-steps-with-node-red.html):