Python plugin: Presence detection from wireless router

Python and python framework

Moderator: leecollings

drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

EscApe wrote: Tuesday 19 June 2018 21:02 @drwurn

The poll interval is now a pull down choice and an option for a override button has been added. Those changes have not yet been added to the instruction. The basic setup is still accurate.
Do you ask because something is not working?
No I've tried the arping method and ifttt method but both not working good. Takes too long before I'm home according to domoticz.

So was looking for a method that actually works good. I want to be detected before I enter my house.
How fast is the detection when connected to wifi?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@drwurn

Detection is nearly instant when the plugin can use native wireless tools on the router (like wl for Broadcom). It depends mostly on the poll interval and the grace period you can configure. Just try and see if it serves your needs.
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

Sounds good.
Tomorrow gonna make this work, atleast gonna try it. Kinda new to domoticz and even more new to programming and scripts and stuff.
devros
Posts: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros »

EscApe wrote: Tuesday 19 June 2018 20:29 @devros

Combining parameters has crossed my mind and I will consider it for a future version. Don’t want to over complicate things though.
The plugin is meant to talk to wireless routers directly (tenda in your case). Does the tenda offer ssh?

Just curious: how long does it take until absence is detected after a device on your network has gone offline? ARP is unusable on my Asus, because it can take forever for it to notice that a device has disconnected (person holding it has left the house).
just did test 8 minutes with arp method...
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Thanks! 8 minutes is not that bad. On my ASUS it sometimes took minutes, but would at times take many hours. You might want to check the presence log for a few days to see if it is consistently around 8 minutes. If so, then arp indeed looks like a usable (fall back) option for certain routers.
devros
Posts: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros »

diged into ubitiqui airos and found this :)
https://community.ubnt.com/t5/airOS-Sof ... d-p/402835
check brctl command...

and works quite well

Code: Select all

XM.v6.1.7# brctl showmacs br0
port no	mac addr		is local?	ageing timer
  1	00:27:22:37:00:0c	yes		   0.00
  1	00:a0:de:a2:a4:1c	no		  98.68
  1	00:d0:4b:96:1f:c6	no		  10.33
  1	04:95:e6:23:1d:50	no		   0.44
  1	28:6c:07:10:84:de	no		  11.42
  1	32:83:45:4b:85:db	no		   0.07
  1	54:10:ec:8b:7c:dd	no		  52.71
  1	60:01:94:70:ac:e0	no		 122.16
  1	78:11:dc:12:ce:b7	no		  28.27
  1	78:11:dc:b0:64:78	no		   6.70
  1	a0:d7:95:85:36:55	no		  12.34
  1	b4:43:0d:f9:16:04	no		   2.45
  1	d4:9a:20:5a:e8:f1	no		   0.00
  1	f0:fe:6b:28:2a:fe	no		   3.93
  1	f0:fe:6b:6a:0f:1a	no		  11.09
XM.v6.1.7# 
can we use this ?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Theoretically we could use any command we can find, however let’s first investigate how ‘real time’ this information is. Since brcrl queuries the bridge and not the wireless interface it might be comparable to arp (maybe even the same info). Like arp it is a generic Linux command so also less router/chipset specific (which is great). I’ll have a look at it tomorrow.
Anyway, a great find and i appreciate your efforts to find alternative approaches!
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

Alternative option: Enter a password on the plugin hardware configuration page and install sshpass on your (linux) Domoticz machine (sudo apt-get install sshpass).Domoticz will store the password in plain text. This is therefore less secure, but far easier to setup
Where to enter a password on the plugin hardware configuration page?
Have installed sshpass, but how to enter a password?
devros
Posts: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros »

EscApe wrote: Tuesday 19 June 2018 22:22 Theoretically we could use any command we can find, however let’s first investigate how ‘real time’ this information is. Since brcrl queuries the bridge and not the wireless interface it might be comparable to arp (maybe even the same info). Like arp it is a generic Linux command so also less router/chipset specific (which is great). I’ll have a look at it tomorrow.
Anyway, a great find and i appreciate your efforts to find alternative approaches!
great did some test and brcrl is updated realtime switched wifi on/off on my iphone and ageing timer was reseted immediately to zero :)
UPDATE
tested iphone and it looks thats sending no wifi signal when is display off, and its remove from brcrl after 5 minutes, when its on is ageing timer updated immediately
Last edited by devros on Tuesday 19 June 2018 22:44, edited 1 time in total.
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@drwurn
In the field that says ‘password’ (depending on your language). It’s right below the username on the plugin (hardware) configuration page.
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

EscApe wrote: Tuesday 19 June 2018 22:33 @drwurn
In the field that says ‘password’ (depending on your language). It’s right below the username on the plugin (hardware) configuration page.
That is the router password correct?
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

2018-06-19 22:39:29.553 Error: (WiFi Presence) Router command failed with returncode: 255
2018-06-19 22:39:29.553 Error: (WiFi Presence) Raw data from router: b''
2018-06-19 22:39:29.553 Error: (WiFi Presence) Could not determine router capabilities
2018-06-19 22:39:39.476 Error: (WiFi Presence) No usable commandline to check presence. Trying again to detect router capabilities.
2018-06-19 22:39:39.476 Error: (WiFi Presence) If you keep getting this message: check your (authentication) settings and router status.
2018-06-19 22:39:39.476 Error: (WiFi Presence) Make sure your router supports ssh and the commands used by this plugin. If this message persists disable the plugin (for now), or the plugin might flood the Domoticz eventsystem
What to do? Having an ExperiaboxV10 and I've forwarded port 22
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@devros

I was to curious and couldn't resists testing your approach ;-) Unfortunately i can confirm your own observation. The ageing timer starts every time there is no traffic and based upon the set ageing time will remove the mac address from the list. It's not suitable to detect presence, since it is based on traffic and not on the connection. Querying the wireless driver directly still seems the best way, apr being a acceptable fallback.
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@drwurn

Yes, that is the router password.

I don't know the experia box, but why would you forward port 22? Port forwarding is normaly used to allow access to a host inside you local network (not being the router) from clients on the internet.

First please try to get ssh acces to the router from your domoticz machine's Linux command line to see if you can get that working. You can google for ssh access to experia box.

If -after that- the plugin fails please enable debug mode, retry and share the log. There's not much to tell from the basic logs.
devros
Posts: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros »

EscApe wrote: Tuesday 19 June 2018 22:55 @devros

I was to curious and couldn't resists testing your approach ;-) Unfortunately i can confirm your own observation. The ageing timer starts every time there is no traffic and based upon the set ageing time will remove the mac address from the list. It's not suitable to detect presence, since it is based on traffic and not on the connection. Querying the wireless driver directly still seems the best way, apr being a acceptable fallback.
but this problem with ageing is with all methods no (if phone is without wifi after some time is disconected from list)?

hmm dunno why but now brctl updates my iphone even with black screen... (max 90 seconds). Maybe apr takes info from brctl but with some timeouts presets for disconecting ...
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

pi@raspberrypi:~ $ ssh [email protected]
ssh: connect to host 192.168.2.254 port 22: Connection refused
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

pi@raspberrypi:~ $ ssh [email protected]
ssh: connect to host 192.168.2.254 port 22: Connection refused
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by drwurn »

2018-06-19 23:18:46.477 Error: (WiFi Presence) No usable commandline to check presence. Trying again to detect router capabilities.
2018-06-19 23:18:46.477 Error: (WiFi Presence) If you keep getting this message: check your (authentication) settings and router status.
2018-06-19 23:18:46.477 Error: (WiFi Presence) Make sure your router supports ssh and the commands used by this plugin. If this message persists disable the plugin (for now), or the plugin might flood the Domoticz eventsystem
2018-06-19 23:18:46.477 (WiFi Presence) Checking router capabilities and wireless interfaces
2018-06-19 23:18:46.478 (WiFi Presence) Using password instead of ssh public key authentication (less secure!)
2018-06-19 23:18:46.478 (WiFi Presence) Fetching data from router using ssh
2018-06-19 23:18:46.478 (WiFi Presence) command: ['sshpass', '-p', '<PASS>', 'ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=2', '[email protected]', '#!/bin/sh\n\t\t\t\t\texport PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH\n\t\t\t\t\ttest=$(which wl > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wl@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wl -i $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which iwinfo > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "iwinfo@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\\|^ath\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(iwinfo $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which wlanconfig > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wlanconfig@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\\|^ath\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wlanconfig $iface list > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\ttest=$(which arp > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "arp"\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tif [ -f /proc/net/arp ]; then\n\t\t\t\t\t\tprintf "procarp"\n\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tprintf none']
2018-06-19 23:18:46.530 (WiFi Presence) Returncode ssh command: 255
2018-06-19 23:18:46.530 (WiFi Presence) Output from router: b''
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@devros

Good point. The router and phone keep the connection alive and i was suspecting that this 'keep alive' traffic was just between the wireless interfaces and would not reach the router. That's still a possibility, but i am no networking expert. I also see the ageing time restarting every 90 seconds (screen on or off makes no difference). That still makes the response less instant as directly interfacing with the wireless drivers, but much quicker than arp.

Your assumption that arp uses a extra timeout seems reasonable. My plugin does the same using the 'grace period'. No need to have two timeouts, so your brctl approach has regained some interest ;-)

To use this in the plugin will take some effort, but i'll give it a go this week.
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@drwurn

If a connection from the command line isn't possible, then the plugin won't be able to connect to ssh either.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest