script for presence detection of a phone

Must be cleaned all topics go to the right subforum
zedd
Posts: 6
Joined: Monday 02 January 2017 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby zedd » Wednesday 04 January 2017 15:27

The solution to this is to use something that does request a reply from your phone (or another device) and will get a response whether the device active or not. Arp-requests are generally answered by all phones, so why not use this? Indeed, checking with 'arp', only performs a lookup in an existing table in the OS.
But, there is a tool called 'arping' which will send an arp-whois and report the response. Use it like this:

Code: Select all

$ sudo apt-get update
$ sudo apt-get install arping
$ sudo arping 192.168.1.1


I've altered the script from https://www.domoticz.com/wiki/Presence_detection to use arping as it is much more reliable AND will detect when a device is disconnected / leaves the network.
Note: I've installed Domoticz on a Raspberry Pi, so above mentioned method may or may not work for you. Arping is available on most OS's though.
Caveat: arping uses a system call that requires root-access, so you need to prepend 'sudo' to the command.

User avatar
jannl
Posts: 746
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 04 January 2017 16:46

I think also arping will give problems with Android's doze function (Android 6)

zedd
Posts: 6
Joined: Monday 02 January 2017 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby zedd » Wednesday 04 January 2017 22:32

To be honest: I haven't got a clue if that's the case or not.
Experience (meaning, the past few days) arping has worked flawlessly for me. Which doesn't mean that other versions than our Android 7 phones may provide different results.

User avatar
jannl
Posts: 746
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 05 January 2017 8:48

Place your android 7 phone on a flat surface, no charger attached and wait a few hours. See what happens.

Koensk
Posts: 27
Joined: Friday 25 September 2015 9:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby Koensk » Tuesday 10 January 2017 10:19

Anyway,
What's the working solution? I tried both ip-presence detection and this arp methode but both off them don't work.

With arp I get the following error

[email protected] ~/domoticz/scripts $ ./arpdetect.sh 192.168.0.110 635 10
PING 192.168.0.110 (192.168.0.110) 56(84) bytes of data.

--- 192.168.0.110 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 215.874/215.874/215.874/0.000 ms
./arpdetect.sh: line 60: unexpected EOF while looking for matching `"'

Anyone an idea or an other solution to detect if my phone is in the house or not?

Thx

ArnieO
Posts: 7
Joined: Sunday 19 March 2017 17:08
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Norway
Contact:

Re: script for presence detection of a phone

Postby ArnieO » Monday 20 March 2017 23:10

I get exactly the same error as what Koensk is describing.
What are we doing wrong?
Electronics engineer partly out of practice, having turned the skills into hobby recently. Playing with RPi, Arduinos, ESP8266s etc. Installed Nexa units in the house, which I want to control with Domoticz / RPi3 / RFXtrx433.

ArnieO
Posts: 7
Joined: Sunday 19 March 2017 17:08
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Norway
Contact:

Re: script for presence detection of a phone

Postby ArnieO » Monday 20 March 2017 23:45

SOLVED: There was an error with one or the <">-signs in line 50. Copy/paste issue, I assume.
Electronics engineer partly out of practice, having turned the skills into hobby recently. Playing with RPi, Arduinos, ESP8266s etc. Installed Nexa units in the house, which I want to control with Domoticz / RPi3 / RFXtrx433.

Nerre
Posts: 7
Joined: Sunday 19 March 2017 17:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby Nerre » Tuesday 21 March 2017 21:53

Strange thing for me is that arping to the IP address of the phone (Android) works, but not using the MAC address (I wanted to use the MAC to avoid having to give the phone static IP).

And I am sure I use the right MAC address since arpinging the IP address gives med the MAC as response, so I can copy it and paste.

This is probably something with my phone, but it shows that is may be hard to find a foolproof method.

kubrik
Posts: 52
Joined: Wednesday 30 November 2016 16:07
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby kubrik » Wednesday 22 March 2017 2:04

Perhaps i'm going a little ot...but..
i've found that using nmap (i've tried arp-scan too..totally unreliable) is the more "reliable" method for network presence detection.

Code: Select all

sudo  /usr/bin/nmap -sP -PR 192.168.1.50-100 | grep MAC | awk '{ print $3 }'

gives a list of mac addresses alive into the network range 192.168.50-100 (dhcp scope in this case...you can put all subnet but slower).
You can search for your phones mac addresses into this list.
Android issue can be "solved" considering a time range detection for the "absence".
Infact i've also found that with 1 minute check you can consider offline the phone if the script doesn't find it for 12 minutes (with 10 minutes i get some false positives).
If you use the absence for alarm activation, it could be a reasonable activation time.

Nerre
Posts: 7
Joined: Sunday 19 March 2017 17:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby Nerre » Wednesday 22 March 2017 20:41

The version of nmap I have installed (6.47) does not seem to have options -sP or -PR...

Oh, it did... but I didn't see them in the manpage.... strange.

kubrik
Posts: 52
Joined: Wednesday 30 November 2016 16:07
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby kubrik » Thursday 23 March 2017 1:02

You are right...
-sP is the actual -sn (no port scan), both are the same.
-PR (Arp ping) is the default with local network.

So "nmap -sP -PR 192.168.1.50-100" is equivalent to "nmap -sn 192.168.1.50-100" :)

rif. https://nmap.org/book/man-host-discovery.html

Jazzle
Posts: 1
Joined: Friday 31 March 2017 13:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby Jazzle » Sunday 23 April 2017 16:52

Koensk wrote:Anyway,
What's the working solution? I tried both ip-presence detection and this arp methode but both off them don't work.

With arp I get the following error

[email protected] ~/domoticz/scripts $ ./arpdetect.sh 192.168.0.110 635 10
PING 192.168.0.110 (192.168.0.110) 56(84) bytes of data.

--- 192.168.0.110 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 215.874/215.874/215.874/0.000 ms
./arpdetect.sh: line 60: unexpected EOF while looking for matching `"'

