BEGINNER Guide to iPhone Presence?

OK, you really are a beginner.

I recommend you spend some time reading and learning about how to get around on Linux, or run openHAB on an OS that you are more familiar with.

  • On your Raspberry Pi you are running a Linux based operating system (OS) called Raspbian (probably the Jessy version since that is the latest). Just like Windows 10 and OSX, Raspbian is a generic platform upon which you run programs.

  • openHAB is a program that runs on top of, in this case, the Raspbian OS. It is a Java based program which lets it run on most OS’s, but since you are working with a Raspberry Pi with Raspbian Jessy I’ll limit the discussion to that.

  • openHAB supports addons which allow it to support communicating with different bindings. Two different types of these addons are called bindings and actions. Bindings allow one to represent something in the real world as an Item within openHAB (e.g. represent a zwave switch as a Switch in openHAB). Actions allow one to call something in the real world directly.

  • One such binding is the Exec binding. This allows one to represent a command entered on a command line as an Item. For example, have it execute a bash script when a Switch is toggled. The command line string and the shell script are executed by the OS completely outside of openHAB.

  • Similarly one such action is the executCommandLine action. This allows one to execute a command entered on a command line directly from within a Rule.

  • A Rule is a bit of code written in a language that openHAB knows how to understand which tells it to do something in response to some event. Your line above is a valid line calling executeCommandLine from a Rule that is executed by openHAB.

  • When you Putty/ssh login to the Raspberry Pi, you are interacting with Raspbian. openHAB may not even be running! So you must communicate with the command line you are using (almost certainly bash) in a way that it understands. val results = executeCommandLine("/etc/openhab/configurations/scripts/iPhoneDetect.script", 5000) logInfo("iPhone", results) is nonsensical to bash. That is the openHAB Rule’s language.

  • What executeCommandLine does is pass the contents of the first String on to the operating system and says "Hey, execute this and tell me what it says.

  • So, when you Putty/ssh over to your Pi to execute this script yourself, all you type in is that first String.

/etc/openhab/configurations/scripts/iPhoneDetect.script

All of the above is really basic computer knowledge and is understood to be known by users of openHAB on Linux. Based on this I would not recommend moving forward with use of the Exec binding or the executeCommandLine action until you gain a bit more knowledge and experience about how Linux works. Or run openHAB on a Windows or OSX machine upon which you are more familiar in which case the string you would pass to executeCommandLine would be the same thing you would type in a CMD window.

1 Like

As always: thank you for your reply! And more so for actually replying, an not just keeping quiet, because it was on those newbie question (like many others would do).

Unfortunately (or fortunately) I can relate to the OP all too well.
When I started, I ran into exactly the same problems… the major one being “incomplete” information to arrive at the desired result being shown.
I also understand that it is neither realistic nor reasonable feasible to explain from Adam & Eve how to detect a phone…

What would help is a prescribed way on how to set-up OH on Linux – with permanent top posts, on the forum like: Warning! Post should refer to the OH recommended process of installing OH on Linux… and a pointer to the wiki article.

I re-installed OH when I started, because the article I found useful at the time of getting started did not use the apt-get install process, thus resulting in OH files and directories not being where a standard (proper) install would expect these.

Maybe responses to newbie question should be to ask first: do you have the file XYZ in this directory; with XYZ referencing a file that would only be there when the proper installation process was used.
The reply should be: given the multitude of problems which will eventually occur due to a non-standard install, we highly recommend to re-install OH, try the desired solution again, and if it still fails, come back to the forum.

May sound harsh, but I reckon – once this is consistently enforced – the forum will gain more value to anyone needing help with OH… and may – most likely – result in members to actually try to help.

I was never peeved off that I had to re-install; quite the opposite – I am now certain that my foundation is right, and can be ruled out as a source of error (unless a bug appears)…

As for common ‘knowledge’… starting with Linux is no mean feast for a beginner… IMHO it takes many days to even complete the simplest of tasks. BUT, your right, if you want to run things on Linux a basic understanding is required to even dare to go there.

I digress… I had no intention at present to do iPhone detection, but while I saw the post, I thought I give this approach a try and it worked eventually. However, it took a while to figure where to put the script, what rights to give it, and needed debug enabled to realise I pointed the .item to to the wrong path. :frowning:

Good luck to the OP! Be persistent, you will be rewarded with success.

I don’t disagree, but the installation instructions for both OH 1 and OH 2 and there are even special instructions for installation on the Raspberry Pi are actually fairly well documented. But OP didn’t have trouble installing OH, he/she had a fundamental misunderstanding about where the border is between OH and the OS.

