Introduction: Raspberry Pi Bluetooth In/Out Board or "Who's Home"

Picture of Raspberry Pi  Bluetooth In/Out Board or "Who's Home"

I wanted to create an electronic In/Out Board. The type you may have to record who is present or not in a company or home. It is usually placed by the front entrance so anyone can see who is in or out.

I also wanted another project for my Raspberry Pi, so that is why I am using it, but it makes a really good lower power choice of computer to drive this application. There are many extensions to this basic instructable, because of the Pi, like run a web server to show who is in and out via a web page, or run a local LCD display.

There is one major assumption here, that everyone carries a mobile phone and they are willing to keep bluetooth on (is that two assumptions? :-/). Their bluetooth must be on but it is NOT necessary to be 'discoverable' for this to work. Each person must only make there device discoverable for a short time so you can determine their bluetooth address. Once that is found, this device will just query the presence of that known bluetooth device.

I call it my "Who's Home" device.

Step 1: Raspberry Pi Setup

Picture of Raspberry Pi Setup

Let's get started.

You will need:
A Raspberry Pi computer, I tested with the 512 MB model, with internet connection.
An SD card loaded with the latest Raspbian “wheezy” software. See http://www.raspberrypi.org/downloads for how to get that setup.
A supported bluetooth USB dongle. See http://elinux.org/RPi_VerifiedPeripherals#USB_Bluetooth_adapters
Some way to access a terminal on the Pi. I used ssh, or just use the normal desktop and open Terminal.


Step 2: Install Bluetooth Software

Picture of Install Bluetooth Software

At the terminal type the command

sudo apt-get install bluez

this will take a little while to install the bluetooth drivers and software.
Also needed is the the pybluez interface from Python to bluetooth.

sudo apt-get install python-bluez

If you have not already, insert your bluetooth dongle into a USB port on the pi.
At the terminal you can use hcitool to check your device, e.g. type

hcitool dev

Step 3: Get Bluetooth Device Addresses

Picture of Get Bluetooth Device Addresses

You can use the inquiry.py script from the pybluez website to query the nearby discoverable devices.
https://code.google.com/p/pybluez/source/browse/trunk/examples/simple/inquiry.py

At the pi terminal:
wget https://pybluez.googlecode.com/svn/trunk/examples/simple/inquiry.py
python inquiry.py

You will need the long address, numbers separated by :'s for the next step.


Step 4: Run the Basic In/Out Board

Picture of Run the Basic In/Out Board

You can get my basic In/Out Board python script using this command:

wget https://raw.github.com/paulbarber/raspi-gpio/master/inoutboard.py

Insert your device addresses and person names into the script. Repeat the 'if' block for each person.
Run it with:

python inoutboard.py

Note that the devices no longer need to be discoverable for bluetooth.lookup_name to work.
Play and make it better!

Comments

dconsuegra (author)2017-12-06

I'll be sure to use this in the next couple of days... Probably using the Bluetooth route.... but I'd like to be able to send the "Daniel is home/has left" command to Smartthings.... Any ideas? Thanks!

ShaneH26 (author)2017-09-18

Error 404: file not found.... please advise. https://code.google.com/p/pybluez/source/browse/trunk/examples/simple/inquiry.py

prb3333 (author)ShaneH262017-09-18

It appears to have moved to:
https://github.com/karulis/pybluez/blob/master/examples/simple/inquiry.py

Cheers,
Paul

KeoO (author)2017-04-25

I have a Raspberry Pi 3 so now bluetooth is standard with the device. There are also built in functions. Run "hcitool scan" to scan for discoverable bluetooth devices. I am going to re-write the code to pull a user list from a JSON config file. To run bluetooth and wifi, add both sets of credentials to each user.

Hydrostevo (author)KeoO2017-05-13

Cool, I would be interested to see what you get. I am looking for a solution using BLE beacons instead of smart phones running on Raspberry Pi 3 with Jessie.

wv110 (author)2015-09-08

I'd like to build this inside my car. Then when I approach the car it unlocks. When I leave it locks. Getting tired of fob not working...Any comments?

JasonR238 (author)wv1102016-12-11

I'm working on this right now, but instead of wiring it directly to the door locks and all that extra work, I'm just disconnecting the battery ground in my key fob and running the wires to the relay controlled from the pi (one wire from the negative of the battery and the other wire back to the ground of the key fob). So the key fob becomes powered again when phone is in range. I will probably wire it so that it sends a lock command when out of range because it won't let me lock the car with the key fob powered and inside the car. Or use a manual input to shut off the key fob when the car stops running so then the car can be locked as you get out as well.

cchiosa (author)wv1102015-09-28

It could be a potential problem regarding security... the long addresses are spoofable. But then again, who is going to know that you are scanning for bluetooth in the area?

Also, you'll have to tangle the beast that is car security systems.

But if you do manage, let us know how you did it! Good luck!

CesarD17 (author)2016-02-20

Hi,

I like this script, but i don´t get that inquiry.py work on mi RasperyPi2, this is the error message when i type "python inquiry.py":

performing inquiry...

Traceback (most recent call last):

File "inquiry.py", line 12, in <module>

nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True, flush_cache=True, lookup_class=False)

TypeError: discover_devices() got an unexpected keyword argument 'lookup_class'

Thanks you for your help.

RomanS59 (author)CesarD172016-10-15

I was able to get it to work by manually removing the ", lookup_class=False" part. According to https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=159442 it's deprecated?

moldaker (author)CesarD172016-03-04

The script still works, I was getting the same error, make sure that Bluetooth is set up on the Pi, and the device is paired and trusted before running the script.

DS56 (author)2016-07-03

I am a nube to python and programming, but have a project in mind this will help me with.

This Works pretty good and i'm very interested in getting this to work for me, however, I noticed that as long as John is found, then it "looks" for Paul, then Paul shows either In or Out correctly, but if John is not found, it assumes Paul is not found and does not try to "look" for Paul, and just prints

John: Out

Paul: Out

If i comment out John's section, then the Paul's works as expected.

Please Help. Thanks

prb3333 (author)DS562016-07-05

Hi,
Is it an issue with the line indentation?
Python is fussy about how specific lines are indented to make the 'if' statements work correctly. If your indentation is with spaces, check all the indents have the same number of spaces.
Hope that helps,
Paul.

PranveshC (author)2016-06-08

unable to get inquiry.py code from https://code.google.com/p/pybluez/source/browse/trunk/examples/simple/inquiry.py and https://raw.githubusercontent.com/karulis/pybluez/master/examples/simple/inquiry.py

BudByrd (author)2016-04-11

Nice script. Added GPIO commands to run status LEDs.

DanielW166 (author)2016-04-08

Hi all,

I'm not a programmer nor do I own a Raspberry Pi, but looking at this gave me an idea and I was wondering if it's feasible so need your help:-

I work in a primary school and thought that for the safety of the children how accurate would it be if they wore some kind of bluetooth tag, or wifi tag, to indicate where they were? If there were Pis placed around the building, and the nearest Pi with the strongest signal picked up a kid's tag -- would that work?

Could this go anywhere?

Im aware these kind of systems exist in hospitals in the UK but use different sensors and such -- can it be done using rasperry pi? If so, how accurately?

jwarren20 (author)2016-02-29

Nice project however I don't think Bluetooth is the best way to do this function due to it's limited range.

A much better way is to use the WiFi connection that already in place, the Pi doesn't need WiFi it's self, and detect the MAC address of the connecting phone. This has the advantage of much larger range than you could ever get using Bluetooth. There is also not any limitation other then the number if devices the local network can handle.

In a larger installation with multiple networks you'd need a Pi on each network. Then each Pi would inform the other Pis who's on which network. So at work you would not only know they are there but which building or floor they are in.

Think about it and consider updating with the option for WiFi connection.

CesarD17 (author)2016-02-20

Hi,

I like this script, but i don´t get that inquiry.py work on mi RasperyPi2, this is the error message when i type "python inquiry.py":

performing inquiry...

Traceback (most recent call last):

File "inquiry.py", line 12, in <module>

nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True, flush_cache=True, lookup_class=False)

TypeError: discover_devices() got an unexpected keyword argument 'lookup_class'

Thanks you for your help.

dyna67 (author)2015-12-17

How would I adapt the code so that it logs the number of visitors to the house? I want to store the number of guests in like a .txt file

KavyaG3 (author)2015-12-06

Can anyone help me with the same project but to be done with arduino
And what bluetooth device must be used to scan nearby bluetooth devices?

aschlüter1 (author)2015-10-06

Would this possible over LAN only? And also trigger a special event if XX comes home?

LariceN (author)2015-07-14

I'm trying to do this project for a science and don't dont know how to fix the script instep four to put my device address and my name

prb3333 (author)LariceN2015-07-18

The script is just Python. Edit it in a text editor, e.g. use the nano command or gedit if you have the desktop running.
Hope you figure it out...

LariceN (author)2015-07-02

is it possible to make a program that reads the names out loud as they come in

prb3333 (author)LariceN2015-07-02

I am sure that would be possible. You could try one of the text-to-speech solutions mentioned here:

http://elinux.org/RPi_Text_to_Speech_%28Speech_Syn...

There is also a post below about executing command line programs from python if you need it.

DonG12 (author)2015-05-22

The inquery script has been moved this this URL:

https://raw.githubusercontent.com/karulis/pybluez/master/examples/simple/inquiry.py

oguime (author)2014-06-17

Worked nicely but the range was only about 4 meters with a Cambridge Silicon Radio bluetooth dongle (HCI mode) and a Moto X.

What distance can you get before loosing contact?

prb3333 (author)oguime2014-06-17

Inside the house the range was about that, maybe a little more. My house is not that big :-)

ARMmaster17 (author)2014-03-08

