script for presence detection of a phone

Must be cleaned all topics go to the right subforum
mrEd
Posts: 3
Joined: Wednesday 30 November 2016 23:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

script for presence detection of a phone

Postby mrEd » Thursday 01 December 2016 0:17

I have written a script for presence detection based on WiFi presence that also works for iPhone (ios 10.1.1).

The script checks the ARP table (Address Resolution Protocol) of the Raspberry Pi for the presence of a phone. If the phone is in the same network as the Raspberry Pi, it shows up in the ARP table if there is traffic from the Pi to the phone. Other scripts I found use ping, but that does not work very well for iPhone because an iPhone does not respond to ping when it is inactive. I have tested the ARP method, and I have found it to be more reliable when combined with ping. I think this is because when the Pi pings the phone, it requests the MAC address of the phone on the network and stores it when it is replied in the ARP-table.

The script sets the dummy device to "On" if the phone has been seen on any moment in a given timeframe, and switches of the device if the device has not been seen during the give timeframe.

The setup consists of the following:
1. A dummy switch device in domoticz;
2. The script itself;
3. An entry in crontab that runs the script every minute.

First step is to create the dummy device in Domoticz via Setup > Hardware > Add device.
Check in devices what is the Idx number of the device, you will need this later on.

Second step is to create the scriptfile.

Login to the raspberry Pi via SSH and go to the domoticz/scripts directory. Usually the full path is /home/pi/domoticz/scripts, but that depends on your local setup. Please make sure that the directory you use is in the variable dir in the script in the Variables section. In the variable section you must also add username and password for Domoticz and if you have set it, the passcode for the dummy device.

create the script file, give the script file execution rights and open the nano-editor so you can paste the script:

Code: Select all

touch arpdetect.sh
chmod +x arpdetect.sh
nano arpdetect.sh


The script is below:

Code: Select all

#!/bin/bash
# experiment for presence detection:  update dummy switch in Domoticz based on ARP result.
# will run every minute if setup in crontab
# Date: 2016-12-01

# parameters for calling script:
# 1: name of device
# 2: numeric ID of switchdevice in Domoticz
# 3: number of minutes after which away is detected (on is always set directly)
# example: ./arpdetect phone 72 10

# Variables
dir="/home/pi/domoticz/scripts"
username=""
password=""
passcode="pinokkio"

# do not change below line
#-----------------------------------

# check correct number of arguments
if [ "$#" -ne 3 ] ; then
  echo "Usage: $0 devicename #domoticzdevice #minutes" >&2
  exit 1
fi
period=$(expr $3 \* 60)
now=$(date +%s)
limit=$(expr $now - $period)
result=$(/usr/sbin/arp -a $1)
readdate=$(date)
# first do ping to make sure ARP is updated
/bin/ping -q -c 1 $1

# check if devicename exists in ARP table
if [[ ! "$result" =~ "$1"  ]] ; then
echo "devicename $1 does not exist"  >&2
exit 1
fi
# determine last detection:
# if devicename is in ARP table then last detection is now,
# otherwise read from file
if [[ $result =~ "ether" ]] ; then
lastdetect=$now
echo $now $result $readdate > $dir/lastdetect_$1.log
else
lastdetect=$(/usr/bin/awk '{print $1}' $dir/lastdetect_$1.log)
fi

# read current switch status
/usr/bin/curl -s "http://127.0.0.1:8080/json.htm?type=devices&username=“$username”&password=“$password"&rid="$2"" | /bin/grep '"Status" : Off",'
switchon=$?

# if device has not been present in the requested timeframe and switch is on then switch it off.
if [[ $limit -ge $lastdetect  &&  $switchon -eq 1 ]] ; then
/usr/bin/curl "http://127.0.0.1:8080/json.htm?type=command&username="$username"&password="$password"&param=switchlight&idx="$2"&switchcmd=Off&passcode="$passcode"";
fi

# if lastdetect in the requested timeframe and switch is off then switch it on.
if [[ $lastdetect -gt $limit  &&  $switchon -eq 0 ]] ; then
/usr/bin/curl "http://127.0.0.1:8080/json.htm?type=command&username="$username"&password="$password"&param=switchlight&idx="$2"&switchcmd=On&passcode="$passcode"";
fi


Some remarks on the script:
- It is assumed the there is no username/password necessary for Domoticz. If you have setup Domoticz with password protection, you should add these to the variables to the script.
- If you want to prevent the dummy devices from being set via the Domoticz website, you can use passcode protection. In that case you must add the passcode to the variables.

You call the script from the commandline with three parameters:
1. The name or IP-address of the phone you want to use for presence detection. If you want to find out your name, just type arp on the command line, you might recognize your phone :D. On my system the arp result looks like this:

Code: Select all

Address                  HWtype  HWaddress           Flags Mask            Iface
iPad                     ether   4c:32:75:4f:78:a2   C                     eth0
10.0.0.69                ether   00:21:9b:e0:56:0e   C                     eth0
10.0.0.102                       (incomplete)                              eth0
10.0.0.137               ether   d8:50:e6:33:1e:2f   C                     eth0
DiskStation              ether   00:11:32:37:ab:76   C                     eth0
iMac                     ether   00:23:df:99:59:5e   C                     eth0
10.0.0.73                ether   00:21:9b:e0:56:0e   C                     eth0
iPhone                   ether   00:6d:52:78:39:b4   C                     eth0
android-b0150a3c1e62016  ether   10:a5:d0:23:0d:d1   C                     eth0
router.asus.com          ether   08:60:6e:ba:17:58   C                     eth0


If you want use the iPhone for presence, that exact name is what you should use.

2. the idx of the created dummy device.

3. the period between the latest time your phone is detected and the moment you want the switch the dummy device off in minutes.

You can test the script by calling it from the command line by typing ./arpdetect.sh <name of device on network> <idx> <#minutes>.

Example: ./arpdetect.sh iPhone 72 10

This searches for the iPhone and will switch off device 72 after 10 minutes.

You will see that after running the script a file is created in the directory with the name lastdetect_<device>.log. This contains the last time the device is detected, both in Epoch format and human readable format.

If the script works, you must setup the crontab to run the script every minute.

type

Code: Select all

crontab -e