@ThomDietrich is currently working out a documentation scheme with stickies in the forum. I’m not certain this is exactly what he is after given how much documentation already exists for installation in the official docs, but the conversation is worth having.

I completely understand the frustrations newcomers have when coming to an application as complex as openHAB but the developers and documenters have to draw the line somewhere and I’m firmly of the opinion that Linux fundamentals is beyond the scope of what should be documented in OH. Particularly since OH runs perfectly well on pretty much all the other platforms.

I do think perhaps some guidance and/or documented prerequisites (as in what knowledge users are assumed to possess) might be in order. Also, @ThomDietrich’s work with openHABian will go a long way towards making the Pi (and soon other platforms) easier to get up and running for the new user.

But there will always be a few bindings/actions which will require a greater amount of knowledge to use effectively no matter how easy openHAB becomes to install and configure. I’d put Exec, Serial, TCP/UDP, GPIO, and HTTP bindings in that category for sure. All things considered, I would not recommend any of these bindings to inexperienced users.

That is a challenge that I’ve not figured out a way to address. It is a clear problem because there are tons and tons of tutorials out there written by third parties of various vintages and almost none of them use apt-get. Even such authoritative sources as Makezine follow a “manual” wget based installation process (I posted a comment on that posting).

I’m not convinced a sticky will help this problem. There are (as linked above) well written instructions in the official docs for installation and people are not checking those before installing. Because these third party instructions use wget, most of these users don’t even visit the openhab website or this forum until after they installed and are having problems.

I agree, I should do this more. But it isn’t always clear who the newbies are (though in this case it is clear) and being too basic and basic in our first response can come across as condescending as well. Its a hard balance to reach. Though once the openHAB 2 documents are a bit more complete I do have planned in the back of my mind a troubleshooting document or wizard or the like to walk new users through the basic troubleshooting steps (is it running? are there errors in the log? is the addon installed? …).

I actually think the number of active helpers on this forum is pretty high when compared to other forums. Most have their specialties (e.g. chris jumps on zwave questions, watou is great with mqtt, and several folks help out newbie type questions, etc). Compared to most forums for OS projects of this nature, that is a really high level of participation. Most have only one or two active helpers. Is the OH forum as helpful as it could be? Probably not. But this is THE challenge with 100% volunteer projects like this.

Care to post how you got it to work as a tutorial? I don’t have an iPhone in the house (and the script doesn’t work on Android, I tried) so I can’t get any further than what I’ve already posted here. @ThomDietrich is leading an effort to get a bunch of tutorials like this into the forum under the Tutorials and Examples tag and this is a perfect candidate to get a good and complete example written up.

1 Like

As usual: good points…

Did not try to say: nobody is helping :slight_smile:

As for the ‘sticky’ … maybe there should be one in each forum: "Did you install OH with apt-get? If not you will have problems! … and then describe what and why, and how… pointing to links etc.

Convinced or not… I am sure that a notice put up, will persuade a bunch of newbies to re-install their OH system… and try again.

this is not about multiple ways to skin a cat, but about consistency, as a prerequisite for proper support. Linux is unfortunately known for its ability to solve problems in far too many ways. RO like the saying: if we have a problem, we route around it :slight_smile:

Happy to post a tutorial… what is the process?

On another note for this particular solution: It takes a while for a switched off phone to be removed from the arp cache.
The arp command will list all IPs / MACs it has in its cache, even if the phone is switched off it will be shown as present, until the arp entry is removed – which is usually 60 seconds.
find out with cat /proc/sys/net/ipv4/neigh/default/gc_stale_time

Hey guys, interesting discussion you have going on here. :slight_smile:

With the new docs still being partially incomplete, I can understand that not all (OH2) users are going with it. I do not miss to annoy users with a link to them ^^

I’m sadly a bit behind in the ambition to make the forum a bit more guided. As the two of you already discussed, the forum can definitely need some guidance in the individual categories. I’ll very soon start creating pinned posts that contain a description what the category is for (and not!), important links and an FAQ section.

I would also like to establish some kind of competence awareness among the moderators, allowing them to invite the right people to threads they personally can not help with.

The Tutorials and Examples is especially important to me, as they will considerably help new users getting started and understanding concepts and possibilities with OH(2). Judging by questions often asked regarding rules and sitemaps, this is definitely an area we should improve in.

