presence detection - wifi via arping

6 replies [Last post]
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011

Okay heres my first attempt, just add the names and mac addresses into the table and your good to go. tested on Rasp PI after installing arping (sudo apt-get install arping)

lets see if this works for the apple stuff..I suspect they will stop responding on standby but time will tell!

 

Garry.

 

 

-- Presence detection via wifi
module(...,package.seeall)

require("xap")
require("xap.bsc")
require("string")

info={
   version="1.0", description="Presence Detection via Wifi"
}

local endpoints = {GarryPhone = "xx:xx:xx:09:33:1B", GarryIpad = "xx:xx:xx:xx:82:BA"}

function Detection(timer, self)
                local msg = "arping -c 1 -i eth0 -q "..self.address
--              print("msg"..msg)
                retval = os.execute(msg)
                if (retval == 0) then state = "on" else state = "off" end
                self:setState(state)
                self:sendEvent()
        timer:reset()
end

function init()
        for k,v in pairs(endpoints) do
                k = bsc.Endpoint{instance="Presence:"..k, direction=bsc.INPUT, type=bsc.BINARY, address=v}
                xap.Timer(Detection, 60, k):start(true)
        end
end

 

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
well I shouldnt be

well I shouldnt be surprised!. iphone doesnt respond to arping on standby.

have installed arp-scan (sudo apt-get install arp-scan) on the Pi and modded the scripts as follows to see if this is any better? NOTE HEX mac address needs to be lower case.

 

-- Presence detection via wifi
module(...,package.seeall)

require("xap")
require("xap.bsc")
require("string")

info={
   version="2.0", description="Presence Detection via Wifi"
}

local endpoints = {GarryPhone = "xx:xx:xx:09:33:1b", GarryIpad = "xx:xx:xx:xx:82:ba"}

function Detection(timer, self)
                 local msg = "sudo arp-scan 192.168.0.3-192.168.0.20 |grep "..self.address

--              print("msg"..msg)
                retval = os.execute(msg)
                if (retval == 0) then state = "on" else state = "off" end
                self:setState(state)
                self:sendEvent()
        timer:reset()
end

function init()
        for k,v in pairs(endpoints) do
                k = bsc.Endpoint{instance="Presence:"..k, direction=bsc.INPUT, type=bsc.BINARY, address=v}
                xap.Timer(Detection, 60, k):start(true)
        end
end

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Garry what type of iPhone do

Garry what type of iPhone do you have?

As I said in the other thread, my wife's 4S responds to WiFi pings when asleep but my 3GS does not. I use both arp-scan and l2ping in a python script to detect presence and switch on bluetooth on the 3GS.

Incidentally, I use the return code to increment an absence counter or reset it to 0 if a zero return code is received. This works OK-ish for longer absences but I don't think it's ever going to be able to switch a light on when I return home because it's just too slow a respone.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks Allanstill at the

Thanks Allan

I'm still at the playing around stage but some sort of counter is a good idea, I have a 5s. 

Here is the xively plot for the arp-scan code over the last 45mins. Iphone in stanby unused the whole time!?  Looks too flaky to be useful?

any other ideas folks?

AttachmentSize
image.jpg 16.05 KB
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Better than nothing!

Ok added a fifteen minute timeout and poll every 45secs and this seems robust on mine ant the wife's iPhone 5S.

Probably good enough for heating and water, etc but not really responsive enough for lighting.

module(...,package.seeall)
require("xap")
require("xap.bsc")
require("string")
info={
   version="3.0", description="Presence Detection via Wifi"
}
local endpoints = {GarryPhone = "xx:xx:xx:09:33:xx", EmmaPhone = "xx:xx:xx:a9:af:xx"}
local ttl = 900
function Detection(timer, self)
local msg = "sudo arp-scan 192.168.0.3-192.168.0.20 |grep "..self.address
-- print("msg"..msg)
retval = os.execute(msg)
if (retval == 0) then state = "on" else state = "off" end
if (state == "off") then
if ((os.time()-self.lastSeen)> ttl) then
self:setState(state)
self:sendEvent()
end
else
self:setState(state)
self:sendEvent()
self.lastSeen = os.time()
end
timer:reset()
end
function init()
for k,v in pairs(endpoints) do
    k = bsc.Endpoint{instance="Presence:"..k, direction=bsc.INPUT, type=bsc.BINARY, address=v, lastSeen=os.time()}
xap.Timer(Detection, 45, k):start(true)
end
end
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Looks good

Because I use the raspi to do the detection at the moment and then fire the result off in a UDP packet I don't bother with actual times. I just increment a tick on the HAH and reset it to 0 if I get a hit on WiFi (or bluetooth on my 3GS). As you say OK for detecting longish absences for dealing with heating etc. but not much use for lights.

I was going to play with geofenced reminders on SWMBO's iPhone to see if I could generate anything useful (perhaps via IFTTT) but I'm not sure she's too happy with me knowing when she's arriving and leaving!! I'll have to wait a while until I inherit her phone      :-(     so it probably won't be anytime soon!

 

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
arping

Garry

I had to use the IP rather than MAC address and use arping rather than arp-scan but working fine here.

Thanks for the code

Mark

Hardware Info