A quick walk through the Evothings Arduino BLE example

Aaron ArdiriBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

redbearlabs-blend-arduinoble

At Evothings we take pleasure in showing how simple it is to interact with IoT devices from an iOS or Android mobile device.

At the numerous conferences and events we have attended over the past few months – one of our examples tends to be quite popular, as it grabs the attention of the audience we are demonstrating for, is our Arduino BLE example.

Mikael Kindborg, one of our team members, recently wrote up an in-depth and detailed getting started with BLE guide which you should definitely read over to understand the basics of Bluetooth Low Energy.

Not only is the example interactive on the both the Arduino and mobile device simultaneously, but it is also demonstrates two-way communication between the devices using Bluetooth Low Energy communications protocol.

From a hardware perspective there is a small “shopping list” that is required we used the following components:

Any Arduino compatible micro-controller can be used, if you already have one – you could also use the BLE Shield or any shield that uses the Nordic Semiconductor nRF8001 chipset. Our example was built based on this chipset – however it could be quite easily modified to support additional chipsets and their associated SDKs.

arduinoble-components

Part of the fun is actually wiring up the circuitry, we often run workshops where attendees can do this on their own or with our assistance. Inside the example documentation is a fritzing diagram that outlines how to connect the various components to each other and the Arduino.

To make things easier to understand it is recommended to use a red wire for 5V and black wire for GND (ground) as some components actually do have to be orientated the right way to function – such as the LED. A 220 Ohm resistor is used to reduce the voltage from 5V to something the LED can handle – depending on the voltage rating of the LED it may vary; but in this example using a 220 Ohm gives a nice bright glow.

Arduino Sketch

The Arduino Sketch is located in the examples/arduino-ble/arduinoble directory.

On startup the setup() function is called and a number of underlying system and BLE functions are called to configure digital PIN 2 in output mode, analog PIN A0 in input mode and start the underlying BLE communications system with the identifying name of “arduinoble” so it can be identified from the mobile application.

In the loop() function, the following code is available:

void loop() 
{
	// If there's any input...
	while(ble_available()) {
		// Read input.
		int c = ble_read();
		if(c != 0) {
			// Non-zero input means "turn on LED".
			Serial.write(c);
			digitalWrite(LED_PIN, HIGH);
		} else {
			// Input value zero means "turn off LED".
			Serial.write('0');
			digitalWrite(LED_PIN, LOW);
		}
	}

	// Read the analog input pin and send the data over BLE.
	short i = analogRead(INPUT_PIN);
	ble_write_bytes((byte*)&i, 2);

	// Process BLE events.
	ble_do_events();
}

In this code a single byte that has been sent from the mobile device is read from the BLE communications channel and depending on its value – the LED is either turned on or off using the digitalWrite function. Once complete a call to analogRead is performed to read the value from the potentiometer and the resulting value which is between 0 and 1023 (by default, the analog input has 10-bit precision) is written to the BLE communications channel which will be received on the mobile device.

Simply ensure you have the appropriate libraries for the Nordic nRF8001 chipset (you can also use the RedBearLabs libraries available in their getting-started guide) and upload the sketch to your Arduino device using the Arduino IDE.

Evothings App

There are two main components in the Evothings App – the first is the scanning and connection from the mobile device to the BLE chipset on the Arduino, if you look at the following code it is quite simple to see that setting the BLE name was important to identify which device we should connect to.

    evothings.ble.startScan(
      function(deviceInfo)
      {
        if (app.knownDevices[deviceInfo.address])
        {
          return;
        }
        console.log('found device: ' + deviceInfo.name);
        app.knownDevices[deviceInfo.address] = deviceInfo;
        if (deviceInfo.name == 'arduinoble' && !app.connectee)
        {
          console.log('Found arduinoble');
          connectee = deviceInfo;
          app.connect(deviceInfo.address);
        }
      },

In the user interface there are two HTML buttons that have their onClick states defined to call the app.on() and app.off() functions – which writes a single byte to the BLE characteristic providing serial communication; namely 1 for on and 0 for off.

  on: function()
  {
    app.write(
      'writeCharacteristic',
      app.deviceHandle,
      app.characteristicWrite,
      new Uint8Array([1])); // 1 = on
  },

  off: function()
  {
    app.write(
      'writeCharacteristic',
      app.deviceHandle,
      app.characteristicWrite,
      new Uint8Array([0])); // 0 = off
  },

For receiving information from the Arduino, the application configures a notification handler to process information as it is received from the BLE communications channel by converting two bytes into a 16-bit integer to obtain the values between 0 and 1023 that was originally sent from the Arduino.

    // Start reading notifications.
    evothings.ble.enableNotification(
      deviceHandle,
      app.characteristicRead,
      function(data)
      {
        app.drawLines([new DataView(data).getUint16(0, true)]);
      },
      function(errorCode)
      {
        console.log('enableNotification error: ' + errorCode);
      });

With a bit of JavaScript and Canvas 2D code a buffer of data points (as they are collected) are plotted to the mobile display in real time. As the user rotates the potentiometer the graph updates to reflect the current analog value obtained from the Arduino.

Conclusion

Once you manage to connect everything together and start the application on the mobile device – you should have something like the following you can interact with:

redbearlabs-blend-arduinoble

Now that we have outlined how the application works under the hood – why don’t you grab a copy of Evothings Studio and try it out for yourself! If you create some cool and interesting projects or have questions on how to get started, you can post information and questions to our forum.