Working Iphone wifi presence locator

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:

items:

String MOB001_WLAN_N "Zu Hause [%s]" (Praesenz) { exec="<[/data/OpenHab/iphonedetect.sh:60000:REGEX((.*?))]" }

sitemap:

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
2 Likes

I’m glad you like it! Why do you prefer the http binding?

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?

Maybe I dont understand you but why dont use sudo hping in the script as in the opening post?

And why can’t u use sudo in exex?

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

I have an issue to get the script output into the variable. The script runs perfectly when i run it in bash. My item is

String phone_christin “%s” (gMobiles) { exec="<[/etc/openhab/configurations/iphonedetect.sh:300000:REGEX((.*?))]" }

but the value is always “%s” not “Anwesend” or “Abwesend”

Edit: my fault, has to be [%s]

try

String phone_christin “[%s]” (gMobiles) { exec="<[bash /etc/openhab/configurations/iphonedetect.sh:300000:REGEX((.*?))]" }

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

openhab ALL=(ALL:ALL) ALL

I fixed it. This is what to do

Add
www-data ALL=NOPASSWD: ALL

to the bottom of visudo

www-data is the user that the exec program uses to execute commands.

Hope this helps

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

http://www.makeuseof.com/tag/getting-started-openhab-home-automation-raspberry-pi/

This calls for me to do

sudo chown -hR openhab:openhab /etc/openhab
sudo chown -hR openhab:openhab /usr/share/openhab

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…

Hi there

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

Best regards

Ossy

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.

Hallo Jonathan

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((.*?))]" }

echo is not writing in my /opt/openhab/ip1.txt :frowning:

Thanks a lot
best regards

Ossy

Short question, you do have the exec binding installed, don’t u

what in the world are “french commas”??

` is a french comma. it has to be

DEVICES=`/usr/sbin/arp -an | awk ‘{print $4}’`

ah of course - well I used $( … ) anyway same difference I guess.

A few notes on the suggested exec script though, I noticed that hping3 could only be run with privileged access (sudo/root) so I added sudo into the command line. Also, arp displays all mac addresses in lowercase - which explained why I couldn’t get a positive response.