This is a few notes on BLE-based presence detection.
The obvious use case seemed to be to have your phone as the presence token. I started off looking into Eddystone and iBeacon devices but they seem to be too application oriented and I couldn’t get my phone working with them - interestingly this seems to due to the security differences between stock Android and LineageOS - but I couldn’t really get this to work usefully.
So I started fishing around for cheap things to play with and found a plentiful supply on eBay. The common devices at the moment are iTag and Nut, which are both application oriented but are really just simple BLE devices which send a MAC address and name. The iTags are square or triangular Bluetooth 4.0 BLE devices and cost a couple of quid each. I got a couple from a UK supplier (there are a few). The Nut is a square device an inch or so square, and i ordered a couple at £3.00 or so that turned out to be coming from China and still haven’t arrived, so I tried a couple at £6.00 or so which turned up in a few days.
As I now understood that what I was looking for was presence detection it was easier to find what I needed. The HappyBubbles devices look good but $30 still seems a bit much for fiddling. Then I came across Room Assistant and everything clicked. My Home Assistant device is a Raspberry Pi 3 B that doubles as a media player. As this has built in bluetooth it was easy enough to install the tools to detect the BLE devices and to install Room Assistant on it. You’ll also need an MQTT broker - mosquitto or the native broker in Home Assistant should be fine.
The tags can be detected using bluetoothctl
. Starting it from the command line should find your bluetooth interface (usually hci0) and then running scan on
should find broadcasting devices, which should look like this:
Device C1:BE:FF:73:AD:74 nut
That’s the tag’s MAC address and name. It might be possible to change the name but the MAC address should be all you need.
Install Room Assistant as per the instructions in the README on the repository. It’s easier to run it as root rather than as a user but both should work.
Setting up room assistant is fairly simple:
"mqtt": {
"enabled": true,
"url": "mqtt://mqtt.host.name:1883",
"username": "user",
"password": "password",
"reject_unauthorized": true,
"topic": "Room1"
},
"console": {
"enabled": true
},
"ble": {
"enabled": true,
"channel": "room_presence",
"max_distance": 5,
"whitelist": ["c1beff73ad74"],
"use_mac": false,
"system_noise": 0.01,
"measurement_noise": 3,
"update_frequency": 5
},
The main variables are the host and port of your MQTT broker, the username and password and the topic. This usually represents the room that Room Assistant is in.
In the ble section, add the MAC address of your tag to the whitelist as shown, in lower case with the colons removed: the client and server seem to be happier with a plain string.
Test with npm start index.js
. You should see some messages that the interfaces are being initailised and then the payload being sent to the broker, which looks like this:
[room_presence] {
[room_presence] "id": "c1beff73ad74",
[room_presence] "name": "nut",
[room_presence] "rssi": -59,
[room_presence] "distance": 0.9933927152745275
[room_presence] }
You can confirm that the broker is receiving the messages with mosquitto_sub using the topic room_presence/#
Enable the mqtt_room sensor in Home Assistant like this:
- platform: mqtt_room
device_id: "c1beff73ad74"
name: "nut"
state_topic: 'room_presence'
timeout: 5
Restart and you should see a sensor state which should return your topic as the value, or away if your tag is sleeping.
However, this isn’t much use with one room. You will need multiple detectors for your house, and this is where the Raspberry Pi Zero W comes in. These are the newest variant of the Pi, with wifi and bluetooth. and 512MB of RAM They sell at around £9.00 in the UK. Pimoroni or Adafruit are good sources, and Pimoroni have their great little Coupe case and adaptors in a package for £19. Add in an SD card and you have the basis for another detector. Possibly a bit of overkill compared to a HappyBubble but it will be capable of doing other things as well.
Downside is that the nodesource distribution of node isn’t available for the Pi Zero’s ARM6 processor so you have to use the binary bundle (don’t use the node package in Raspbian), and there’s a bit more work to set it up but not a lot. The only difference in setup otherwise is a different topic for the mqtt configuration. Place the Zero in another room and make sure it can connect through wifi and your mqtt_room state should show either room depending where your tag is.
Next: how to make that do something useful…