[SOLVED] Iphone presence issue WIFI


(Rich Koshak) #4

Newer Android phones behave this way too.

There is an arping feature in the Network binding that works very well. See the arping configuration docs in the Network Binding and search the forum for “arping” and you will find several postings about how to tune it properly for iPhone detection. I know many ore using this configuration very successfully for iPhone detection.

As Dan mentions, the iCloud binding is also reliably used by many on this forum.


(Juan) #5

Thanks.

I will read carefully the documentation, but I have already done before, but I am missing something.

It is mandatory to install the arping tools, right? to work properly with the Network binding?


(Rich Koshak) #6

Yes you must install the arping tools. And you need to work with the timeout parameters to get reliable detection which is discussed in the forum threads.


(Juan) #7

Something I am missing, I have installed arping, and I am modifying the timeout, but does not work properly.

Thing:

Thing network:pingdevice:phone [ hostname="192.168.2.104", port="0", retry="50", timeout="7000", refresh_interval="10000", use_system_ping="false" ]

Items:

Switch OUT_HOME "AWAY"   { channel="network:pingdevice:phone:online" }

At the end, when the phone enter in sleep mode, after the all retries and timeout, the status change to OFF

I have tried untill this setup:

Retry=50
Timeout=7000
refresh=10000

I have typed this in terminal:

arping -c 1 -I eth0 192.168.1.113

When phone is up, I receive answer, but when is in sleep mode/locked I got:

ARPING 192.168.2.104 from 192.168.2.123 eth0
Sent 1 probes (1 broadcast(s))
Received 0 response(s)

I have read this topic: Network Binding - iPhone Presence Detection - arping Configuration question

And I do not understand the idea to modify the Timeout. It does not matter if OH detect the device off after 2 minutes or after 30 minutes, at the end will be detected as OFF when the device is conected to the WIFI… so this will not work…


(Rich Koshak) #8

I don’t use this for iPhone but I do know this. Hundreds of users are successfully using the Network binding with arping for iPhone detection. Beyond that I can’t offer technical advice as I don’t use it.

But I don’t think the Network binding uses arping alone. Back when we had to do this by scratch we used to have to send a special packet using hping3 first which forces the iPhone to wake up and then the arping catches the arp response from the iPhone after it is forced to wake up. Again, I’m not an expert but I think this is how it works. I think the Timeout parameter defines how long to wait for this special packet to be sent out again.


(Juan) #9

ok, this is the clue.

This is what I am missing. ok thanks for the answer. I will read carefully about it, and search info…


(Brian Long) #10

I’ve had pretty good luck using OwnTracks geofencing

The only time it misses is when my iPhone is under 5% battery other than that it’s been flawless


(Juan) #11

Thanks, I will have a look as well.


(kevin) #12

@grhosso I use a combination of OwnTracks, the Network binding, contacts on all exterior doors and a few PIR’s that also operate the lights.
Once a phone is detected, or a PIR trips my rules assume someone is home until a door is opened and closed. Then after a delay the rules check for a phone or PIR trip before setting the away state. I have a series of OwnTracks geofences that regulate the HVAC setpoint such that the temperature is normalised by the time I return home.
Recently I’ve found that I have to ignore OwnTracks updates when I am at home as iOS seems to give out incorrect locations when the phone is sleeping.
Someone once referred to this in the deep and distant past as a “Bee in a Box” scheme, basically meaning the Bee is always in a box until the door is open, after the door is closed you look to see if the Bee has remained


(Juan) #13

thanks.

I guess that OwnTrack works with MQTT right?


(kevin) #14

Its got its own binding, but yes it works over MQTT. A little fiddly to get set up, but worth it in the end. The community over there will help as long as you’ve had a go at following their instructions first


(Juan) #15

cool. I will have a look. Interesting. A combination of geofence and wifi, could be enough. I have a magnetic sensor at the main door and I will use them for the Presence in combination as well.

Regarding to the Networking binding, I have found this topic: iPhone Presence Detection with hping3 and ARP but it is from 2015. Is it working this procedure? I mean, I need a script to wake up my iphone every minute when is in sleep mode, otherwise, OH sees the iphone OFF and it seems that none at home.


(kevin) #16

The phone does wake up from time to time, maybe every 20 minutes when not in use and the network binding is very fast at detecting this, iPads work the same. Repeatedly waking it will shorten your battery life considerably. If you are happy to take the battery hit I’d give the procedure a try. I did look into the icloud binding as an OwnTracks replacement, but the location seemed to be out of date due to needing the phone to periodically wakeup, send its location to icloud, then the delay before the binding got the location from iCloud. Even OwnTracks can only wakeup periodically to send its location so I have more geofences than strictly necessary to ensure the “coming home, tweak the heating” messages get through.
Use the phone as a steering mechanism would be my suggestion, you could use bluetooth beacons attached to your keys if it must know you are there, and for this I’d recommend checking out https://www.happybubbles.tech/presence/detector but it will hammer your MQTT server several times a second, as it will detect ALL bluetooth broadcast messages and the Tile Bluetooth tags stop broadcasting when they connect to the app on the phone. Bluetooth beacons are very short range and have no broadcast security.


