Connecting to RPi3 using BLE

In this lab you will learn how to develop a mobile application that communicates via Bluetooth Low Energy with your Raspberry Pi 3. You will use Javascript and HTML to accomplish this.

You need the following tools:

  • A Raspberry Pi 3 development board - with super user access.
  • iOS / Android phone with BLE support
  • Evothings Studio software

Configure Bluetooth on the Pi

The Raspberry Pi 3 will be running a Node.js application that makes use of the Bleno library. This library requires that you have installed the Bluetooth stack, Bluez. Bluez is pre-installed in the latest releases of Raspbian. To check that Bluez is installed and which version. Do this by running the following command:

hcitool | grep ver

This command should return the version number, typically 5.* . If this is not successfull, install blueZ as follows:

sudo apt-get install pi-bluetooth

For Bleno to work, you need to stop the bluetooth daemon. The following commands will stop and check the status:

sudo systemctl stop bluetooth
sudo systemctl status bluetooth

Verify that the status is dead. Now make this permenant by running the following command:

sudo systemctl disable bluetooth

You also need to power up the Bluetooth adapter after stopping the bluetooth daemon.

sudo hciconfig hci0 up

Get Node

Install Node on the Pi 3. If it's already there then you may be able to skip this step. The lab is confirmed with this version...

Get Node binaries

wget https://nodejs.org/dist/v5.9.1/node-v5.9.1-linux-armv7l.tar.gz
tar -xvf node-v5.9.1-linux-armv7l.tar.gz

Install to /usr/local

cd node-v5.9.1-linux-armv7l
sudo cp -R * /usr/local/

To check Node.js is properly installed and you have the right version, run the command node -v and it should return v5.9.1

Finally, update the packages and install libudev (enables hardware detection and enablement).

sudo apt-get update
sudo apt-get install git libudev-dev

The Node App

Clone the following repo to your home directory:

cd /home/pi
git clone https://github.com/evothings/evothings-examples.git

Go to the node application in the repo and install the required dependencies

cd evothings-examples/evothings-examples/examples/rpi3-system-information/rpi3-application
npm install

You may see the odd warning here but, hopefully, this wont affect the node app execution. In this folder , open index.js and update to the following:

var bleno = require('bleno');
var name = 'YOUR NAME RPi';
var SystemInformationService = require('./systeminformationservice');

var systemInformationService = new SystemInformationService();

bleno.on('stateChange', function(state) {
  console.log('on -> stateChange: ' + state);

  if (state === 'poweredOn') {

    bleno.startAdvertising(name, [systemInformationService.uuid]);
  }
  else {

    bleno.stopAdvertising();
  }
});

Now run the app with the following command:

sudo node index.js

You shouLD see the following:

on -> stateChange: poweredOn
on -> advertisingStart: success

Look at the systeminformationservice.js. It defines the Bluetooth service and associated characteristics:

var bleno = require('bleno');
var util = require('util');

var LoadAverageCharacteristic = require('./characteristics/loadaverage');
var UptimeCharacteristic = require('./characteristics/uptime');
var MemoryCharacteristic = require('./characteristics/memory');

function SystemInformationService() {
  bleno.PrimaryService.call(this, {
    uuid: 'ff51b30e-d7e2-4d93-8842-a7c4a57dfb07',
    characteristics: [
      new LoadAverageCharacteristic(),
      new UptimeCharacteristic(),
      new MemoryCharacteristic()
    ]
  });
};

Mobile Applications using EvoThings

Evothings is a development tool that enables users to build cross platform IoT-applications using nothing but web technologies such as HTML and JavaScript. You will need to install the EvoThings workbench on your laptop/development machine and the EvoThings Viewer on your mobile phone. There is more detailed instructions available on Evothings homepage.

Open the Evothings Workbench on your computer and press the Connect tab. In this tab you will connect your smartphone with your computer in order to enable live updates of your mobile application project. Press the GET KEY button to receive a key.

Open the Evothings Viewer on your smartphone and enter the key in the Connect Keyfield and press the Connect button. Now you have connected your workbench to your mobile phone and you are ready to start to develop your mobile application.

Updated App

The example apps can be used as templates to build your own app. Clone the following github repo on both the pi and your desktop. As before, start the node.js service on the pi (found in evothing-ble-demo/rpi3-application). On the deskop, open EvoThings Studio. Browse to the generated /evothing-ble-demo/ directory and find the evothings.json file. Press the My Apps button in the Evothings Studio and drag the file to the window. Now you should have an application named Raspberry Pi 3 - System Information in your window. If you press the Run button the application should load on your smartphone as shown below...