Bluetooth / iBeacon Presence Sensor with Raspberry Pi scanner

This Presence Sensor has two components - a plugin for Vera that provides a new virtual device type for keeping track of presence states and a Raspberry Pi that does the actual polling and updates the virtual devices as changes are noted. Due to the size of the image, I have made the disk image for the Raspberry Pi available on my website.

The Raspberry Pi image supports polling for known bluetooth devices (gets around iPhones going to sleep) and also continuously searches for ibeacons and can check them against known addresses as well.

The Raspberry Pi sits near the front door in our house and provides coverage of the road outside, our driveway, as well as most of the house. I am using this plugin to monitor my own and my wife’s iPhone and I also have an ibeacon stuck to the windscreen of each of our cars to detect when they are home.

Some experimentation may be needed to find the optimal placement to get the required coverage as the Pi does not have a high-gain antenna for Bluetooth. Better results may be had by using a USB bluetooth adapter with external antenna.

[size=18pt]Vera Presence Sensor Virtual Device Component[/size]

The Presence Sensor Virtual Device was modeled on Ping Sensor and allows the presence or absence of the monitored device to be used as a trigger for scenes or PLEGs. It was written to be used with the Raspberry Pi image outlined below, but should be usable with anything that can send HTTP requests to the Vera unit to update the status (such as apps like Tasker).

To install the plugin, unzip the plugin files and upload to your Vera unit using the Apps → Develop Apps → Luup Files function.

Once the files have been uploaded and Vera has restarted, you will need to create one or more devices. Go to Apps → Develop Apps → Create device. Set Description to the name you want the device to be called (Bob’s iPhone) and the Upnp Device filename to “D_PresenceSensor.xml”

If you are using my Raspberry Pi image, on the settings tab of the device page you need to enter the Bluetooth MAC address of the device you want to monitor and whether it’s a bluetooth device or ibeacon. How to find this MAC address is left to an exercise of the reader - there are numerous bluetooth scanners for both major phone platforms.

The plugin has been tested on UI7. I believe it should also work on UI5, but I don’t have any UI5 systems to test.

