BEGINNER Guide to iPhone Presence?

Edit: final tutorial can be found here: Tutorial

Just an overview of what the outcome is of the above posts and some other posts on the forum.

To enable Iphone detection in openHAB 1.8.3 follow these steps (on RPI2):

  1. Make a directory to store scripts (if you don’t have this yet)
sudo mkdir /home/pi/scripts
  1. Create a file IphoneD.sh in this directory
sudo nano IphoneD.sh

with the following code:

#!/bin/bash
declare -a DEVICES
sudo ip neigh flush all
sudo hping3 -2 -c 10 -p 5353 -i u1 192.168.xxx.xxx -q >/dev/null 2>&1
sleep 1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="xx:xx:xx:xx:xx:xx"
if [[ $DEVICES[*]} =~ $CHECK ]];then
        echo "Present"
else
        echo "Away"
fi

Make sure you fill in your own local IP address of your Iphone as well as your MAC address (replace xx.xx.xx… in row 6). Make sure to use all lowercase for your MAC address.
Save and close the file.

  1. Now you need to change the permissions:
sudo chown 777 IphoneD.sh

Now check with:

ls -all

if the script is executable.

  1. Install hping3:
sudo apt-get install hping3
  1. Make a rule to check from time to time presence (every minute in this example):

As an example I made an item that shows the result of my rule:

String Iphone "John Doe @ Home? [%s]" 

This item is updated by this rule, trigger by a cron job (link for details: Cron Trigger tutorial

rule "IphonePresence"
when
    Time cron "0 0/1 * * * ?" 
then 
    var String IphoneExec = executeCommandLine("/home/pi/scripts/IphoneD.sh", 10000)
    if(Iphone.state != IphoneExec) {
        Iphone.postUpdate(IphoneExec)
    }
end
  1. Edit sudoers file with
sudo visudo

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

openhab ALL=(ALL) NOPASSWD: ALL
1 Like