Thanks. I'm going to start building it soon. The end product I want is when I walk into my room, the RPi will detect the phone in my pocket and start playing a pandora stream. One question though, how do you execute command line (or terminal) commands from python?

prb3333 (author)ARMmaster172014-03-09

Hi, there are a couple of options but I think the best is the call function from the subprocess module. E.g.

from subprocess import call
call(["ls", "-l"])

ARMmaster17 (author)prb33332014-03-16

thanks

stnickt (author)2014-01-30

awesome tutorial. thanks

Can you help me getting inquiry.py running though. I get the following..

pi@raspi ~ $ python inquiry.py

performing inquiry...

Traceback (most recent call last):

File "inquiry.py", line 12, in <module>

nearby_devices = bluetooth.discover_devices(lookup_names = True)

File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 22, in discover_devices

raise BluetoothError ("error communicating with local "

bluetooth.btcommon.BluetoothError: error communicating with local bluetooth adapter

stnickt (author)stnickt2014-01-30

Also I took this and ran..

I combined two search methods. Wifi only works if the iphone screen is on, and bluetooth has such a short range, so I combined the two (makes sense right)

Basically, search for the phones wifi, and if it cant find that, search for the bluetooth.

i have both a python script to search for the phones, and a php script to run on the www page.

Another note: I am pretty new to programming, so this is all from stuff i pulled off the web and kind of put together myself. Might not be the prettiest but it works. Hopefully someone can fine tune it a bit :)

Here is the code (note there are a couple extra lines in there that are used for php to print new blank lines on the web page

whoishome.py

import os, subprocess, time, sys, bluetooth

running = 1

devnull = open('/dev/null', 'wb')

nick = "192.168.1.8"

paul = "192.168.1.13"

mike = "192.168.1.18"

i=0

lstip = [nick, paul, mike]

lstname = ["Nick","Paul","Mike "]

bt = ["00:00:00:00:00:00", "00:00:00:00:00:00", "00:00:00:00:00:00"]

print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())

print "<br />\n" #new line for php web

print "<br />\n" #new line for php web

def printme(pingip):

# Check WIFI IP if IN

if pingip == 0:

print lstname[i], ": in"

print "<br />\n" #new line for php web

# Check BlueTooth if NO IP is found

elif pingip == 1:

result = bluetooth.lookup_name(bt[i], timeout=4)

if (result != None): # If Bluetooth is found, IN

print lstname[i], ": in"

print "<br />\n" #new line for php web

else: # If Bluetooth is NOT found, OUT

print lstname[i], ": out"

print "<br />\n" #new line for php web

#time.sleep(2)

return;

while i < len(lstip):

pingip = subprocess.call(['ping', '-w', '1', '--', lstip[i]],

stdout = devnull)

printme(pingip);

i+=1

And for the php webpage

ping.php

<?php

$command = "python /home/pi/whoishome.py 2>&1";

$pid = popen( $command,"r");

while( !feof( $pid ) )

{

echo fread($pid, 256);

flush();

ob_flush();

usleep(100000);

}

pclose($pid);

?>

prb3333 (author)stnickt2014-01-30

That's a great idea to use Wifi as well. It may be necessary to setup static IP addresses for the devices on the router as some only lease them for a few days. Some-one at raspberrypi.org was try to do similar with MAC addresses:

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=48&t=52570

prb3333 (author)stnickt2014-01-30

Hi, thanks for the comment. I am afraid I am not very experienced with troubleshooting the bluetooth. Mine just works so have not come across this error.

Sorry,

gcklaus (author)2013-10-28

Just reproduced your steps, simple and useful idea. Converted it to a Perl-script, as I prefer Perl to Python. If somebody is interested to do this with Perl:

Step 1: Install module

sudo apt-get install libnet-bluetooth-perl

Step 2: inquiry.pl

#!/usr/bin/perl

use Net::Bluetooth;

my $device_ref = get_remote_devices();

foreach $addr (keys %$device_ref) {
        print "Address: $addr Name: $device_ref->{$addr}\n";
}


Step 3: inoutboard.pl

#!/usr/bin/perl

use Net::Bluetooth;

my @sdp_array = sdp_search("AA:92:A4:44:3D:C9", "", "");

print "Mike: out\n" if $#sdp_array == 0;
print "Mike: in\n"  if $#sdp_array >  0;
       
my @sdp_array = sdp_search("F0:8C:E3:E9:E5:53", "", "");

print "John: out\n" if $#sdp_array == 0;
print "John: in\n"  if $#sdp_array >  0;

JonasEAndersson (author)2013-06-27

Awesome article! Simple and useful just as I like it :)
I will try this on my server (that is already running 24/7 at home) and see if I can't get it turn on my stationary computer when I come home :D

About This Instructable

137,561views

137favorites

License:

More by prb3333:Programming the ATtiny85 from Raspberry PiRaspberry Pi  Bluetooth In/Out Board or "Who's Home"Raspberry Pi Snail Mail System
Add instructable to: