Raspberry Pi & Bluetooth
The objective of this project is to generically assess the Bluetooth capabilities of the Raspberry Pi and to see what applications we can put it to. Specifically, we are interested in tracking whether devices are detectable, e.g. are family members are home. This is part of the functionality we would like to deliver in our smart front door project.
Knowing whether one or more family members is at home is a powerful tool to improve our home automation system. The heating, hot water and other things use the house status to set target temperature levels, etc. The house status is currently manually set but, anything we can do to automate this and augment it with accurate, real-time data would simplify the user experience and improve over all efficiency.
Note: This project is now an integral part of the presence detection system in our home.
For this project we are testing with two different Bluetooth dongles.
This is a micro Bluetooth USB dongle. These are available everywhere and are very cheap (~£2). This was originally bought to Bluetooth enable a Dell laptop that didn't have built-in Bluetooth.
This is an old Zoomtel USB dongle with a large aerial. Our thinking is that this will help with range, despite this being an obsolete device now. We have done the majority of our testing with this device.
Initially, we are doing all out testing on a mini-ITX PC running Debian. Our testing has shown that these dongles need to be inserted into a USB port on the Raspberry Pi and not via a USB hub.
Whilst we could get this to work on other Linux machines, we struggled a bit on the Raspberry Pi until we used the latest build, called
Packages & Tools
You only really need to install the 'bluetooth' and 'bluez-utils' packages but, we used the following command to install some test tools and the GUI Bluetooth manager:
sudo apt-get install bluetooth bluez-utils blueman. You may need to run
sudo apt-get install first. This takes some time to complete!
USB Dongle Detection
lsusb shows a new device:
Bus 001 Device 008: ID 0a5c:200a Broadcom Corp. BCM2035 Bluetooth dongle (this is the Zoomtel dongle)
/etc/init.d/bluetooth status command shows whether Bluetooth is running.
hcitool dev will confirm the device can be seen.
Switch your phone's Bluetooth to 'visible'. We then used the hcitool command to discover our devices initially:
hcitool scan showed the following:
A0:75:91:01:02:03 Nexus S
For iPhones, you just need to go into 'Settings', 'General' and then 'Bluetooth', to make the device discoverable and to start it scanning.
Once you have the Bluetooth MAC address of your devices, you can still ping them, even when your bluetooth is set to 'hidden'. This is done using the
l2ping command (which needs root permissions via sudo). Note this command starts with an l (as in the letter L and not a number one)!
sudo l2ping -c 1 EC:9B:5B:00:00:00 (example MAC address) returns:
Ping: EC:9B:5B:00:00:00 from E8:39:DF:00:00:00 (data size 44) ...
44 bytes from EC:9B:5B:00:00:00 id 0 time 64.82ms
1 sent, 1 received, 0% loss
We did a few experiments to see just how good the range is using the Zoomtel dongle and an iPhone 4 and we could detect its presence from one end of the house to the other. We could also see that the ping time was giving a rough indication of range. The phone in the same room took about 68mS to ping. By our front door it was about 100mS. In the conservatory on the back of our house it was about 118mS. We could even pick up the iPhone 4 whilst in a car on the drive! :-)
We also noticed that other devices appeared when we ran the
hcitool scan command. This included laptops in our home. This means that we have the ability to track usage of these devices by our children.
The Sony PS3 Bluetooth address is not an easy bit of information to get hold of. Sony make it deliberately difficult but, it can be done with the right tools. A PS3 controller must pair via USB first to communicate with the PS3. This pairing process writes the PS3's Bluetooth address to memory on the controller, and it also adds the controller to the PS3's allowed devices list. Without this pairing process the PS3 ignores all communication from the controller. This is process means that the PS3 doesn't make itself visible for pairing.
The easiest way to programmatically test for the presence of devices in our home is going to be via a script that tests the output of the l2ping command.
sudo l2ping -c 1 EC:9B:5B:00:00:00; echo command returned $? returns the following:
Ping: EC:9B:5B:00:00:00 from E8:39:DF:00:00:00 (data size 44) ...
44 bytes from EC:9B:5B:00:00:00 id 0 time 20.82ms
1 sent, 1 received, 0% loss command returned 0
When the device is off, the command returns:
Can't connect: Host is down
command returned 1
So if l2ping returns 0, the device is pingable (and thus in range) and if it returns 1, it is not. We have a written a script to check our set of known Bluetooth MAC addresses, looking for devices that are reachable.
Our script searches for known devices (iPhones, Android phones, laptops, games consoles, etc.) and pings them to see if they are reachable or not. It then uses socket layer comms to inform our Home Control System (HCS) if the status has changed. We also plan to capture unknown devices and log them as and when they appear.
Initial results looked quite promising but the devices do sometimes appear unreachable for short periods of time. We have since implemented a simple algorithm to handle these 'drop outs' and now have a reliable and timely indicator of whether a device is present in our home or not.
We have a noticed a few issues during testing with our script over the last few weeks:
- The USB dongle seems to lock up occasionally and has to be removed and re-inserted.
- iPhones don't enable Bluetooth unless they are paired to one or more devices. The iPhone 3GS doesn't respond to pings and always appears off-line though.
- The iPhone 4 appears to be unreachable for short periods of time and we needed a software algorithm to hand this. Despite the regular pings, there is no real impact on battery life.
- The iPhone 4S is much more reliable in responding to regular pings but, its battery takes a big hit in doing so. From fully charged to about 40% over night.
The l2ping tool is written in C, so it makes a lot of sense to use this code and this language to write a program to do what we require.
To compile C, we have installed the 'build-essential' package to install gnu C and C++ compilers.
C code compiled on our x86 mini-ITX test machine won't work on the ARM-based Raspberry Pi. It needs to compiled on the Raspberry Pi.
The thinking is that the Raspberry Pi with Bluetooth capability will form part of our smart front door project and will thus be sited close to our front door. This means that it will detect phones (people) as they approach the house and they don't necessarily have to be in it.
As well as using this information to update our house status, we could use it to do things like automatically switch on the convenience lighting over the front door at night time, when someone arrives back at home. This would augment the PIR sensor activations.
This is yet another project that shows how temperamental the USB ports are on the Raspberry Pi. It has been frustrating when our Bluetooth devices work in our mini-ITX test machine running Debian but, the experience is so much more complicated on our Raspberry Pi.
What we have shown so far is that Bluetooth is a viable way to track if people (and their mobile phones) are in the house or near the house. This can be done very accurately, securely (i.e. without devices been visible publicly) and with very few errors by using a fairly simply software algorithm. It can also be done in a timely and responsive manner (i.e to within about 60 seconds).
The same approach can also be used to log how long device such as computers and games consoles are used (if they use Bluetooth). This project is now an integral part of the presence detection system in our home.
This project has been a bit of an eye opener in terms of privacy and personal devices, such as Smartphones. Devices that make themselves visible over Bluetooth are vulnerable to being tracked. It would appear that many people don't realise that their Smartphone is making its Bluetooth MAC address visible to the world. Once you have the Bluetooth MAC address of a device, you can ping it to see if it is in range and there is nothing the device owner can do, short of turning the Bluetooth radio off. On many devices, this is not really an option.
One characteristic of Bluetooth that makes things worse, is that it has a decent range. It is therefore possible to pick up devices in the street or in neighbours houses. In just a few days, we saw over 35 devices go past our house. Some of them made regular appearances and it would be possible to predict when they might appear. Many had an ID that was also made public and it was often the make and model of the device and in some cases the name, nickname or initials of the owner.
Some devices were spotted very regularly and it quickly became obvious, that these devices belonged to neighbours. This information could easily be used to determine if a house was occupied.
Knowing the Bluetooth MAC address of a device will also allow you to identify the type of device being used. There are websites that can lookup this information for you, since address ranges are registered to device manufacturers.
Credits & Further Reading
I have to thank my friend (and Linux expert) @cmarkwell for help with this project.