BEGINNER Guide to iPhone Presence?

No, I use Designer to edit any OpenHAB files since it will show line errors in the file, but I did have to create this one using Notepad. I do have Notepad++, so I’ll use it in the future. I recreated the script in Nano and can view it in Designer with no errors. When I run

/etc/openhab/configurations/scripts/iPhoneDetect.sh

I get:

-bash: /etc/openhab/configurations/scripts/iPhoneDetect.sh: Permission denied

I tried running it as “sudo /etc/openhab/configurations/scripts/iPhoneDetect.sh”

and I get the error “command not found”. I went to the exact directory to verify the script is there and still get the same error:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls
demo.script  iPhoneDetect.original  iPhoneDetect.sh  README
pi@raspberrypi:/etc/openhab/configurations/scripts $ iphoneDetect.sh
-bash: iphoneDetect.sh: command not found

Glad it works for you! I would try it, but as I said in my original post, I couldn’t get any items to show up in IFTTT and searching through the forums didn’t have a single post with the solution.

Please try a

ls -l

in your script directory and post the result here. We have so see if your script is executable (that was maybe the reason for the “permission denied”). Furthermore you made a second try starting the script with “iphoneDetect.sh”, but the correct name is “iPhoneDetect.sh” (with a capital P). Maybe that’s the reason why you became a “command not found”.

You can try to execute the script (while you are in the correct directory) with: ./iPhoneDetect.sh

I have a working iPhone detection using the exec-Binding and a Bash script, just like you want to do it. It was mainly a matter of permissions and user rights to get this working. But as Rich already mentioned, this is clearly no real beginner stuff. You should at first get a little more comfortable with Linux as an OS.

results of ls -l:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls -l
total 16
-rw-r--r-- 1 openhab openhab  36 May 22 17:24 demo.script
-rw-r--r-- 1 openhab openhab 245 Sep 17 11:57 iPhoneDetect.original
-rw-r--r-- 1 openhab openhab 231 Sep 19 08:27 iPhoneDetect.sh
-rw-r--r-- 1 openhab openhab 230 May 22 15:44 README

When I try to run ./iPhoneDetect.sh:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ./iPhoneDetect.sh
-bash: ./iPhoneDetect.sh: Permission denied

Ok. We’ll getting there. :wink:

Your scripts aren’t executable. You have to have an “x” before the “rw” and ideally also before the second and third “r”.
Try

chmod 777 iPhoneDetect.sh

It isn’t the perfect solution, but with this command we can rule out some other problems.
Then try again to execute the script. Maybe do another ls - l before to verify that the “x” have been set.

I had to use sudo chmod 777 iPhoneDetect.sh, it looks like it worked?

-rwxrwxrwx 1 openhab openhab 231 Sep 19 08:27 iPhoneDetect.sh

When I ran the script it returned “Absent” - which is progress!!

How do I permanently set the permissions on the script?

They are permanently set. The difference is: When you manually start the script (like you just did), you execute it as User “pi”. When Openhab executes the script with the exec binding, it executes the script with the user “openhab”. We have to make sure that this is allowed (which we did with chmod 777).

Next step: Please quote the current content of your iPhoneDetect.sh script. I want to compare it with my script (but I can’t do this until this evening when I am at home). Try to get this with using nano on your ssh shell (type: nano iPhoneDetect.sh).

Furthermore we have to take care that the included commands in the script (like hping3) also are allowed to be executed by the user openhab. We have to edit the /etc/sudoers file for this. But one step at a time…

You see: No rocket science, but quite a bunch of stuff to consider…

#!/bin/bash
declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 xxx.xx.x.xxx -q >/dev/null 2>&1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="my:mac:addr"
if [[ $DEVIES[*]} =~ $CHECK ]];then
	echo "Present"
else
	echo "Absent"
fi

xxx.xx.x.xxx is my iPhone’s IP address connected to my home wifi
my:mac:addr is my iPhone’s mac address when connected to my home wifi

Next step: Have you already installed hping3 like Max said:

Try it with simply executing the command hping3 on your ssh shell. It makes no sense but you can see if the program is installed. Otherwise install it with sudo apt-get install hping3.

And check if your mac address in the script is written in lower cases (aa:bb:cc instead of AA:BB:CC). For me it didn’t work with upper cases.

Next step (as mentioned before): Edit the /etc/sudoers file. Execute the following command on your shell:

sudo visudo

Now you have started an editor and opened the sudoers file. Scroll down to the very end of the file and add the following lines:

pi ALL=(ALL) NOPASSWD: ALL
openhab ALL = NOPASSWD: /usr/sbin/hping3
openhab ALL = NOPASSWD: /usr/sbin/arp

This allows the programs of the script to be executed by the user openhab. Save your changes and exit the editor.
CAUTION: This is a sensible file. Don’t mess up the file or modify/delete any of the other content.

Checking your script I found one (maybe two) problems:

  1. There is a typo. You wrote “DEVIES” in line 6. It has to be “DEVICES” (like in line 2 or 4). Don’t know where this typo comes from as Rich has written it correctly. Anyway…

  2. This could be unnecessary, but I have an additional “sudo” before hping3 (sudo hping3 -2 -c …).

Other than that, the script seems to be identical to mine. With the above mentioned extra steps this should lead to a correct output now.

OK, done.

Done.

Modified to lower case.

Corrected.

Done.

I rebooted my Pi after this and ran the script again and I got Permissions denied again. I ran the ls -l and saw the x’s were missing from the script so I knew the permissions were incorrect again. I ran the sudo chmod and then ran the script manually and got “present”!

I also checked the app and “To House” now states “Present” as well.

Sorry, my fault. As Rich mentioned, the file permissions in the folder you used for the script are resetted with every boot. This folder is not intended to be used for other files.

So move over the file to another directory (Max suggested one, mine are in /scripts). So make a directory with:

mkdir /scripts

Copy your sh script to this folder (I won’t describe how to, just google and learn some linux commands :wink: ) and check the permissions one more time. Adapt your item configuration to the new path and test again.

And then disable wifi on your phone and check if the state changes to absent. The script only executes once a minute, so the change is not visible immediately.

Awesome, thanks a million for everyone’s help!!

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

Nice and concise tutorial on how to make hping3 work. Thank you for posting.

I might make one suggestion to add a sleep between the hping3 and the arp call in IphoneD.sh. I’ve found on my machine at least that the script runs faster than arp packets fly across the network causing the script to only intermittently correctly report Present. Adding a sleep 1 in my case (much less would probably be adequate) makes it return the correct state every time.

Nice @Maurits28!
Now nothing is stopping you to just copy&paste the tutorial to an article at https://community.openhab.org/c/setup-configuration-and-use/tutorials-examples - That would be amazing :wink:

Be sure to check out the About Tutorials & Examples article.

I just made a tutorial here: Iphone presence.

Some things occurred which I could not explain, maybe you can explain to me:

  1. I inserted a hyperlink, first one works, second one only shows the text.
  2. I inserted link to another forum user, first one works, second one only shows the text
  3. I could not give ‘hard enters / line breaks’ at some point in the post, which makes it more difficult to read (e.g. step 3)

Thank you for the suggestion, I added it to the tutorial, and changed it in the post above.

Amazing. Okay it seems the original question was answered and the step-by-step tutorial is now available:

With that I will go ahead and close this thread. If there’s a reason against that, please do not hesitate to write me a personal message! :wink: