script for presence detection of a phone

All kinds of 'OS' scripts

Moderator: leecollings

zedd
Posts: 9
Joined: Monday 02 January 2017 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by zedd »

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.
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: script for presence detection of a phone

Post by jannl »

I think also arping will give problems with Android's doze function (Android 6)
zedd
Posts: 9
Joined: Monday 02 January 2017 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by zedd »

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.
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: script for presence detection of a phone

Post by jannl »

Place your android 7 phone on a flat surface, no charger attached and wait a few hours. See what happens.
Koensk
Posts: 33
Joined: Friday 25 September 2015 9:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by Koensk »

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

pi@raspberrypi ~/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: 15
Joined: Sunday 19 March 2017 16:08
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Norway
Contact:

Re: script for presence detection of a phone

Post by ArnieO »

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: 15
Joined: Sunday 19 March 2017 16:08
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Norway
Contact:

Re: script for presence detection of a phone

Post by ArnieO »

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: 8
Joined: Sunday 19 March 2017 16:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by Nerre »

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: 47
Joined: Wednesday 30 November 2016 15:07
Target OS: Linux
Domoticz version: 2020.2
Location: Italy
Contact:

Re: script for presence detection of a phone

Post by kubrik »

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.
Ubuntu 20.04 on ACEPC AK1 (previously RPi3b+)
Z-Stick Gen5, RFXtrx433E, Philips HUE Bridge, Yeelight, ESP8266 NodeMCU, Broadlink RM
dZvents, bash, php, Node-RED
Nerre
Posts: 8
Joined: Sunday 19 March 2017 16:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by Nerre »

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: 47
Joined: Wednesday 30 November 2016 15:07
Target OS: Linux
Domoticz version: 2020.2
Location: Italy
Contact:

Re: script for presence detection of a phone

Post by kubrik »

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
Ubuntu 20.04 on ACEPC AK1 (previously RPi3b+)
Z-Stick Gen5, RFXtrx433E, Philips HUE Bridge, Yeelight, ESP8266 NodeMCU, Broadlink RM
dZvents, bash, php, Node-RED
Jazzle
Posts: 21
Joined: Friday 31 March 2017 13:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by Jazzle »

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

pi@raspberrypi ~/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: 50
Joined: Wednesday 28 January 2015 17:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by micksel »

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: 9
Joined: Monday 02 January 2017 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by zedd »

micksel wrote:Zedd, Can you share your modified script?
I actually altered the script in the Wiki page...
micksel
Posts: 50
Joined: Wednesday 28 January 2015 17:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by micksel »

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
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: script for presence detection of a phone

Post by EdwinK »

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 Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
micksel
Posts: 50
Joined: Wednesday 28 January 2015 17:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by micksel »

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.
Jordyboytjuh
Posts: 25
Joined: Thursday 10 March 2016 20:57
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: script for presence detection of a phone

Post by Jordyboytjuh »

EdKo66 wrote: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.
I think I've the same issue. Since the update to Android 7 my online device checker won't work as good as it did. When I put my phone in just stand-by mode. After a little while, it says it's not "on" anymore in domoticz. When I put the mobile on again, the pushnotification that "someone's home" immediately appears. When I try to ping it directly on the pc, it says request timed out (when it's a little while in standby mode). I think in Android 7 something has changed the pinging?
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: script for presence detection of a phone

Post by jvdz »

Correct, Android 7 does put the WiFi much more in sleep mode than older releases, meaning any MAC or IP checking will be flawed. I am using both Ping and Bluetooth Check to determine the presence of the phones and that works like a charm.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
thijsdebets
Posts: 3
Joined: Friday 29 May 2015 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: script for presence detection of a phone

Post by thijsdebets »

I created a script to detect all devices on the network.
Installation instructions are here: https://github.com/thijsdebets/networkdetectz/wiki

It uses a combination of arpscan, ping and even supports bluetooth detection.
I use it very succesfull to detect who is at home and to set the lights to TV-Mode when the tv switches on, etc.

It requires one arp-scan every minute via cron making it low on resources.
v3.4834 on RPiB2
P1 Smartmeter
Onmik Solarpanel
Evohome heating
NAS WOL/SOL
KiKa: 14 switches + 3 PIR
Extra
Full LAN detection w. arpscan
TravelTime via GoogleMaps in TempGraph
Internet speed in Othergraph
Control
20 Blockly and 5 LUA scripts
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest