By James Nebeker
Beacon™ proximity technology is an exciting tool already being used by retailers and developers to build location-aware apps to improve user experience and much more. In this post, we’ll show you an easy way to harness iBeacon™ technology in your own home, without having to do any serious programming or developing. In this simple example, we’ll be using a Raspberry Pi to turn on a light whenever it detects a specific beacon.
Beacon Development Kit (alternately, you can supply your own Raspberry Pi, bluetooth LE dongle, and SD card)
Standard micro-USB 5V cell phone charger
Jumper wires (recommended) or household wire
Desk lamp or other light source
Other tools you will need:
Proximity Beacon (compatible with iBeacon™ technology)
Computer monitor with HDMI input
Small flat-head screwdriver
Network cable for internet access (optional)
Before we put this all together let’s go over each part individually, for starters we’ll assume you have your Raspberry Pi already set up (with BlueZ, the Linux bluetooth stack, installed) and have some familiarity with basic Linux command line operations. If not, or if you want a quick introduction to the Raspberry Pi and iBeacon™ technology, check out our blog post, How to Make an Beacon Out of a Raspberry Pi, to get acquainted with Linux, the Raspberry Pi, and iBeacon™ technology.
Scanning for beacons
The ability to scan for nearby proximity beacons is the latest feature of the Beacon Development Kit, new to Version 3.0. In this guide we will be using the newest development kit software to do the scanning. Information on how to download this update can be found here. If you already have your own Raspberry Pi and bluetooth dongle you can create your own beacon scan script using the steps I’ve outlined in this post.
Before we can start scanning we need to make sure we have a beacon nearby to test with. The easiest way to do this is probably to download our free Locate for iBeaconapp for iOS. You can also check out our products page for other beacons to test with. For this tutorial, I will be using a test beacon with the following identifiers:
Once you have your test beacon up and running, let’s do a quick test scan. First, make sure the development kit isn’t currently broadcasting using the
ibeacon stopcommand, then run the following command to start a scan and you should see the identifiers of your test beacon.
$ ibeacon scan UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -59 UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -59 UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -59
To exit a scan press control-c.
You may experience some instability when using the new scan feature for extended periods of time in high-traffic areas due to an error where the bluetooth adapter enters a bad state. The only way to recover from this state is to power cycle the adapter or reboot the machine. We are continuing to work to minimize this issue and will be providing additional updates.
For triggering the light in this example, we’ll be using the
-b option for bare output. Go ahead and try it out, and you should see something like this:
$ ibeacon scan -b 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -59 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -59 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -59
Now that we can detect nearby beacons with the Raspberry Pi, the next step is turning the light on and off.
Controlling the light
To control the light we will be taking advantage of the GPIO pins on the Raspberry Pi, which can supply a voltage when commanded, and the PowerSwitch Tail II, which switches power from a wall socket with a small control voltage. The first step is to connect the power switch to the GPIO pins on your Raspberry Pi, we recommend using jumper wires to make things easier. The diagram below shows the wiring configuration, the ground pin on the Raspberry Pi is attached to the negative input on the switch and the GPIO1 pin on the Pi is attached to the positive input on the switch. Use a small flat-head screwdriver to secure the wires into the two slots on the power switch.
Once everything is connected, go ahead and plug the power switch into the wall and plug your light into the switch. Make sure any switches on the light itself are in the on position. Now we’re ready to test the power switch, to control the voltage on the GPIO pins we’ll be using the Wiring Pi library which is installed by default on the Beacon Development Kit. The first step is to change GPIO 1 into
OUT mode. Then command the pin to turn on, which will activate the power switch and turn the light on. To do this run the following commands (shouting “Let there be light!” is optional):
$ gpio mode 1 out $ gpio write 1 1
You can turn the light off by switching GPIO 1 off:
$ gpio write 1 0
Now all that’s left is to combine this with beacon scanning and we’ll have an automated beacon lightswitch.
Putting it all together
We’re going to write a little bash script that will read the input from a beacon scan line by line and search for a specific beacon’s identifiers. Once it detects the beacon it will switch on the light, if you exit the script with control-C the light will be switched off. Use your favorite text editor (e.g., vi, nano) to create the
welcome_light script seen below:
$ nano welcome_light
Paste in the contents of this block:
#!/bin/bash gpio mode 1 out trap "gpio write 1 0" exit while read line do if [[ `echo $line | grep "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1" ` ]]; then gpio write 1 1 fi done
Be sure to change the beacon identifiers in the if statement to the ones you’ll be broadcasting with your test beacon. Save this file and get your beacon ready, make sure it is off for to start the test. Now run the following command to start the scan and light switch sequence, it starts a beacon scan with the bare output option, piping its output into our
welcome_light script. If you made your own scan script, just run that script and pipe its output to
$ ibeacon scan -b | ./welcome_light
Once the scan is started, activate your beacon and you should see the light turn on automatically. Check out the video clip below to see the demo in action.
For a homework assignment, you can build on this script to turn off the light when it no longer detects the beacon (i.e., when you leave the room).
We hope this quick tutorial gave you a good example of the power of proximity beacon technology. There are tons of other cool ways to take use beacons with a Raspberry Pi, we look forward to seeing what everyone can come up with.