Presence detection with low cost BLE devices and Raspberry Pi/Zero W

https://github.com/mKeRix/room-assistant/issues/33

Might help u

1 Like

Thank you very much for your replys. It was the same issue!
I was using [""] instead “” on the white list.
Now i have it running perfectly.
Thanks!!!

So this is going to be long, but below I’ve detailed how I’ve gotten this working for my own personal use, using an ibeacon simulator app for my android phone.

I am by no means an expert on any of this, this is simply the steps I took to get this to work for me. A lot of this is repeated in this thread, in the room assistant repository and elsewhere, but my goal is by placing it in the order I have below, this will click for others. I’m posting it here in hopes of helping others and for any feedback to determine if there is a more efficient way to complete what I’ve gotten working.

1. Flash Raspbian Stretch Lite on SD card intended for the Pi Zero W. https://www.raspberrypi.org/downloads/raspbian/

2. After flash, I added my wifi network to the Pi. I then use PuTTY from my Windows machine to SSH into the Pi Zero W.

3. Checked for updates to Rasbian

sudo apt-get update
sudo apt-get upgrade -y

4. Run raspi-config and update settings as needed. I updated host name and my password.

5. I used this script to update NodeJs

my node version currently shows:

pi@RoomAssistant1:/ $ node -v
v9.5.0

6. I used Stretch Lite when I installed Rasbian, so I needed to install Git:

sudo apt-get install git

7. Install additional dependencies:

sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev libusb-1.0-0-dev

8. Install Room Assistant

git clone https://github.com/mKeRix/room-assistant.git
cd room-assistant
npm install

9. Setup Room-Assistant as a service:

sudo nano /etc/systemd/system/room-assistant.service

10. Copy and paste the following.

[Unit]
Description=Room Assistant service

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/home/pi/room-assistant
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

11. Ctrl+O, Enter, then Ctrl+X to save and close the service you’ve created.

12. Install Docker

curl -sSL https://get.docker.com | sh

13. Run with docker:

docker run --network=host -d --name room-assistant -v ~/room-assistant/config:/room-assistant/config mkerix/room-assistant:latest-rpi

14. Copy default.json config to local.json and open it. Edit the file to your own personal mqtt server & ibeacon settings.

sudo cp config/default.json config/local.json
sudo nano config/local.json

At this point, the only items I’ve changed from the default local.json:

  • For the MQTT section, I used the MQTT server IP & Port, username, and password from my Home Assistant configuration.

  • Under ibeacon, i changed enabled to True

Example:
/home/pi/room-assistant/config/local.json:

{
“app”: {
“unsafe”: false
},
“mqtt”: {
“enabled”: true,
“url”: “mqtt://enter_your_mqtt_ip:and_port”,
“username”: “username_if used”,
“password”: “password_if used”,
“reject_unauthorized”: true,
“topic”: “livingroom”
},
“console”: {
“enabled”: true
},
“ble”: {
“enabled”: false,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: ,
“use_mac”: false,
“system_noise”: 0.01,
“measurement_noise”: 3,
“update_frequency”: 5000
},
“ibeacon”: {
“enabled”: true,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: “”,
“system_noise”: 0.01,
“measurement_noise”: 3,
“major_mask”: “0xFFFF”,
“minor_mask”: “0xFFFF”
},
“temper”: {
“enabled”: false,
“channel”: “temper”,
“interval”: 60000,
“scale”: 1,
“offset”: 0
},
“gpio”: {
“enabled”: false,
“only_send_updates”: true,
“ports”: ,
“qos”: 0,
“retain”: false
},
“shell”: {
“enabled”: false,
“commands”: ,
“qos”: 0,
“retain”: false
}
}

Note: I also downloaded beacon simulator for android which is free.
I decided to go with ibeacon component over the BLE as the UUID did not change if I shut the beacon off and on, whereas when I used the BLE component the ID was simply the MAC address of the current BLE and changed if I turned the app on or off.

15. Ctrl+O Enter, then Ctrl+X to save and close the local.json you’ve just modified.

16. Stop and restart your room-assistant service.

sudo systemctl stop room-assistant.service
sudo systemctl start room-assistant.service

