Always On, Always Connected Week 9

Building Mobile Accessories with Arduino and BLE

A variety of BLE modules that either work with Arduino (such as a shield) or are Arduino's themselves have begun to come on the market. This makes it easy for us to start prototyping phone connected hardware or accessories.

RedBearLab's BLE Nano

The BLE Nano is an Arduino compatible board that can run Arduino sketches and has BLE capabilities. We have them available for checkout in the ER. Get the ones that include the USB programmer board (itself an Arduino).

It requires that you have the Arduino Library for nRF51822 setup. The process for doing this is detailed in their Getting Started with the nRF51822 document. Essentially, you need to download the Git repo for the library and move the folder called "RBL_nRF51822" from within Arduino XXX/hardware/arduino to your Arduino application itself. To do this, you'll cntrl-click on the Arduino application and select "Show Package Contents" and then navigate to Resources/Java/hardware/arduino and put the folder there.

Next, you'll want to figure out the process of using the "bootloader" to automatically copy the compiled sketch to the BLE Nano.

Here is the command, assuming you are already in the same directory as the bootloader.hex file.

sudo mount -u -w -o sync /Volumes/MBED ; cp -X bootloader.hex /Volumes/MBED/
		
You'll need to run this before each time you attempt to upload a new sketch via the Arduino software.

Arduino BLEPeripheral Library

The Arduino BLEPeripheral Library is very full featured and supports the BLE Nano so installing that is the next step.
cd ~/Documents/Arduino/libraries/
git clone https://github.com/sandeepmistry/arduino-BLEPeripheral BLEPeripheral		
		

BLE Nano and BLEPeripheral Library

Next, you'll run this example sketch to test. Use your phone with a BLE Scanner and the Arduino Serial monitor to see if it works.

If that works, we can move on to a more full featured example. Maria Paula and Tom Igoe put together a few examples online.

The LED Switch is a great starting point for a variety of apps. Just change the LED pin to 13 and give it a shot.

Here is an example page for working with it on the PhoneGap side. Don't forget to install Don Coleman's BLE Central Plugin

<!DOCTYPE html>
<html>
    <head>
 <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript">

			var led = {
				service: "19b10000-e8f2-537e-4f6c-d104768a1214",
				data: "19b10001-e8f2-537e-4f6c-d104768a1214"
			};
			
			var peripheralId = null;

			function onPageLoad() {
				document.addEventListener("deviceready", onDeviceReady, false);
			}

			function onDeviceReady() {
				ble.scan([], 10, scanSuccess, scanFailure);
			}

			function scanSuccess(peripheral) {						
				if (peripheral.name == "LED") {
					ble.stopScan(stopScanSuccess, stopScanFailure);
					console.log(peripheral);
					console.log("Connecting");
					ble.connect(peripheral.id, connectSuccess, connectFailure);
				}
			}

			function scanFailure(err) {
				console.log(err);
			}
			
			function stopScanSuccess() {
				console.log("Stop Scan Success");
			}
			
			function stopScanFailure() {
				console.log("Stop Scan Failure");
			}
			
			function connectSuccess(peripheral) {
				console.log("Connected");
				console.log(peripheral);
				
				peripheralId = peripheral.id;
				//console.log(foundPeripheral);

				// Do a read
				ble.read(peripheral.id, led.service, led.data, readSuccess, readFailure);
			}
			
			function connectFailure() {
				console.log("Connection Failed");
			}
			
			
			function readSuccess(data) {
				console.log(data)
				document.getElementById('data').innerHTML = message;
			}
		
			
			function readFailure(err) {
				console.log(err);
			}

			function writeData(data) {
				var configData = new Uint8Array(1);
				configData[0] = data;

				ble.write(peripheralId, led.service, led.data, configData.buffer, 
				function() { console.log("Wrote: " + data); },function() { console.log("Error");});
			}
                
        </script>
    </head>
    <body onload="onPageLoad()">
	<div id="data"></div>
		<button onclick="writeData(1);">Write 1</button>
		<button onclick="writeData(0);">Write 0</button>
    </body>
</html>