Presence Detection

From Wildsong
Revision as of 14:45, 2 February 2023 by Brian Wilson (talk | contribs) (→‎iBeacon on Pi)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

When I started this project I wanted to be able to manage phones, doing things like forwarding calls. I might get back to that sometime.

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.

I have found that using DHCP this way is spotty. Using tracker software on the phone is spotty too. Half the time it shows Julie's phone at home when I know it's in her locker at work.

iBeacon

iBeacons are just chatty BLE devices. Maybe I can program some ESP32 devices to be iBeacons. Making an iBeacon with an HM-10 is another cheap option but I don't happen to own any. You could use a Raspberry Pi but that seems like a waste unless you have one sitting around already. OH! I do, they run my hifi speakers so most of the time they are just hanging around listening for commands to play music.

  1. Put OwnTracks app on your phone. Tell it to use iBeacons.
  2. Add the OwnTracks integration to Home Assistant.
  3. Scatter iBeacon devices hither and thither (put their IDs into OwnTracks).
  4. When your phone sees a beacon, it can tell OwnTracks. OT can tell Home Assistant. There you go.

This slightly breaks down because I don't currently carry a smartphone. I do generally carry a WiFi tablet though, which means it can respond when it sees iBeacons, as long as it's on WiFi then it can report position.

The cool part is, I can put an iBeacon tag anywhere without setting it up on the local network, for example, at work. Just stick it on my bulletin board for example. As long as OwnTracks knows its location... the location does not have to be fixed either. So if I put one in my car or bicycle, it will know I am near it.

iBeacon on Pi

Well, my Pi Zeros Okapi and Zebra are running media server software, not a full Debian image, so they are really not set up to be iBeacons.

I will have to pull out the Pi 3 Violet or the Pi 4 Tenrec if I want to try this. What about Bellman? I don't even know if Bellman has Bluetooth. Oh! He does, how convenient!

Even more convenient is this calculator that converts my URL https://w6gkd.radio to the correct commands.

sudo hciconfig hci0 up

sudo hciconfig hci0 leadv 3

sudo hcitool -i hci0 cmd 0x08 0x0008 19 02 01 06 03 03 aa fe 11 16 aa fe 10 00 03 77 36 67 6b 64 2e 72 61 64 69 6f 00 00 00 00 00 00

The Beacon Scanner and nRF Connect apps don't see it. I have no idea if the Bluetooth on Bellman is functioning.

I ran the same 3 commands on Plover and hey presto! it works. In nRF Connect, Plover shows up as "Physical Web Beacon", and when I expand it I can see the URL.

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.

MQTT

Now I learn more about MQTT.

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.)