I first want to put out there that I am not a programmer and I would consider myself someone who has benefited immensely from all the support in the Openhab community.
This tutorial describes how to detect the presence of an iPhone if it is connected to your home’s wifi network.
This solution is based on iPhone Presence Detection with hping3 and ARP
The major difference being this solution is aimed at a Windows host.
I am currently running Openhab 2 build 887 on a Windows 8.1 machine and an iPhone 6 (fixed ip on my home network)
I wanted take my home automation to the next level and implement presence detection.
I implemented Owntracks presence detection using the free and secure CloudMQTT broker service.
Unfortunately when the iphone goes to sleep, my location drifts from home and falsely reports me away.
So like so many others I have gone down the rabbit hole of implementing some sort of home network “ping” on the iphone.
Since I am not a proficient programmer, I struggled with implementing the hping3 script on a windows machine.
So I went in a slightly different direction
I installed iTunes and the associated services (Bonjour)
I then installed nmap
I wrote the following simple bat file and dropped it into the root directory of openhab.
(so I can use exec to call the batch file) The script needs administrative privileges.
@echo off
CD\
CD Program Files (x86)
CD nmap
arp -d 192.168.1.7
nmap -sU -p 5353 -Pn --script dns-service-discovery 192.168.1.7 >c:\openhab\phone.txt
findstr /R /N "^" c:\openhab\phone.txt| find /C ":" >c:\openhab\temp.txt
type C:\openhab\temp.txt
I think (?) i am leveraging the bonjour zero config discovery functionality of the iPhone using the predefined nmap script that quires port 5353 for available services.
This quire evokes three responses,
The first looks like this
Starting Nmap 7.40 ( https://nmap.org ) at 2017-04-29 09:54 Central Daylight Time
Nmap scan report for 192.168.1.7
Host is up (0.19s latency).
PORT STATE SERVICE
5353/udp open zeroconf
| dns-service-discovery:
| 32498/tcp apple-mobdev2
|_ Address=192.168.1.7 fe80:0:0:0:8a2:e3c0:579b:dfcc
MAC Address: 90:60:F1:63:A8:33 (Apple)
Nmap done: 1 IP address (1 host up) scanned in 1.52 seconds
The second looks like this
Starting Nmap 7.40 ( https://nmap.org ) at 2017-04-29 10:48 Central Daylight Time
Nmap scan report for 192.168.1.7
Host is up (0.082s latency).
PORT STATE SERVICE
5353/udp open zeroconf
MAC Address: 90:60:F1:63:A8:33 (Apple)
Nmap done: 1 IP address (1 host up) scanned in 3.42 seconds
The third response looks like this
Starting Nmap 7.40 ( https://nmap.org ) at 2017-04-29 09:59 Central Daylight Time
Nmap done: 1 IP address (0 hosts up) scanned in 1.47 seconds
I then setup an exec “thing” to call the script
I then created the following rules to determine presence
var Number IphoneCounter = 0
val Number MaxCounter = 3
rule "Log Presence"
when
Item Iphone_Sw changed or
Item Presence_PhoneMqttHome changed
then
logInfo("Alarm", "Owntracks Presence = " + Presence_PhoneMqttHome.state)
logInfo("Alarm", "Network Presence = " + Iphone_Sw.state)
end
rule "Execute script IphonePresence"
when
Time cron "0 * * * * ?"
then
sendCommand(IPhone_Ping_Running, ON)
logInfo("Alarm", "iPhone Ping= " + IPhone_Ping_Output.state)
sendCommand(IPhone_Ping_Running, OFF)
end
rule "Determine presence Iphone"
when
Item IPhone_Ping_Output received update
then {
if(IPhone_Ping_Output.state == "3") {
IphoneCounter = IphoneCounter + 1
if(IphoneCounter == MaxCounter) {
Iphone_Sw.sendCommand(OFF)
IphoneCounter = MaxCounter -1
logInfo("Alarm", "iPhone @ Home = " + Iphone_Sw.state)
}
else {
logInfo("Alarm", "iPhone Counter= " + IphoneCounter)
}
}
else {
Iphone_Sw.sendCommand(ON)
IphoneCounter = 0
logInfo("Alarm", "iPhone @ Home = " + Iphone_Sw.state)
}
}
end
rule "Determine Presence"
when
Item Iphone_Sw received command or
Item Presence_PhoneMqttHome received command
then {
if ((Iphone_Sw.state == ON) || (Presence_PhoneMqttHome.state == ON)) {
Presence_Detection.sendCommand(ON)
logInfo("Alarm", "Kevin @ Home = " + Presence_Detection.state)
}
if ((Iphone_Sw.state == OFF) && (Presence_PhoneMqttHome.state == OFF)) {
Presence_Detection.sendCommand(OFF)
logInfo("Alarm", "Kevin @ Home = " + Presence_Detection.state)
}
}
end
The results so far look promising, I am on a 1 minute ping and a 3 minute away detection.
Battery life does not appear to be adversely affected, however it has only been running stable for a couple of days.
Next step is to understand if installed iTunes services are needed.
One important note: I am not running an apple sleep proxy server on my home network (AirPlay, AppleTv or Mac).
I am not sure how they would affect this hack.