So I recently got a raspberry pi and loaded openhab for my home automation system. After a few days (weeks?) of trying to figure it out, I finally got all of my lights set up. The only thing missing was good presence detection. I wanted the system to know when my wife and I were in the house. We didn’t want to carry anything extra on us, so I figured the iphone (which is always on us) would be perfect. Openhab has a network health configuration and a bluetooth configuration so I thought this would be great.
…It turns out that its not great. As I found out, the iphone has a “deep sleep mode” which turns off wifi and bluetooth after it is asleep for a few minutes. I looked everywhere on the internet, but nobody had an answer. After a long time, i think i may have figured it out. I made a bash script that wakes the phone with hping3, and then uses the arp command to find the iphone’s MAC. If the iphone is connected to wifi, it sends a command to openhab to turn off a switch. Arp is preloaded with raspian, but you will need to download hping3 (sudo apt-get install hping3). Here is the script.
#!/bin/bash
while true
do
sudo hping3 -2 -c 10 -p 5353 -i u1 your-ip
DEVICES=`arp -an | awk '{print $4}'`
if [[ $DEVICES = *MAC* ]]
then
curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "ON" http://Your-ip/rest/items/your-item/state
else
curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "OFF" http://your-ip/rest/items/your-item/state
fi
sleep 30
done
Just put in your mac address (instead of MAC), your ip (instead of your-ip) and your openhab switch name (instead of your-item) and run the script (sudo bash scriptname.sh)
It is similar to this solution. The problem is that I couldn’t get her solution to work for me, thats why I came up with this one. I also tried making a script that just used hping3, but it didn’t work either.
Hi Jonathan,
Thanks so much for your little script. With that and some modifications, I got finally my iphone detection to work.
I changed it, however, to be used by the http binding as follows:
Text item=MOB001_WLAN_N icon="Praesenz" valuecolor=[Anwesend="green",Abwesend="blue"]
iphonedetect.sh - placed in /data/OpenHab
!/bin/bash
declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 xxx.xxx.x.x -q >/dev/null 2>&1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="yy:yy:yy:yy:yy:yy"
if [[ ${DEVICES[*]} =~ $CHECK ]]
then
echo "Anwesend"
else
echo "Abwesend"
fi
```
with xxx.xxx.x.x ip of iphone in network
with yy:yy:yy:yy:yy:yy MAC adress in Network
Actually ignore my question. Your method is much better. Now I don’t have to continuously run two scripts in a terminal window on my pi. Also, it means that I can use a string instead of a switch, Thanks!
Hey, maybe you can help me. I’m actually having a problem running the script your way. The problem is, that I habe to run hping3 -2 -c 10 -p 5353 -i u1 xxx.xxx.x.x -q >/dev/null 2>&1 as root (so sudo hping3 -2 -c 10 -p 5353 -i u1 xxx.xxx.x.x -q >/dev/null 2>&1 ), and I cant do that within the exec binding of openhab. Any tips?
Hi Jonathan,
That was also my intention, just to run one script.
Problem I still encountered is, that hping3 could not wake up in many cases my and my wife’s iphone. I installed OnavoProject VPN app (free) which hinders the wifi to be turned off in deep sleep mode. Seems to work so far.
In regard to your second question:
I installed OpenHab in “root mode”.
However, maybe you can change the rights of the exec binding with chmod via putty or what so ever.
Advise if this helped.
Again thanks for posting your idea. Great job!
Cheers Björn
Jonathan I am encountering the same problem. Did you find any solution ? I tried to add user openhab to sudo but i am still not allowed to use sudo via “sudo visudo” and adding
Which os are you using and which user is openhab running at? I added the www-data user as you mentioned and when I use sudo hping3 in the script it results in a message that I am not allowed to run sudo
Are you sure that it works and exec binding uses www-data user? Did you check if hping3 work by removing the > /dev/null ?
Im using a raspberry pi, running jesse. To be honest, it didn’t work at first. I was so frustrated (by this and a few other things that werent working on my system), I wiped my raspi, reinstalled openhab, re-wrote the script, added www-data to visudo and it started working. I think one problem that I had was that when i installed openhab on my raspi, I used these directions
In order for my other computers to have SMB access to openhab (to more easily edit my files). My theory is that this messes up some of the permissions. When I reinstalled openhab, I didn’t do this step and it started working.
If i was better at this stuff, i probably wouldn’t have had to wipe and start over, but sadly I am not…
Could someone tell we with binding has to be enabled, because my iphonedecect.sh doesn’t fire up.
I’m using debian Jessie and openhab2.
Thanks in advance
It doesn’t need a binding. it works with HTTP PUT requests to a switch. You have to make sure that the item is iphonedetect.sh has the same name as the switch (or presence) in your home.items in openhab. If you post your version of the script and your home.items, ill let you know whats going wrong.
Thanks for your help.
here is my /opt/openhab/iphonedetect.sh script.
#!/bin/bash
echo “number one $(date)” >> /opt/openhab/ip1.txt
declare -a DEVICES
/usr/sbin/hping3 -2 -c 10 -p 5353 -i u1 192.168.0.59 -q >/dev/null 2>&1
DEVICES=/usr/sbin/arp -an | awk '{print $4}'
CHECK="54:e4:3a:2d:0a:d5"
if [[ ${DEVICES[*]} =~ $CHECK ]]
then
echo "available"
else
echo "away"
fi
I created 3 test script hoping to find my error.
my sitemap file:
Frame label=“Groupe Ossy” {
Text item=CurrentDate
//Text item=MOB001_WLAN_N icon=“Praesenz” valuecolor=[Anwesend=“green”,Abwesend=“blue”]
Text item=MOB001_WLAN_N icon="siren"
Text item=MOB002_WLAN_N icon="siren"
Text item=MOB003_WLAN_N icon="siren"
Text label=“Groupement Ossy” icon=“firstfloor” {
Switch item=Lights mappings=[OFF=“All Off”]
Group item=gHeating label="Heating"
Group item=gWindows label="Windows"
Text item=Temperature
}
and at last my item file:
//Iphone detect
// Working Iphone wifi presence locator
String MOB001_WLAN_N “Zu Hause [%s]” (gPresence) { exec="<[sudo /opt/openhab/iphonedetect.sh:60000:REGEX((.?))]" }
String MOB002_WLAN_N “Zu Hause2 [%s]” (gPresence) { exec="<[bash /opt/openhab/iphonedetect2.sh:60000:REGEX((.?))]" }
String MOB003_WLAN_N “Zu Hause3 [%s]” (gPresence) { exec="<[/opt/openhab/iphonedetect3.sh:60000:REGEX((.*?))]" }