Anyone an idea or an other solution to detect if my phone is in the house or not?

Thx


Koesnk, dit you find the reason for the EOF error and did you get the Arp script working?

I get the following 2 errors:

./arpdetect.sh: line 60: unexpected EOF while looking for matching `"'
./arpdetect.sh: line 62: syntax error: unexpected end of file

Anyone an idea?

Thanks

Jasper

micksel
Posts: 79
Joined: Wednesday 28 January 2015 18:58
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby micksel » Tuesday 23 May 2017 14:48

zedd wrote:I've altered the script from https://www.domoticz.com/wiki/Presence_detection to use arping as it is much more reliable AND will detect when a device is disconnected / leaves the network.
.


Zedd, Can you share your modified script?

zedd
Posts: 6
Joined: Monday 02 January 2017 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby zedd » Tuesday 23 May 2017 16:14

micksel wrote:Zedd, Can you share your modified script?

I actually altered the script in the Wiki page...

micksel
Posts: 79
Joined: Wednesday 28 January 2015 18:58
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby micksel » Tuesday 23 May 2017 16:29

zedd wrote:
micksel wrote:Zedd, Can you share your modified script?

I actually altered the script in the Wiki page...

Feeling stupid, thanks

User avatar
EdKo66
Posts: 478
Joined: Sunday 22 January 2017 22:46
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Rhoon - Netherlands
Contact:

Re: script for presence detection of a phone

Postby EdKo66 » Tuesday 23 May 2017 23:28

When trying this

Code: Select all

python check_device_online.py 192.168.0.117 168 10 30


results in

Code: Select all

python check_device_online.py 192.168.0.117 168 10 30
23:25:12- script started.
23:25:12- according to domoticz, 192.168.0.117 is offline
Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination
  -f : quit on first reply
  -q : be quiet
  -b : keep broadcasting, don't go unicast
  -D : duplicate address detection mode
  -U : Unsolicited ARP mode, update your neighbours
  -A : ARP answer mode, update your neighbours
  -V : print version and exit
  -c count : how many packets to send
  -w timeout : how long to wait for a reply
  -I device : which ethernet device to use
  -s source : source ip address
  destination : ask for what ip address
This goes on until I stop the script.

It seems my phone (Android 7) goes to doze after some time. Can't find a place to turn that off. Trying to add a ping in the script I'm not sure off doing.

Manually setting the switch to 'on' results the script in answering that the phone is online, of course. But I'm sure that isn't the right way of working with it.
Running latest BETA on a NAS Synology 413j | Toon Thermostat | RFxcom | Dashticz V2 |

micksel
Posts: 79
Joined: Wednesday 28 January 2015 18:58
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: script for presence detection of a phone

Postby micksel » Sunday 28 May 2017 8:42

Seems that the used a big W in the script and not a small w, so start by changing that, I hade that problem, but I also changed by removing the timeout so that will use the default value and set a -c of at least 15 because my android is something very slow on responding.


Return to “Scripts [cleaning in progress]”

Who is online

Users browsing this forum: No registered users and 3 guests