17. From the room assistant directory run:

sudo npm start index.js

Below is a sample of the output:
Ex:

pi@RoomAssistant1:~/room-assistant $ cd room-assistant
pi@RoomAssistant1:~/room-assistant $ sudo npm start index.js

> [email protected] start /home/pi/room-assistant
> node index.js “index.js”

                                       Starting Room Assistant...
                                       Connected to MQTT server
                                       Started console publisher
                                       iBeacon scanner was initialized

[room_presence] {
[room_presence] “id”: “3453b11223434bb295731cadd7b2e6e3-0-0”,
[room_presence] “uuid”: “3453b11223434bb295731cadd7b2e6e3”,
[room_presence] “major”: 0,
[room_presence] “minor”: 0,
[room_presence] “rssi”: -72,
[room_presence] “distance”: 1.5537514174765523,
[room_presence] “accuracy”: 1.4939266621809253,
[room_presence] “measuredpower”: -65,
[room_presence] “proximity”: “near”
[room_presence] }

18. Ctrl Z. Now that you have your iBeacon id, you can plug that into the local.json under whitelist. The id not the UUID.

sudo nano config/local.json

Update your whitelisting as shown below. Ctrl+O, Ctrl+X to save and close.

Example:

“ibeacon”: {
“enabled”: true,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: “3453b11223434bb295731cadd7b2e6e3-0-0”,
“system_noise”: 0.01,
“measurement_noise”: 3,
“major_mask”: “0xFFFF”,
“minor_mask”: “0xFFFF”
},

19. Open a new SSH instance for your home assistant, not room assistant. We’re checking to see if the MQTT server is broadcasting your ibeacon.

pi@Hassbian:~ $ mosquitto_sub -h host_ip_here -p port_here -u **username_if_neede**d -P password_if_needed -v -t room_presence/livingroom

Output:

room_presence/livingroom {“id”:“3453b11223434bb295731cadd7b2e6e3-0-0”,“uuid”:“3453b11223434bb295731cadd7b2e6e3”,“major”:0,“minor”:0,“rssi”:-70,“distance”:1.6380266201803306,“accuracy”:1.3320511843278362,“measuredpower”:-65,“proximity”:“near”}

20. Add the sensor in home assistant.

In my sensors.yaml

- platform: mqtt_room
device_id: "3453b11223434bb295731cadd7b2e6e3-0-0"
name: "Beacon Name Here"
state_topic: 'room_presence'
timeout: 5
away_timeout: 30

I also created a group in my groups.yaml for the time being:
ble_trackers:
name: BLE Trackers
entities:
- sensor.s7

Note: The sensor will show in Home Assistant as sensor.name. Mine currently shows sensor.S7

  1. Restart your Home Assistance Instance.

  2. Profit.

https://imgur.com/a/CCPGa
https://imgur.com/a/7qMTa

14 Likes

@touliloup Your setup sounds like the one i want.
I have already seen some videos on YouTube with the guy with the swiss accent.
Some questions about Your hardware:
What kind of esp32 modules do You use, the small or Big ones?
How do You supply them with power?

Raspberry pi zero w has wifi and ble bluetooth built-in, so I don’t think you need any esp modules or nrf addons. I think @HAVEN8 got it working on Rpi zero w which has not been done before. (Rpi 3 was done before).

I too saw the video from “the guy with the Swiss accent” and modified his code

ESP32 presence detection

to send ‘Home’ and ‘Away’ MQTT presence messages to Homeassistant.

To make the build as small as possible I used an ESP-WROOM-32 which can be picked up for under £4 on AliExpress. I housed mine in the case from an old faulty USB Bluetooth stick so it can be plugged into any USB 5v charger.

ESP32 presence detector wiring

ESP32 USB stick

I used a HT7333 to drop the 5v to 3.3v and put a capacitor across the output as I was getting brownout reboots. I’m currently running the detector to make sure long term stability is good, so far everything is working well.

2 Likes

Is there a resistor going from 3.3v to the reset pin? (hard to tell from the photo)

I used a 10k SMD resistor between 3.3v and the “EN” pin (between 2nd and 3rd pins from the top on the right)

1 Like

I have room assistant working on a raspberry pi zero w together with a nut 3. The publishing interval of the nut 3 is every 2 seconds it seems which is fine. Room assistant is set up to send every 5000 milliseconds an mqtt message. It now sends an mqtt message at an highly irregular interval, sometimes every 10 seconds and sometimes not for minutes at a time. While testing with an ibeacon app on my phone it gives updates with regular intervals so I don’t think it is due to room assistant. Anyone else experiencing this problem? Maybe a good tip for a cheap configurable alternative ibeacon/ble beacon?

You probably want to change the timeout value in the MQTT_presence setting. It should be longer than the update period in room assistant or it will show n/a. The Nut and similar devices do go to sleep if they are unpaired but the Nut is pretty sensitive so it’s easy to wake it up if it’s in your pocket.
There seems to be a market for a cheap configurable portable beacon but it would have to be a mass market device to get it to the right price point (about £/$/€10) and it may be that it’s less useful than Zanzito and Find or similar, which I have to admit I now use.

I use multiple rpi zero w ble for home presence to have coverage with same major and minor id. Could I instead use room assistant and still use for home presence and room presence ?

Eg. Particular room presence using room assistant tied to scripts with lights etc and any room presence as “being home” for other scripts for home presence irrespective of which room is activated. Any thoughts or suggestions ?

I now have the amazefit bip smartwatch, and it can be used as ble beacon.
Batterylife should be arround 20-30 days.

It gets detected with the Happy Bubbles nodes.

This is awesome! Thanks for creating it and I’m going to try it one of my ESP32 modules soon. Things have been pretty busy for me so I have not been checking forums as much but I’m happy other people are taking on this challenge on the ESP32. It’s what I wanted is for Happy Bubbles detectors to be very low cost and DIY, but I just could not get a performance I was happy with, I hope it improved.

Also, if you want to take a stab at detecting iBeacon and Eddystone types, have a look at my parsing code here: https://github.com/happy-bubbles/presence-firmware/blob/master/user/user_main.c#L305 in my firmware. Maybe it’s useful for you. It’s in C so should be usable in Arduino hopefully.

1 Like

Thank you for the info, me too have been busy and didn’t make a lot of progress on this, it seems 1technophile also integrated bluetooth detection via ESP32. My next step will be to check the integration from 1technophile.

Hi all,
I’m trying to setup room assistance, but I have some issues.

If I enable ble in the config file (set to true) I get this error:

module.js:471
    throw err;
    ^

Error: Cannot find module '../build/Release/binding.node'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/room-assistant/node_modules/bluetooth-hci-socket/lib/native.js:3:15)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node index.js "index.js"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-03-10T15_17_56_852Z-debug.log

If I disable ble (set to false) room assistant start but obviously is useless.

This is the ble section in my local.json config file

},
  "ble": {
    "enabled": true,
    "channel": "room_presence",
    "max_distance": 3,
    "blacklist": [],
    "whitelist": ["6708eda4278d"],
    "use_mac": true,
    "system_noise": 0.01,
    "measurement_noise": 3,
    "update_frequency": 5000
  },

Any hint? what am I doing wrong?
thank you

EDIT:
npm is updated to the latest version.
I’ve tried with latest version of node and with version 6.11.5 as suggested in previous posts. Same result.

ever get an answer?

Sometimes I get similar errors, usually when I upgrade npm or node.
My solution is always the same:

  • Backup your config file
  • Uninstall room-assistant & delete the folder
  • Git pull & reinstall room-assistant
  • Restore config file

PD: I’m on mobile, sorry for the speechless instructions.

thanks. I was missing a comma after true under mqtt:)

I think I am missing something here. I have done the above and have 2 pis running the room presence and 2 sensors in ha. I can see both sensors and they are constantly updating distance like I would think they should.

But where is the software that ties this together. Do the math to determine which room I am actually in. I’d like to use this for both home and room presence if at all possible.

I’m using 2 pi3s and a tile. Just opened and activated don’t even have he app. Hat parts seems to be fine since both pis are updating at the same time.

Thank you

So is any one actively developing this on the ESP32 ?
@touliloup @1technophile @sfgreenwood @happybubbles