iPhone Presence Detection - A slightly different approach

Tags: #<Tag:0x00007f5c86b15438> #<Tag:0x00007f5c86b151e0> #<Tag:0x00007f5c86b15050>

(KPagano) #1

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 Program Files (x86)
CD nmap
arp -d
nmap -sU -p 5353 -Pn --script dns-service-discovery  >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
    Host is up (0.19s latency).
    5353/udp open  zeroconf
   |  dns-service-discovery: 
   |  32498/tcp apple-mobdev2
   |_    Address= 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
 Host is up (0.082s latency).
 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"


	Item Iphone_Sw changed or
	Item Presence_PhoneMqttHome changed


	logInfo("Alarm", "Owntracks Presence = " + Presence_PhoneMqttHome.state)
	logInfo("Alarm", "Network Presence = " + Iphone_Sw.state)

rule "Execute script IphonePresence"
    Time cron "0 * * * * ?"
	 sendCommand(IPhone_Ping_Running, ON)
	logInfo("Alarm", "iPhone Ping= " +  IPhone_Ping_Output.state)
	sendCommand(IPhone_Ping_Running, OFF)

rule "Determine presence Iphone"


	Item IPhone_Ping_Output received update

then {
	if(IPhone_Ping_Output.state == "3") {
		IphoneCounter = IphoneCounter + 1
		if(IphoneCounter == MaxCounter)	{
			IphoneCounter = MaxCounter -1
			logInfo("Alarm", "iPhone @ Home = " +  Iphone_Sw.state)
		else {
			logInfo("Alarm", "iPhone Counter= " + IphoneCounter)
	else {
		IphoneCounter = 0
		logInfo("Alarm", "iPhone @ Home = " +  Iphone_Sw.state)

rule "Determine Presence"


	Item Iphone_Sw received command or
	Item Presence_PhoneMqttHome received command

then {

	if ((Iphone_Sw.state == ON) || (Presence_PhoneMqttHome.state == ON))  {
		logInfo("Alarm", "Kevin @ Home = " +  Presence_Detection.state)

	if ((Iphone_Sw.state == OFF) && (Presence_PhoneMqttHome.state == OFF))	 {
		logInfo("Alarm", "Kevin @ Home = " +  Presence_Detection.state)


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.

Presence Detection with iPhone and OH2
( ) #2

Hey I might be wrong but from fist glance this sounds a lot like the solutions posted in this thread:

The biggest difference is that your solution is aimed at a windows host. You might want add this detail to your posting title.

Thanks for your contribution!!

(Dominic Bonneau) #3

Hey ! I’m really impressed with what you did I’m trying to do that since a while with my Mac … didn’t find the how to do it yet … I’m gonna post it like you if I figure it out !

(KPagano) #4

Thanks Dominic, It is my first contribution!

Does Nmap support OSX?

(Dominic Bonneau) #5

Actualy it does and I figure out how to ping udp port 5353 and it work !!

I just need to find 2 thing :

First the -sU command on Mac need to be run as root … it’s hard to execute a script as a root …

Second, I send the result to a test.txt but I don’t know how to transform the result into a true or false value ( or on and off or 1 and 0 etc… )

EDIT : also after 2 or 3 min the command seams to stop working and even if my wifi is on map say 0 host up

this is my command : nmap -sU -p 5353 -Pn --script dns-service-discovery