add the line below to your crontab file (if this is the first time you use crontab, select nano for editor, it is most user friendly.

Code: Select all

 * * * * * /home/pi/domoticz/scripts/arpdetect.sh iphone 72 10

of course use the correct data for your personal situation.

Check if the script runs every minute by looking at the logfile.

Hope this is useful for someone.
Last edited by mrEd on Thursday 01 December 2016 22:46, edited 2 times in total.

digisaster
Posts: 5
Joined: Monday 28 November 2016 23:40
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby digisaster » Thursday 01 December 2016 0:54

Thanks for this script, for me it useful because i just wanted to start making my first script with domoticz.

I will try to test this but i already have some questions.
If i look on the output of tshark in monitormode then it will also grab all macnummers of devices who are not connected to the wifi.
Some mac addresses are fake because of the mac randomize functions on some os , but most of them reveal the real MAC number.
If you use this kind of detection it would be more reliable.

I have no idea if this would be easy to implement.

Egregius
Posts: 2059
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Beitem, BE
Contact:

Re: script for presence detection of a phone

Postby Egregius » Thursday 01 December 2016 6:55

How long are you using this?
I tried arp once but devices didn't dissappear or showed in the table as expected.

User avatar
jannl
Posts: 724
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Geleen
Contact:

Re: script for presence detection of a phone

Postby jannl » Thursday 01 December 2016 7:44

Is your pi on the same wifi network as your phones? When it isn't the arp table of the pi won't have entries for the phones.

Egregius
Posts: 2059
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Beitem, BE
Contact:

Re: script for presence detection of a phone

Postby Egregius » Thursday 01 December 2016 8:15

I used mi RPi with Pi-Hole for the test.
Mac address is instantly in the list because it is set as DNS server on my phone.
It worked flawless as long as I switched wifi on/off on my phone but not when leaving the signal.
I had a script that checked every 2 seconds the arp table so response was very good.

User avatar
jannl
Posts: 724
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Geleen
Contact:

Re: script for presence detection of a phone

Postby jannl » Thursday 01 December 2016 16:19

arp table is only filled with mac-addresses on the same collision domain. Better would be to query the wifi accesspoint for this, as is described in the wike or the forum as far as I remember.

mrEd
Posts: 3
Joined: Wednesday 30 November 2016 23:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby mrEd » Friday 02 December 2016 18:09

You are right. I have now added one ping to force the Pi to refresh its ARP-table.

mrEd
Posts: 3
Joined: Wednesday 30 November 2016 23:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby mrEd » Friday 02 December 2016 18:33

Egregius wrote:How long are you using this?
I tried arp once but devices didn't dissappear or showed in the table as expected.


I force the Pi to refresh it's ARP-table by sending out one ping. Even if the phone is not responding to the ping, it does respond to ARP requests. If I try the ping, first thing the system does is send the Ping to the MAC-address it has in its ARP-table. When the phone does not respond within the time-out, the system sends out an ARP-request. I see that an iPhone, even if it not used, always responds to the ARP-request. After several (it is configurable, but not very clear to me how) fruitless attempts at sending an ARP request, the device is set as <incomplete> in the ARP table.

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 11:49

Trying to execute the script (./arpdetect.sh SamsungS5plus.folst.eu 468 1), but it ends up in error:
-bash: ./arpdetect.sh: /bin/bash^M: bad interpreter: No such file or directory

Because I use Linux (Ubuntu) and no Pi, I expect that will cause the error but I'm unable (noob) to get it working. Could someone pinpoint me to a solution?

Thanks.

User avatar
jannl
Posts: 724
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Geleen
Contact:

Re: script for presence detection of a phone

Postby jannl » Wednesday 07 December 2016 12:08

Looks like /bin/bash is not the correct path, please check where bash is in unbuntu. And may get rid of the ^M, which is awindows newline character

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 12:48

Hi Jannl,

Bash is in /bin in Ubuntu. Can't find any evidence of a ^M in the script.

Recreated the script (copy paste) using a Ubuntu laptop, no luck so far. I do however see some strange 'color behavior' in gedit. the last part where the switch status is read, in gedit all is colored pink meaning it is just text..... When fiddling with "" the part below is recognized as code again but I don't know if this is on purpose or not....

User avatar
jannl
Posts: 724
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Geleen
Contact:

Re: script for presence detection of a phone

Postby jannl » Wednesday 07 December 2016 15:35

Does the script have the execute bit set?

ls -l should shot something like: -r-xr-xrwx or so

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 16:07

Think so: rights are -rwx--x--x

I did the chmod +x thing mentioned above

Egregius
Posts: 2059
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Beitem, BE
Contact:

Re: script for presence detection of a phone

Postby Egregius » Wednesday 07 December 2016 16:40

With -rwx--x--x you have to be sure that the executor of the script is the owner.
Ootherwise, try chmod 777 to give everyone execute rights. After it runs you can set it back to a stricter level.

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 18:42

Changed rights to 777 (-rwxrwxrwx), tried again, no luck, same error.

I'm lost.....

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 18:52

...NOT LOST :D

tried some Google and found this:

Code: Select all

sed -i -e 's/\r$//' scriptname.sh


I don't know what it actually does but it has indeed something to do with Windows style script saving (<- which is odd, because I did re-create the script on a Linux laptop with copy-paste from the forum). Anyway, since computers don't make mistakes, only users do, I obviously did something wrong. It does work now....

Thanks for all the helping guys, much appreciated!

EDIT: by the way, if you happen to not use the default port for Domoticz, one has to change the port in the URLs as well.

Ray-Man
Posts: 36
Joined: Friday 12 December 2014 20:03
Target OS: Linux
Domoticz version: 3.x
Location: Netherlands (near Zwolle)
Contact:

Re: script for presence detection of a phone

Postby Ray-Man » Wednesday 07 December 2016 21:37

There is a typo in the script:
/usr/bin/curl -s "http://127.0.0.1:8080/json.htm?type=devices&username=“$username”&password=“$password"&rid="$2"" | /bin/grep '"Status" : Off",'

Should be:
/usr/bin/curl -s "http://127.0.0.1:8080/json.htm?type=devices&username=“$username”&password=“$password"&rid="$2"" | /bin/grep '"Status" : "Off",'

buxol
Posts: 16
Joined: Saturday 02 January 2016 0:21
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby buxol » Wednesday 14 December 2016 0:26

mrEd wrote:I see that an iPhone, even if it not used, always responds to the ARP-request.

As I understand the arp -a command only checks the (local cached) arp table of your machine, the phone is not involved.
You can turn off your phone and still get a result out of the arp -a once it is in the arp table.

Egregius
Posts: 2059
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Beitem, BE
Contact:

Re: script for presence detection of a phone

Postby Egregius » Wednesday 14 December 2016 0:57

My experience was that switching wifi off/on give very stable and fast result in arp table.
Just leaving the wifi signal didn't.
Even did a trick to remove the arp entry as soon as I switched tha 'away' switch.
But then, when coming home, the arp wasn't filled fast enough to use it to switch the away switch off.
I'll stick with my manual switch on my floorplan ;)

buxol
Posts: 16
Joined: Saturday 02 January 2016 0:21
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby buxol » Wednesday 14 December 2016 9:48

If you are happy basing your detection on the TTL (time-to-live) of the record in your local arp table, then this will be the right method for you.
With my post I wanted to clarify, for other users trying to detect wifi presence, that the above quote from mrEd is technically incorrect. An ARP request is only a lookup in a local table and doesn't actually involve a response of the phone.


Return to “Scripts [cleaning in progress]”

Who is online

Users browsing this forum: No registered users and 5 guests