Build an internet connected Bluetooth wearable with Arduino and Cordova, part two

Did you catch part one of our tutorial where we walk through creating your own bluetooth hardware with Tinyduino? In this article we'll be continuing from where we left off and completing our bluetooth fitness band.

When we last left you we connected your phone to a bluetooth module. Next, we'll be adding an accelerometer to the Arduino and creating our own app to graph accelerometer data.

Add the accelerometer to the Tinyduino stack

We'll be using the Tinyduino accelerometer module which fits right on top of the rest of our Arduino project. An accelerometer measures acceleration in three axis (x, y, z) and will help us track our steps for our fitness tracker. The accelerometer also includes a temperature sensor, but we won't be using that right now.

Snap the accelerometer into the top of the TinyDuino stack. It fits right in like the rest of the Tinyduino components.

Test the accelerometer

Next, we'll test the accelerometer. Run this code on your TinyDuino and then look at the Serial Log for the X, Y, and Z acceleration.


#include 
#include "BMA250.h"

BMA250 accel;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  accel.begin(BMA250_range_2g, BMA250_update_time_64ms);//This sets up the BMA250 accelerometer
}

void loop() {
  accel.read();//This function gets new data from the accelerometer
  Serial.print("X = ");
  Serial.print(accel.X);
  Serial.print("  ");
  Serial.print("Y = ");
  Serial.print(accel.Y);
  Serial.print("  ");
  Serial.print("Z = ");
  Serial.print(accel.Z);
  Serial.print("  Temperature(C) = ");
  Serial.println((accel.rawTemp*0.5)+24.0,1);
  delay(250);//We'll make sure we're over the 64ms update time set on the BMA250
}

Shake that Arduino around (but not too much!). You should see the X, Y, and Z acceleration move all over the place. The Y acceleration will always read around 9.8 due to the gravity of earth. Cool huh?

Send the Accelerometer data over Bluetooth

Wohoo! Our hardware build is complete. No soldering required! Now to get this accelerometer data on to our phone.

Remember our Arduino Bluetooth sketch from Part 1? All we're going to do take the example code above and merge it with the Bluetooth sketch. Then when we open the Bluetooth console on our phone, we'll be able to see the accelerometer data.

Upload this codebender sketch onto your TinyDuino and then connect to your Arduino using the NRF UART 2.0 app for Android or iPhone just as you did in Part 1 of this tutorial.

Now whenever you send a message to the Arduino, the Arduino will respond with it's current accelerometer data!

This is what the serial log will look like:


Evt Device Started: Standby
Advertising started
Evt Connected
Evt Pipe Status
Evt Pipe Status
Evt Pipe Status
Evt link connection interval changed
Pipe Number: 11
4 bytes: test
4
test
Pipe Number: 11
4 bytes: test
4
test

Creating a Bluetooth app with Cordova

Let's build our phone app! Don't worry, it's super easy. We'll be using Cordova, a package that allows anybody to create apps for iPhone and Android using HTML and CSS. And best of all, all the code is already ready for you! Just make sure you have NodeJS and Cordova installed on your machine.

Then, clone the pubnub-bluetooth repository. Within this directory, run the following commands:


$ cordova platform add android ios  
$ cordova plugin add com.megster.cordova.ble
$ cordova plugin add https://github.com/apache/cordova-plugin-whitelist.git#r1.0.0
$ cordova run

Great! Your app should now be running on Android or iOS device! Note that you won't be able to connect through Bluetooth using an emulator, you'll need to deploy it to a live mobile device.

Connect to the the Arduino

Launch the app on your device and look for your Arduino Bluetooth module.

Click on your Arduino device, and you'll be presented with a live chart graphing the X, Y, and Z accelerations as well as a window showing all the messages being sent and received through Bluetooth. Swing the accelerometer around to make the chart move up and down!

How it works

First the accelerometer records the acceleration on the three axis. Then that data is sent to the Bluetooth module and then broadcast over the air to your phone. Your phone receives that data and then rebroadcasts it over the internet using the PubNub data stream network. Then, we graph the internet data using a spline chart.

It's important to note, because we broadcast that data over PubNUb, this wearable is internet connected! You can see the live acceleration from any device with internet access.

The Live Chart

The live chart is powered by PubNub's Project EON. EON graphs data published over PubNub in a nice embeddable chart. Because we're working with data streams, all the accelerometer data is being published over the internet. This means you can embed the chart anywhere, including multiple phones or even in a web page.

Try it out, stick this code into an HTML file on your local machine and watch your accelerometer data there too!


The final product

I thought this was a wearable! Right now we've just got a small computer, but it's nothing some creative engineering can't fix. Head over the Sports Authority and grab yourself a sweatband. Then, take a ride over to RadioShack and get a CR1632 coin cell battery for the TinyDuino.

Cut a slit in the arm band, and slide the TinyDuino in there. Boom, now you've got your own internet connected wearable.

Ian Jennings is a developer at PubNub who specializes in building Internet of Things prototypes. Based in Ausin, Texas, Ian enjoys cycling, hiking, drumming, and cooking.