As @rlkoshak pointed out, that’s a problem because (a) openHAB is available for all platforms, (b) at least for the next 3-5 months we are still dealing with openHAB1.8 stable and openHAB2 beta setups and (c) I wouldn’t say that the manually installed setup is any less functional - it’s just a question of how good the system is managed. (That said, I would recommend an apt installation.)

Earlier you discussed, that it would be nice to give more guidance to unexperienced users, especially in the first forum response. In my personal experience, the more you take people by their hand and show them the way, the less they look to the left and right… It is very important to give helpful and complete answers but in the same moment empower the user to find the solution by himself. Making a few assumptions (like with @Max_G 's XYZ example), giving some advice and posting relevant links is probably a good practice.

Regarding linux, I wanted to point out to the links I included here and here (right beneath TOC). If something like this found its way in the FAQs, we should starting to reference that section instead.

In general, you should start of with a short description, pros and cons of your solution, requirements and a screenshot. Then describe the setup process. Remember links for when things go south. The last thing I wanted to write in the sticky is that it would be great if the creator of the first post would incorporate things discussed in the comments (improvements, alternatives, potential problems)…

Related to the presence topic, I think it would be good to also reference the already discussed alternatives and give comparison as far as you can. That would be the first question I myself would have regarding that topic which seems to be solvable in 10 different ways.

Procedurally just create a new posting in the Tutorials and Examples section. Use Thom’s Influx+Grafana Tutorial and an example to follow. And reference this thread and the original thread. You might also come back to both of these threads and add a posting pointing to the new tutorial.

Once written, this tutorial will be the authoritative source for how to do it which we should all reference when iPhone presence detection comes up.

Yes, I have an RPi3 running the Jessie version of Raspbian.

Yes, I have OpenHAB installed and it starts on boot up. I have the Hue bindings installed and can turn on/off lights as well as set their color. I also have the bindings below installed (all version 1.8.3):

astro, exec, harmonyhub, http, hue, ntp, weather, zwave

The below io:
hamin (v. 1.7.0)
myopenhab

The persistence below:
logging
rrd4j

OK, I understand.

Yep, I have a rule to update the last weather update time.

I have it begin on startup. I have Samba and Designer installed on my Win10 computer and will use putty/designer/Samba to execute commands, modify OpenHAB files and save/move files.

OK, I ran this and now get the error below:

-bash: /etc/openhab/configurations/scripts/iPhoneDetect.script: /bin/bash^M: bad interpreter: No such file or directory

It would be very helpful to have a tutorial! I always compare learning OpenHAB to cooking. You can have a step that says “chop 1 C of onion”, but if you don’t know what an onion is or where to buy one, you’re gonna have a hard time! :slight_smile:

That being said, I’ve found the multiple people on this forum MORE than willing to try to help a girl who is just trying to move from Vera to OpenHAB.

Thanks!

That bash error is an unfortunate but common problem. On Windows newlines are represented with two characters “\r\n”. On Linux/Unix/OSX only one character is used: “\n”. A lot of times this difference doesn’t matter but this is one of the cases where it does matter.

You can run dos2unix on the script and it will strip the “\r” (seen as “^M”) characters out of your file and bash should no longer have any problems with it.

dos2unix iPhoneDetect.script

If you intend on editing files on Windows using notepad this will be a perennial problem. I recommend getting Notepad++ instead of using the built in Notepad program, or edit the files on the Raspberry Pi itself using nano.

This is just a quibble and not really relevant to anything functional, but by convention a shell script such as this typically ends in “.sh”, not “.script”.

Your response is a bit of a non-sequitur. The primary point I want to make sure you understand is that when you putty to your Pi, you are interacting with Raspbian, not openHAB. So any commands you issue must be in a format that Raspbian (really bash) understands. See the following link for a bit on how to work on the command line.

http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/

Some important commands you will want to know when working with openHAB include:

  • tail -f /var/log/openhab/openhab.log : lets you see what openHAB is logging as it is running
  • cd : change directory
  • ls : get a list of files
  • ls -a : show even hidden files and directories (i.e. files that start with “.”)
  • man <cmd> : where <cmd> is the name of a command you want to run (e.g. ls), shows the usage/help information for that command. Use man to learn how to use the commands that follow
  • chmod : change the permissions on a file
  • sudo : run the command that follows as root
  • systemctl : start/stop a running service, e.g. sudo systemctl restart openhab
  • cp : copy files
  • nano : command line text editor
  • dos2unix : strips the ^M characters from a text file produced on Windows
1 Like

I’m using the REST API in combination with the MAKER plugin at IFTTT and it works fine for me. Make sure to have some firewall policy on your device :wink: The following screenshot shows an example, which works for me:

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.