Presence Detection

From Wildsong
Revision as of 16:15, 2 January 2020 by Brian Wilson (talk | contribs)
Jump to navigationJump to search

I want to be able to tell when a family member (me or Julie) is present in our domicile, so that the house can respond appropriately.

Two easy ways I can think of to do this are DHCP via DNSMASQ and association with the access point. I've already tested both and today I am working on the Unifi access point approach.

Unifi

Unifi is this Ubiquiti system where you run a Unifi server (for example on Bellman on port 8443) and it watches all the supported WiFi APs on your LAN and lets you manage all of them from one place.

I have just one AP right now. I live in a small house, it's all I need. It supports two separate networks "wildsong2" for 2 GHz and "wildsong5" for 5 GHz. They could be combined but if I do that I can't control or see which network a computer connects to, so I lose control over the bandwidth. (Plover likes to chose the slower 2 Ghz for some reason.)

FYI my AP can run for months at a time without ever seeing Unifi Server once it is set up. The server is for management and logging, packets will fly and authentication will work without server being online.

Unifi API

I can run python scripts to pull data from the UniFi server via REST, see https://github.com/calmh/unifi-api

This is pure coolness.

sudo pip install unifi
./unifi-ls-clients -c bellman -u readonlyuser -p YOURPASSWORDHERE -v v5 -s default
NAME                             MAC          AP           CHAN  RSSI  RX   TX
vastra-preseed           3c:15:c2:dd:15:9c  eLab            60    47  400  400
SqueezeboxRadio          00:04:20:2a:c6:3f  eLab             6    39   54   54
android-e6e08e8b3a7b051f a0:0b:ba:e8:c5:8a  eLab             6    38   39   65
Swift                    60:c5:47:00:90:5a  eLab            60    32  162  117
android-d16cb9f5371c2cf6 30:85:a9:5b:af:57  eLab             6    27    6   63

That's actually not the information I need though.

Unifi logs

Using the Unifi log I can see when a device becomes visible and when it leaves.

The log file "server.log" I need is in a Docker volume called unifi_log

I can build a Docker container to run my microservice and let it have read-only access to the log. Here is a test

docker run -it --rm -v unifi_log:/unifi:ro busybox ash

Now I should get a shell prompt and be able to do

/ # cd /unifi
/unifi # tail -2 server.log 
[2020-01-02 16:03:55,943] <inform-64> INFO  inform - from [80:2a:a8:90:cd:65](80:2a:a8:90:cd:65, U7LT, 4.0.69.10871): state=CONNECTED, ext/stun_ip=192.168.123.66, dev_ip=192.168.123.66, up=2461
[2020-01-02 16:04:09,042] <inform-66> INFO  inform - from [80:2a:a8:90:cd:65](80:2a:a8:90:cd:65, U7LT, 4.0.69.10871): state=CONNECTED, ext/stun_ip=192.168.123.66, dev_ip=192.168.123.66, up=2474
/unifi # 

Way cool.

Here is a full "conversation": I turn on Wifi on my phone then turn it off.

Turning on WiFI generates an event like this:

[2020-01-02 16:06:03,541] <inform-85> INFO  event  - [event] AP[80:2a:a8:90:cd:65] event: { "arp_reply_gw_seen" : "yes" , "assoc_delta" : "10000" , "assoc_status" : "0" , "auth_delta" : "0" , "auth_ts" : "2577.827989" , "dns_resp_seen" : "yes" , "event_id" : "1" , "event_type" : "success" , "ip_assign_type" : "dhcp" , "ip_delta" : "4380000" , "mac" : "d4:63:c6:5b:28:57" , "message_type" : "STA_ASSOC_TRACKER" , "vap" : "ath2" , "wpa_auth_delta" : "1020000"}

Turning off WiFi generates this.

[2020-01-02 16:06:12,551] <inform-90> INFO  event  - [event] AP[80:2a:a8:90:cd:65] event: { "assoc_status" : "0" , "event_id" : "1" , "event_type" : "sta_leave" , "mac" : "d4:63:c6:5b:28:57" , "message_type" : "STA_ASSOC_TRACKER" , "vap" : "ath2"}


Walking out the door will generate the same "sta_leave" event as soon as the phone drops off the network. I will have to debounce because it could drop out momentarily inside the house (for example if I go in the basement) and it could pop on and off a few times as I walk away down the hill and go around a building. I will need to experiment with it.

Flask project

I started a flask project and it's in source/presence on Bellman.

Originally I was doing a micro web server but it's morphing into an MQTT publisher.

It will listen to STDOUT from the Unifi Docker container (which is where log messages go by default.)