Bluetooth Presence Detection
You might be here because you heard me mention my Bluetooth presence detection setup on the new SelfHosted podcast, or possibly because you want to subject yourself to an internet-of-things setup in your own home while desperately trying to escape the consequential corporate surveillance as a result of that action (soft chuckles for everyone). Let’s talk about what it is, why you might want it, and how you can minimize the downsides
What is this?
Quite simply, the future of the smart home is in your hands and no one else’s. I simply cannot imagine a commercially viable smart home that runs from the cloud unless the inhabitants of that smart home are willing to upgrade and replace the smart home components on a rapid basis. While my personal belief is that novelty, more than utility, drives almost all current “smart home innovation,” I will concede that automating aspects of my domestic life is appealing. I don’t believe it is necessary that anyone give up autonomy and freedom (and Software Freedom) in order to enjoy modern conveniences.
One of the foundational blocks we can talk about for home automations is presence detection. How does the smart home know who is at home at any given time?
With this project I show you how to setup a Raspberry Pi zero for presence detection.
Why would you want presence detection?
If you want to do presence detection, this is the best way, if you carry a cel phone. I’ve spent many hours trying, and failing, to find a better way. Wifi is not fast enough and phones will hop off the wifi to safe power when they’re sleeping. GPS is not accurate enough to distinguish between me going from an evening stroll (and needing outside lights for my return) and being inside my home. Wifi suffers from a bit of the same kinds of problems. There are also false negatives when switching e.g. from 4G/LTE on a phone to WiFi. Bluetooth was the only option.
Even then, Bluetooth was not perfect – Bluetooth low energy tokens are expensive (relatively) and most phones are going to beacon on Bluetooth channels just by virtue of being on.
If you want your garage lights to come on after dark when you arrive at home, automatically, and shut off when you enter your home, then this is for you.
If you’ve experimented with IoT devices, then you know that solutions for this are often costly and sometimes depend on an internet connection (!?) in order to work properly.
It goes without saying that requiring an internet connection for basic “smart home” functionality is one of the dumbest things that there is. This won’t require any type of internet connection.
All right I’m sold – let’s do this!
The secret sauce is a Raspberry Pi Zero W and monitor.sh https://github.com/andrewjfreyer/monitor . TL;DR : Passive Bluetooth presence detection of beacons, cell phones, and other Bluetooth devices. Useful for mqtt-based home automation, especially when the script runs on multiple devices, distributed throughout a property.
Step 1 – Configure your Raspberry Pi
Download Raspbian
Image Raspbian
Mount the Boot Partition
Create wpa_supplicant.conf w/this conf:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="Wireless Network Name"
psk="Wireless Network Password"
key_mgmt=WPA-PSK
}
touch ssh
(on Windows create a file called ssh with no extension in the root of the boot partition. This will enable ssh. On Mac/Linux just touch the file.)
Step 2 — Download & Configure Monitor
ssh pi@whatever_the_ip_was_on_your_network (password is raspberry) and set a more appropriate memoerable password.
apt update && apt upgrade
then
apt dist-upgrade
then
apt install pi-bluetooth
Then reboot!
Step 3 – Mosquitto and git
We will need to add the Mosquitto repository to Debian, update and fetch it and the git client.
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
apt-get update
apt-get install -f git libmosquitto1 libmosquitto-dev mosquitto mosquitto-clients
Step 4 – Setup monitor
You may encounter errors during the apt update and upgrade – just reboot and make sure everything has been updated. You can check journalctl -xe
for errors after reboot to make sure it came up right.
With all the packages installed and working properly, you can now setup monitor.
Getting the monitor script up and going is not too bad.
git clone https://github.com/andrewjfreyer/monitor.git
pi@raspberrypi:~ $ cd monitor/
pi@raspberrypi:~/monitor $ chmod +x ./monitor.sh
pi@raspberrypi:~/monitor $ ./monitor.sh
then it may complain about mqtt or hcidump being missing. No big deal; just install those. (You may have to sudo these commands so that it is able to install these packages).
The next step is really testing and tying this into something else.
MQTT is a decent “IoT” messaging system so that various devices can communicate. A single raspberry pi monitoring bluetooth devices’ comings and goings around your place is neat, but to really do a good job you need a small army of these. Soon we’ll cover using automation to manage logging, security, and configuration centrally.
For now, you can pull your bluetooth MAC address and add it to known_static_addresses
Troubleshooting
Nothing open source ever goes exactly according to the how to, eh? Yeah, the adventure really clears the mind though.
journalctl -u mosquitto
This is useful for troubleshooting.
In my case, this was fine. However man mosquitto
showed it had a -c
command for checking the config. Let’s do that.
Segmentation fault
Oh, look. Something is wrong with the config. After spending some time with gdb
I ultimately gave up.
Downgrade the packages:
sudo apt-get install mosquitto=1.6.4-0mosquitto1~stretch1 libmosquitto1=1.6.4-0mosquitto1~stretch1 mosquitto-clients=1.6.4-0mosquitto1~stretch1
… and now it seems to be working.
The last step is to edit mqtt_preferences
and specify your mqtt host. Never heard of mqtt? Learn more here:
http://mqtt.org/
What Now?
Well, you’ve got some work to do to add the devices around you.
# ls -al
total 156
drwxr-xr-x 4 pi pi 4096 Oct 13 05:15 .
drwxr-xr-x 4 pi pi 4096 Oct 13 03:44 ..
-rw-r--r-- 1 pi pi 46 Oct 13 03:51 address_blacklist
-rw-r--r-- 1 root root 638 Oct 13 04:36 behavior_preferences
drwxr-xr-x 8 pi pi 4096 Oct 13 05:13 .git
-rw-r--r-- 1 pi pi 184 Oct 13 05:13 known_beacon_addresses
-rw-r--r-- 1 pi pi 158 Oct 13 05:03 known_static_addresses
-rw-r--r-- 1 root root 87 Oct 13 05:14 .manufacturer_cache
-rwxr-xr-x 1 pi pi 71143 Oct 13 03:45 monitor.sh
-rw-r--r-- 1 pi pi 498 Oct 13 04:36 mqtt_preferences
-rw-r--r-- 1 root root 204 Oct 13 05:13 .pids
-rw-r--r-- 1 pi pi 8 Oct 13 03:46 .previous_version
-rw-r--r-- 1 pi pi 168 Oct 13 05:13 .public_name_cache
-rw-r--r-- 1 pi pi 29595 Oct 13 03:45 README.md
drwxr-xr-x 2 pi pi 4096 Oct 13 03:45 support
As you can see a fair number of files have been generated.
From here you could set it up with Home Assistant or another tool that might be useful to automate things. Check out the video for some ideas that I’ve found actually useful for automation over the years.
The Ten Commandments of Automation
TODO
Okay, so there aren’t exactly ten, and they aren’t exactly commandments. But if you’re going to embark on IoT, always give yourself a little introspection:
- Is this actually going to be easier?
- What problem(s) am I trying to solve with this device or setup?
- Will this save me time, or make my life better in some way?
- Is it easy to recover when it fails?
- Is it possible to reproduce this setup quickly?
- Will this cause visitors to struggle when visiting?
- Is the contemplated future with this working right going to be more relaxing than the current status quo?
- If something goes wrong, will I have a heart attack from stress?
- TODO
No no no this is all wrong!
You might be thinking that this guy has lead me down a path and now I’m operating with two-three-four linux-based ssh-enabled surveillance devices that just cannot wait to mine bitcoin for the Kremlin. And there is some truth to that – fortunately you are (will be?) an IT Pro. The danger, and insecurity, comes from the lack of updates. Just like it would be hilarious, and terribly insecure, if your fridge ran Windows 2000, you must not forget that your IoT devices will need support and updates from now until the end of time.
That’s really the lie of all IoT devices – that they will work forever. In reality they have shockingly short product lifetimes and there have been several funny/sad examples in recent news TODO.
The solution for this is to use standard “configuration management” software, such as Puppet, to manage devices like the Raspberry Pis. When you have configuration changes, the RPi are formatted from scratch and reconfigured using modern packages. With the Debian Linux distribution this approach ensures a 5 to 10 year service lifetime while assuring the same level of security as the main line Debian project.
The specifics of this setup will be a project for another day, however.