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) 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
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:
sudo apt-get install hping3
More information on hping: http://www.hping.org/.
Script
Create a file IphoneD.sh
with your favorite editor in the /opt
directory.
sudo nano /opt/IphoneD.sh
with the following code:
#!/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:
sudo chown openhab IphoneD.sh
sudo chmod 777 IphoneD.sh
Check if the script is executable with:
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.
sudo visudo
Make sure the last line looks like this (if your user for openHAB is openhab
)
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. 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.
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:
String Iphone "John Doe"
Switch Iphone_Sw "John Doe @ Home?"
Your sitemap now shows the presence of the Iphone. Sitemap:
Switch item=Iphone_Sw mappings=[OFF="Away", ON="Home"]