# iPhone Presence Detection with hping3 and ARP

**URL:** <https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171>\
**Category:** Tutorials & Examples\
**Tags:** tutorial, exec, iphone\
**Created:** [December 13, 2016, 8:12pm UTC](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171 "2016-12-13T20:12:33Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Maurits28](https://community.openhab.org/user_avatar/community.openhab.org/maurits28/32/5925_2.png) [@Maurits28](https://community.openhab.org/u/Maurits28)\
**Post date:** [December 13, 2016, 8:12pm UTC](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171/1 "2016-12-13T20:12:33Z")

</div>

This tutorial describes how to detect the presence of an iPhone if it is connected to your home’s wifi network.

It is a summary of several posts on this forum, started by the script posted by @kemotsysinc ([main thread](https://community.openhab.org/t/beginner-guide-to-iphone-presence/14173)) and great additions from @Seaside later in this thread. Thanks for that!

- **Used Hardware:** iPhone 6 and Raspberry Pi 2
- **Used Software:** Rasbian with openHABian 2.0.0 and hping3
- **Necessary binding:** [Exec binding](https://github.com/openhab/openhab/wiki/Exec-Binding)

To enable iPhone detection follow these steps:

## Hping

To wake up the iPhone before checking with arp it’s presence, we use hping3 which pings via UDP to your iPhone:

```bash
sudo apt-get install hping3

```

More information on hping: [http://www.hping.org/](http://www.hping.org/).

## Script

Create a file `IphoneD.sh` with your favorite editor in the `/opt` directory.

```bash
sudo nano /opt/IphoneD.sh

```

with the following code:

```bash

#!/bin/bash

 #Enter your ip of your device here
DEVICES="10.1.1.20"

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

```

Make sure you fill in your own local IP address of your iPhone (replace `10.1.1.20`). Save and close the file. Btw thanks @rlkoshak for the `sleep 1` suggestion.

_Note that this script is adjusted from a multiple- to a single device script from @Seaside which you can find in post 3 of this thread, however to communicate status of multiple devices, mqtt messaging was added there._

Now continue to change the permissions of the script-file:

```bash
sudo chown openhab IphoneD.sh
sudo chmod 777 IphoneD.sh

```

Check if the script is executable with:

```bash
ls -all

```

Edit sudoers file, this enables the user `openhab` to run sudo commands without password. Enabling user `openhab` to run sudo commands can potentially be dangerous, do this at your own risk.

```bash
sudo visudo

```

Make sure the last line looks like this (if your user for openHAB is `openhab`)

```auto
openhab ALL=(ALL) NOPASSWD: /bin/ip, /bin/bash

```

## openHAB

Now we go to openHAB. We make a rule that is triggered by a cron job to check from time to time presence (every minute in this example). For details on cron triggers, see this [link](http://www.quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/crontrigger). And we add a rule to make sure we don’t detect ‘false non-presence’ if the Iphone does not react. Empirical evidence 😄 suggests every 19 minutes at least the Iphone answers once the request.

```php
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

```

Add these to your .items file:

```csv
String Iphone "John Doe"
Switch Iphone_Sw "John Doe @ Home?"

```

Your sitemap now shows the presence of the Iphone. Sitemap:

```php
Switch item=Iphone_Sw mappings=[OFF="Away", ON="Home"]

```

---

_[View the full topic](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171)._