(Juan) #17

I understand. OwnTracks will shorten the battery as well, I guess you need to have running all time the location setting? or only while using the app?


(kevin) #18

They only recommend real time when using the app, the rest of the time I have it in “significant changes” mode where it uses Geofences and I haven’t noticed it draining my battery. Maximising battery life is why I don’t let anything continually use location services


(Rich Koshak) #19

This is what I use and it works. However, this script is exactly what the Network Binding arping configuration does. The only reason I use it is because arping was not available in the Docker Image until a few weeks ago and I’ve not upgraded yet.

MQTT was designed for use in an industrial SCADA context by the oil and gas industry. Given adequate resources, you would need something closer to hundreds of messages per second before you start to see problems.


(Juan) #20

I am using this script, and when I ejecut the script, work pretty fine:

  • when phone is conected to WIFI, I get devices detected.
  • when is not conected to wifi, device is not detected.

but within OH, always show me ON states even with the phone disconected from WIFI…, it seems that the rule does not work properly.


for i in `echo $DEVICES`; do
    # Change dev and eth0 if needed
    ip neigh flush dev eth0 $i
    hping3 -2 -c 10 -p 5353 -i u1 $i -q >/dev/null 2>&1
    sleep 1
    # Only arp specific device, grep for a mac-address
    status=`arp -an $i | awk '{print $4}' | grep "..:..:..:..:..:.."`
    statusMessage="OFF"
    #A mac will be 17 characters including the ":"
    if [ ${#status} -eq 17 ]; then
        echo "Phone $i is detected!"
        statusMessage="ON"
    else
        echo "Phone $i is not present"
        statusMessage="OFF"
    fi
done```


This is my rule file:


```var Number IphoneCounter = 0
val Number MaxCounter = 18

rule "Execute script IphonePresence"
when
    Time cron "0 * * * * ?"
then
    var String IphoneExec = executeCommandLine("sudo@@bash@@/opt/IphoneD.sh", 5000)
    Iphone.postUpdate(IphoneExec)
end

rule "Determine presence Iphone"
when 
	Item Iphone received update
then {
	if(Iphone.state == "OFF") {
		IphoneCounter = IphoneCounter + 1
		if(IphoneCounter > MaxCounter) {
			Iphone_Sw.sendCommand(OFF)
			IphoneCounter = 0	
			logInfo("RULE","Iphone  counter reached threshold and sent command OFF. Counter set to 0")
		}
		else {
			logInfo("RULE","Counter Iphone threshold not yet reached. Counter set to " + IphoneCounter)
		}
	}
	else {
		Iphone_Sw.sendCommand(ON)	
		IphoneCounter = 0
		logInfo("RULE","Iphone_Sw received command ON. Counter reset to 0")
	}
	}
end```

(Juan) #21

Something wrong in the rule:

I got this log:

2018-06-21 18:26:02.944 [INFO ] [.eclipse.smarthome.model.script.RULE] - Iphone_Sw received command ON. Counter reset to 0

==> /var/log/openhab2/events.log <==

2018-06-21 18:27:01.379 [vent.ItemStateChangedEvent] - Iphone changed from Phone 192.168.2.104 is not present to Phone 192.168.2.104 is detected!

2018-06-21 18:27:01.393 [ome.event.ItemCommandEvent] - Item 'Iphone_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-06-21 18:27:01.396 [INFO ] [.eclipse.smarthome.model.script.RULE] - Iphone_Sw received command ON. Counter reset to 0

==> /var/log/openhab2/events.log <==

2018-06-21 18:28:01.379 [vent.ItemStateChangedEvent] - Iphone changed from Phone 192.168.2.104 is detected! to Phone 192.168.2.104 is not present

==> /var/log/openhab2/openhab.log <==

2018-06-21 18:28:01.396 [INFO ] [.eclipse.smarthome.model.script.RULE] - Iphone_Sw received command ON. Counter reset to 0

==> /var/log/openhab2/events.log <==

2018-06-21 18:28:01.397 [ome.event.ItemCommandEvent] - Item 'Iphone_Sw' received command ON


(Rich Koshak) #22

That script’s output is either

  • Phone $i is detected!
  • Phone $i is not present

where $i is presumably the device being searched for. You cannot pass these strings to a Switch Item. A Switch Item can only receive ON or OFF as commands or updates.


(Juan) #23

Great, Thank you. I got it. It seems now is working. I am testing…