Bluetooth Low Energy

Raspberry Pi as a Beacon

Beacons are one-way transmitters that are used to mark important places and objects. Standards are emerging in the beacon space, including Apple's iBeacon and Google's Eddystone beacon platform. A beacon is detectable to a user's device from a range of a few meters, using Bluetooth Low Energy to provide contextual information to devices and applications. In this lab we will use a Raspberry Pi to create a beacon.

Equipment

  • Raspberry Pi 3
  • SD card with a reasonably current version of Raspbian
  • Mobile phone with internet access
  • Ethernet cable

Raspbian

Put the current stable version of Raspbian on a SSD card. You may have this already.

Configure Raspberry Pi for Headless

We're using the Pi as a beacon so we do not need to access the user interface - we can do what we need using Secure Socket Shell (SSH). It would also be handy if we could connect directly to the pi from a laptop. Check out this tutorial for doing just that:

Headless Tutorial

Dexter Labs Tutorial

Set up Beacon

You can use hciconfig and hcitool to configure Bluetooth devices. We will create the following to make the pi act as a beacon: - A start script - A stop script - A config file

  • On your pi create the directory /home/pi/beacon
  • In /home/pi/beacon, create a file called beacon.conf and add the following contents:
#BLE beacon configuration
export BLUETOOTH_DEVICE=hci0
export UUID="e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0"
export MAJOR="00 16"
export MINOR="00 08"
export POWER="c5"
  • In the same folder, create a file called beacon_start with the following contents

    #!/bin/sh
    . ./beacon.conf
    echo "Launching virtual beacon..."
    sudo hciconfig $BLUETOOTH_DEVICE up
    sudo hciconfig $BLUETOOTH_DEVICE noleadv
    sudo hciconfig $BLUETOOTH_DEVICE leadv 0
    sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 $UUID $MAJOR $MIN
    echo "Complete"
  • In the same folder, create a file called beacon_stop with the following contents

#!/bin/sh
. ./beacon.conf
echo "Disabling virtual iBeacon..."
sudo hciconfig $BLUETOOTH_DEVICE noleadv
echo "Complete"

Turn the beacon on

  • Run the start script as sudo and you should see the beacon. Using your smart phone, download a free beacon locator. You should see the beacon and an indication of it's proximity.

Run as a Service

You will want the beacon to start when you reboot the pi. Ìn /etc/init.d/ create a file called beacon with the following contents:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:$PATH

DESC="Beacon Application Software"
PIDFILE=/var/run/beacon.pid
SCRIPTNAME=/etc/init.d/beacon

case "$1" in
  start)
    printf "%-50s" "Starting beacon..."
    cd /home/pi/beacon
    sudo ./beacon_start
    ;;
  stop)
    printf "%-50s" "Stopping beacon..."
    cd /home/pi/beacon
    sudo ./beacon_stop
    ;;
  restart)
    $0 stop
   $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

Now make the file executable by running the following command as the sudo user:

sudo chmod +x /etc/init.d/beacon

We want the beacon to run as a service. We can use update-rc.d to register the service and configure it to start when the pi boots, and stop when it shuts down. Run the following to do this.

sudo update-rc.d beacon start 80 2 3 4 5 . stop 30 0 1 6 .

Eddystone-URL

Reconfigure your pi beacon to be an Eddystone URL beacon. Refer to the following tutorial as a guide:

Eddystone URL Beacon