The latest version will always be available from my website at [url=https://afoyi.com/software/vera-presence/]https://afoyi.com/software/vera-presence/[/url]

[size=18pt]Raspberry Pi Disk Image Component[/size]

The disk image for the Raspberry Pi will fit on a 2GB or larger MicroSD card and is set up to simply be written to the card and just one configuration setting to be set (the local IP address of your Vera unit). The instructions for writing the image to the SD card depend on the operating system that you’re using, but are identical to the instructions for getting any other image for the Pi installed.

One difference in my image is that after it boots up it leaves the root filesystem mounting read-only and creates a few ram drives to run from. This means that power can be pulled at any time and there is absolutely no risk of corrupting the file system. If changes are needed to be made, there’s a command called ‘remountrw’ which can be run to mount the disk into read-write mode so the changes can be made. Once the changes have been completed, don’t forget to run ‘remountro’ to put it back into read-only mode. As the ramdrives shadow files that sometimes need to be edited (particularly /etc), the root filesystem is bind mounted under /ro - so you can make peristent changes to /etc by editing the files under /ro/etc/ and then rebooting.

Logging In

The image is set up using the standard Raspbian credentials of the username ‘pi’ and password ‘raspberry’. It can be accessed using a USB keyboard and monitor plugged into the HDMI port, or via SSH.

Network Configuration

The image is configured to automatically DHCP an address on the wired ethernet port. If you have a Raspberry Pi 3, or a USB wifi adaptor, it can also be configured to connect to wifi. To do this, remount the file system in read-write mode, edit /ro/etc/wpa_supplicant/wpa_supplicant.conf appropriately and then enable the wifi interface in /ro/etc/network/interfaces.

Vera Configuration

To configure the address of your Vera unit, remount the file system as read-write and edit the python scanner script at /ro/srv/scanner/run_scanner.py. All you need to set in this file is the IP address your Vera unit has on your local network at the top of the file.

Reboot your Raspberry Pi and it should automatically start the scanner script, connect to your Vera, find the PresenceSensor devices and start to poll them.

Updating

The image is using two main libraries to do the bluetooth work, plus my own containing the scanner script. All three can easily be updated using git.

ibeacon-scanner by John Shovic <john.shovic@gmail.com>
[url=https://github.com/switchdoclabs/iBeacon-Scanner-]https://github.com/switchdoclabs/iBeacon-Scanner-[/url]

pyvera by Cyber MacGeddon <cybermaggedon@gmail.com>
[url=http://forum.micasaverde.com/index.php/topic,37927.0.html]http://forum.micasaverde.com/index.php/topic,37927.0.html[/url]
[url=https://github.com/cybermaggedon/pyvera]https://github.com/cybermaggedon/pyvera[/url]
(my fork) [url=https://github.com/daemondazz/pyvera]https://github.com/daemondazz/pyvera[/url]

scanner script
[url=https://github.com/daemondazz/vera-presence-scanner]https://github.com/daemondazz/vera-presence-scanner[/url]

Any of those libraries can be updated by remounting in read-write mode, changing into the appropriate directory under /ro/srv/ and running a ‘git pull’

The full image can be downloaded from my website at https://afoyi.com/software/vera-presence-pi/

Awesome.

Was waiting for something like this to pop up - Arduino would be cheaper (assuming it’s up to the job) but I guess it’s time to order a Pi.

Thanks.

Aussie Aussie Aussie!

The biggest issue with the Arduino would be the network access - if you already have an Ethernet shield (or a board with it built in) then I can’t see why you couldn’t get it to work.

If you don’t already have an Ethernet Shield, then it would be more expensive than the Pi.

While I wait for my Pi - couple of thoughts for useful features going forward:

Use the same mechanism that the Virtual Motion sensor does so that there’s one instance, but lots of child devices for each motion sensor, you dial in the number of children you want as a variable on the master - this reduces the amount of memory used on Vera for separate device instances (as I understand it). I’m going to have at least 6 monitored devices.

Return the total number of Bluetooth Devices detected (either specifically monitored, or just all it can see - want to know if your kids are having a party while you’re on holiday? MAC count > 20)

Include the RSSI for each monitored Bluetooth MAC - this can be used to make decisions based on proximity to the Pi, which may not sound useful until…

Adding a second or Third Pi… each of these would have a unique master instance as per the Virtual motion sensor plugin… then using multiple RSSI values you can start to triangulate where a device is either in PLEG, Luup code or perhaps an extension to the Plugin.

Yep, I’ve already been giving a thought how to have multiple Pi’s sharing information. Still working on it.

Including the RSSI of each station is a great idea and one I hadn’t thought of, as is how many devices are found. That could be broken down by type (bluetooth vs ibeacon). The only issue I see there is if we don’t know about a bluetooth device, then we won’t see if if it’s not in discoverable mode.

I’m not sure how to make a parent device that contains all the child devices, will need to keep doing some research - I’m quite competent at Python programming, but this is my first real foray into Lua.

Would you foresee any issues running the Python code on Windows box? I have one that is on anyway not doing much.

I haven’t tried, but as long as you can get the bluetooth stack working on Windows (see GitHub - karulis/pybluez: Bluetooth Python extension module), it should be fine as the rest of the code is platform agnostic.

This is an interesting avenue in the quest for reliable presence detection. I have been thinking along the lines of the “wasp in a box” method myself. Couldn’t this be done directly by the Vera itself?

I have a Vera Edge which doesn’t have bluetooth support, so would need to use a USB dongle. Does the Vera Plus support BTLE?

The other aspect was I figured the ideal placement for the Vera unit may not be the same as the best placement for the bluetooth scanner and I was also planning on allowing multiple Pis to increase coverage from the start, just need to implement it.

Is the script using BLE or just standard Bluetooth? I’m looking to purchase a dongle to see if I can get it to run on a Windows machine and want to ensure I get the right one.

iBeacons use BTLE, which the script detects, so in theory it needs a BTLE compatible adapter.

If you’re only going to be doing BT probing, you could rip out the parts of the script that deals with iBeacons.

Specs for the plus say Bluetooth 4.0 BLE… I’m going to assume BLE is low energy.

Yep, thanks for checking that :slight_smile:

Next question, can you run Python on the Plus, or is it going to require re-writing in Lua?

anyone know how to implement this on an existing Rpi3

mainly… How do I install the libraries?

[quote=“daemondazz, post:13, topic:192938”]Yep, thanks for checking that :slight_smile:

Next question, can you run Python on the Plus, or is it going to require re-writing in Lua?[/quote]

I think it’s probably possible, but it’s questionable as to whether it’s a good idea. Everyone will have an opinion but keeping it in Luup will make it a lot more accessible for people, easier to pass data around and less likely to outright break Vera.

[quote=“konradwalsh, post:14, topic:192938”]anyone know how to implement this on an existing Rpi3

mainly… How do I install the libraries?[/quote]

ok… so its not that hard actually

I just used the GIT CLONE command with the above urls and then place all files into the same folder .

I have however hit a wall :

python run_scanner.py Traceback (most recent call last): File "run_scanner.py", line 27, in <module> v = vera.VeraLocal(VERA_IP) File "/home/pi/Scanner/vera.py", line 1713, in __init__ Vera.__init__(self) File "/home/pi/Scanner/vera.py", line 1335, in __init__ self.update_state() File "/home/pi/Scanner/vera.py", line 1408, in update_state s.definition = SceneDefinition.parse(self, i) File "/home/pi/Scanner/vera.py", line 1044, in parse sd.actions.append(Group.parse(vera, i)) File "/home/pi/Scanner/vera.py", line 952, in parse aset.actions.append(Action.parse(vera, i)) File "/home/pi/Scanner/vera.py", line 589, in parse s["service"] RuntimeError: Don't know how to handle service urn:upnp-org:serviceId:altui1

For your reference I added these two lines to get me up and running

[code] if s[“service”] == “urn:upnp-org:serviceId:altui1”:
return

    if s["service"] == "urn:toggledbits-com:serviceId:DeusExMachinaII1":
        return 
		[/code]

This allows it to run and all works 100%

I am using a RPi3 with onboard BT BLE running OpenLuup and HomeAssistant
and your Scanner

Thanks for your hard work

[quote=“daemondazz, post:13, topic:192938”]Yep, thanks for checking that :slight_smile:

Next question, can you run Python on the Plus, or is it going to require re-writing in Lua?[/quote]

No, unfortunately it would have to written in LUA. Seeing as I’m hoping to bring my RPi into the OpenLuup fold for inclusion into my Vera network, might not be necessary…

The Windows Bluetooth stack does not work correctly with lookup_name. Once cached, it does not seem to get removed. Using find_service will respond if the device is in range. So changing the following line:

found = bluetooth.lookup_name(phone, timeout=5) is not None

to

found = len(bluetooth.find_service(address = phone)) > 0

I’m not sure if phone in the script is coming in a string for the address. If not, may need a tweak.

If you change this like and remove the ibeacon code, it should work on Windows. I’ll be doing more testing later.

[quote=“konradwalsh, post:17, topic:192938”]For your reference I added these two lines to get me up and running

[code] if s[“service”] == “urn:upnp-org:serviceId:altui1”:
return

    if s["service"] == "urn:toggledbits-com:serviceId:DeusExMachinaII1":
        return[/code]

This allows it to run and all works 100%[/quote]

If you cloned my fork of the PyVera and not the original author’s, I have a tuple at line 582 which gives a list of ServiceIds to ignore.

On my todo list is to actually write a readme, which would include instructions on how to install on an existing system. There is also a systemd startup file in /etc/systemd/system which allows it to auto start at boot, which I haven’t got in the repository yet.

[quote=“konradwalsh, post:17, topic:192938”]I am using a RPi3 with onboard BT BLE running OpenLuup and HomeAssistant
and your Scanner[/quote]

I haven’t had a chance to look into OpenLuup yet, but it is on my todo list.

Thanks :slight_smile: