Martin's corner on the web

Presence detection using phone’s WiFi and Node-RED

Nathan tweeted a cool idea couple months ago, use static IP lease for your phone, ping it every minute to see if it responds, and if the ping times out that most probably means that the phone and his owner are not at home:

nathan_tweet

I found ~15 minutes to implement this today in Node-RED, couldn’t be any easier.

Since I always carry my phone with me, this is pretty reliable method of determining my presence at home.

I only needed to ensure that my phone receives a static IP lease, pretty trivial task for most modern wifi routers, you just need to find out you phone’s MAC address. I have WiFi on my phone always on.

Here is just how easy this is to do with Node-RED:

presence_flow

The ping node is set to ping the fixed internal IP address of my Nexus 5 phone every 60 seconds.

Just to be on the sure side, I extended the criteria to three consecutive ping fails:

presence_flow_analyze_results

Finally, that data is fed to emonCMS for home automation and self-quantification purposes:

presence_emoncms

Here is the flow code, you may need to check my previous post for the emonCMS sender part:

[{"id":"ba386057.845d3","type":"mqtt-broker","broker":"localhost","port":"1883"},{"id":"344b04c2.cbb4fc","type":"ping","name":"Ping Nexus 5","host":"192.168.1.80","timer":"60","x":105,"y":160,"z":"127c7a58.ed8386","wires":[["680c5308.97f3ac"]]},{"id":"c7248577.38db78","type":"debug","name":"","active":false,"complete":"false","x":543,"y":248,"z":"127c7a58.ed8386","wires":[]},{"id":"680c5308.97f3ac","type":"function","name":"Analyze Result","func":"// anything stored in context is kept available for next time we get called\ncontext.gotping = context.gotping || 1;\ncontext.pingfails = context.pingfails || 0;\n\n\nif(msg.payload) {\ncontext.gotping = 1;\ncontext.pingfails = 0;\n} \nelse\n{\ncontext.pingfails +=1;\nif(context.pingfails>3) context.gotping=0;\n}\n\nmsg.payload=context.gotping;\n\nreturn msg;","outputs":1,"x":309,"y":183,"z":"127c7a58.ed8386","wires":[["c7248577.38db78","cfb3d65b.304c28"]]},{"id":"cfb3d65b.304c28","type":"mqtt out","name":"Nexus5 presence","topic":"presence/nexus5","broker":"ba386057.845d3","x":546,"y":161,"z":"127c7a58.ed8386","wires":[]},{"id":"74d54a29.8b2ab4","type":"mqtt in","name":"Nexus5 presence","topic":"presence/nexus5","broker":"ba386057.845d3","x":127,"y":276,"z":"127c7a58.ed8386","wires":[["919ef3bc.6e611"]]},{"id":"919ef3bc.6e611","type":"mqtt out","name":"Send to Emoncms","topic":"home/emoncms/out/12","broker":"ba386057.845d3","x":357,"y":276,"z":"127c7a58.ed8386","wires":[]}]

From practical aspect, I will probably use this approach to check my and wife’s presence at home and if both of us are absent for say more than 30 minutes, I could turn down the heating a degree or two.

Update 1: Here is how the presence report looks so far, 1 means I am at home, 0 means I am out:

Presence report

Presence report

WiFi accounted for only 5% of my total battery usage that day:

Battery usage report on Android, pretty heavily used the phone to read news/browse that day

Battery usage report on Android, pretty heavily used the phone to read news/browse that day

11 thoughts on “Presence detection using phone’s WiFi and Node-RED

  1. Paul

    Nice idea, but doesn’t your phone go into energy saving mode after a few minutes and switch off it’s wifi.
    About 3 minutes after screen lock on a iphone results in the wifi being switched off, and not responding to pings.

    Paul

    1. Martin Post author

      I have a nexus 5 android phone, it doesn’t switch off wifi. Are you sure yours switches it off, if it does, how would you be able to receive push emails for example?

      1. Javier

        Hi Martin!

        I visit your webpage regularily and find your posts / tutorials so good, thanks!

        As Paul stated above the iphone switches off wifi at about 15 second after screen lock no matter if you have turn on in your settings (battery saving purposes) So this will not work for iphones, by the way, I’m thinking that perhaps the best presence detection for apple devices is using Bluetooth BLE.
        I think that apple triggers notifications through cellular network that wake up the wifi radio.

        1. Martin Post author

          I’m happy to have Android device then 🙂 Using BLE is probably going to be harder to achieve compared to using WiFi since you’d need a BLE dongle and some coding on top. Too bad Apple didn’t leave an option for the user to decide on WiFi power saving modes, the impact on battery is minimal on my Android..

  2. Jorge Nerín

    You don’t need a static ip, you can use the Thomas Habets arping to test the reachability of the mac address of your phone. As long as it’s connected it’ll answer. See the image

    You could also tie the presence to the dhcp renew messages sent to the router, if the phone fails to renew it’s gone. That way you avoid waking the phone to answer the ping. You need a router which can forward its log to a remote syslog, and then parse the dhcp events.

    OTOH having a static ip makes things easier. 🙂

    1. Martin Post author

      arping is an interesting option, but using it will require modifications to the node-RED ping node. I picked the easy option, I guess

    1. Martin Post author

      Owntracks is in my to-do list, will blog my progress sometime soon. Lovely little app. Still, this method works great when you only care about presence at home, not tracking location.

  3. Steven

    Excellent idea, I got this working using your blog post although had problems with MQQT, so send the data direct into emoncms from node-red missing out the MQQT part for now.

    I created a dashboard which displays a graph like your example and added an LED widget which is a better way to visulize the current presence of a person. What I would like to do is add the timestamp of the last time the value was “1” meaning at home. I can’t find a way to do this on an emoncms dashboard. I’ve asked the question on their forums.

    Maybe I could send the timestamp from node-red as well as the ping status, not sure if this would help. Anyone know how to display the timestamp like this?

  4. Pingback: You watch too much TV | Martin's corner on the web