RPI Jessie and Ping

I was having some issues the other day with SD Card corruption, just after a few hours use and a few reboots the card was done. So I opened ApplePi-Baker and got ready to re-flash the card. Then I thought it had been a while so I went to see if there was a new distro, I don’t really keep up. In fact yes there was, Jessie was now available. Sweet.

So far I have not really noticed any issues (other than this one) with Jessie. It even has Node-RED out of the box. But I couldn’t figure out why my presence detection flow wasn’t working. It was working just fine before I switched from Wheezy to Jessie and yes I did install the Ping node required. I could ping my phone so what was up? I decided to try and ping my phone from the RPi. AHA! That was the problem.

Trying to run a ping resulted in ping: icmp open socket: Operation not permitted

Say wuh? Some quick Googling and I found this thread at the Ubuntu forums.

Run command ls -l `which ping`. If it does not look like this -rwsr-xr-x 1 root root 30856 2007-07-06 02:40 /bin/ping then that is why. The important part is the “s”.

-rwsr-xr-x 1 root root 30856 2007-07-06 02:40 /bin/ping

If you don’t have an “s” run this command to fix it: sudo chmod u+s `which ping`. You should be able to ping away to your hearts content now.

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!