# Sending Live Sensor Data to iOS with BLE

## Overview

https://www.youtube.com/watch?v=526vfNo0cCg

If you have ever wanted to create an iOS application that interacts with an Arduino project, but didn't want to take the time to learn Objective-C or Swift, then this project is for you.&nbsp;We will be using[Apache Cordova](http://cordova.apache.org/) to create an iOS application using HTML and Javascript. If you missed our previous guide&nbsp;about creating a simple iOS weather application, then [you may want to check out that guide](../../../ios-app-development-using-cordova)&nbsp;before continuing with this more advanced Cordova project.

The example application that we will be creating will give visual and audible feedback about the orientation of a BNO055 9-DOF sensor. The sensor data will be sent using a Bluefruit LE UART Friend from a Pro Trinket.

# Sending Live Sensor Data to iOS with BLE

## Installing Dependencies

You will need&nbsp;a Mac running OS X 10.10 to run the latest version of XCode, which is required for uploading to iOS devices.&nbsp;Thanks to changes in XCode 7, you no longer need an Apple Developer account to test applications on iOS devices. You can also use XCode 6 for this tutorial, but you will need an Apple Developer account to upload to your iOS device.&nbsp;XCode 7 is still in beta and not available in the App Store yet, so you will need to download it from Apple's Developer site and install.&nbsp;

[XCode 7 Download](https://developer.apple.com/xcode/downloads/)
You will also need the latest version of [node.js](https://nodejs.org)&nbsp;installed on your computer. Download and install the latest version using the link below.

[Node.js Download](https://nodejs.org)
You will also need the latest version of the Arduino IDE (v1.6.4+) for this project.&nbsp;Download and install the latest version using the link below.

[Arduino IDE Download](https://www.arduino.cc/en/Main/Software)
## Git Repository

Now that you have all of the dependencies installed, you will need to download or clone a copy of the [git repository](https://github.com/adafruit/BNO055-Cordova-Example).

```auto
git clone https://github.com/adafruit/BNO055-Cordova-Example.git
```

[ZIP Archive Download](https://github.com/adafruit/BNO055-Cordova-Example/archive/master.zip)
Next, you will need to navigate to the extracted or cloned repo in the terminal and run the install script using the following command.

```auto
bash install.sh
```

You will see output like the image below as the script installs the necessary Cordova and Arduino library dependencies.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/166/medium800thumb/adafruit_products_hand_tools_automatic_arduino_dependencies.jpg?1448318604)

Now that we have installed all of the software dependencies, we are ready to wire up the hardware.

# Sending Live Sensor Data to iOS with BLE

## Wiring

First, you will need to wire the BNO055 to the Pro Trinket and battery.

- **VIN** to&nbsp; **battery +**
- **GND** to **battery GND**
- **SDA** to&nbsp; **Pro Trinket A4**
- **SCL** to&nbsp; **Pro Trinket A5**

![](https://cdn-learn.adafruit.com/assets/assets/000/026/182/medium800/hand_tools_saw.png?1435620289)

Next, wire the Bluefruit to the Pro Trinket and battery.

- **VIN** to&nbsp; **battery +**
- **GND** to **battery GND**
- **RTS** to **Pro Trinket 10**
- **RXI** to **Pro Trinket 11**
- **TXO** to **Pro Trinket 12**
- **CTS** to **Pro Trinket 13**

# Sending Live Sensor Data to iOS with BLE

## Uploading & Testing

Open the **arduino\_ble.ino** &nbsp;example sketch in the Arduino IDE, and attach the Pro Trinket 3V to your computer using a USB cable. Then, select **Pro Trinket 3V/12MHz (USB)** from the **Tools-\>Board&nbsp;** menu.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/183/medium800/hand_tools_Screen_Shot_2015-06-30_at_11.08.36_AM.png?1435677085)

Then, use the upload button at the top of the IDE to upload the sketch to the Pro Trinket.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/184/medium800/hand_tools_Screen_Shot_2015-06-30_at_11.13.04_AM.png?1435677227)

## Installing the iOS App

Next, navigate to the repo in the terminal and run the following command to tell Cordova to build the iOS app.

```auto
cordova build
```

Once you have built the app, you can open up the XCode project located at the following path inside the repo folder:

```auto
platforms/ios/BNO055 BLE Example.xcodeproj
```

Plug in your iOS device, and click the play button at the top of the XCode project to upload it to your device.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/185/medium800thumb/adafruit_products_hand_tools_upload.jpg?1448318616)

Plug in the battery to the Pro Trinket, BNO055, and Bluefruit. You should then see the blue connection light change on the Bluefruit when the iOS app connects.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/188/medium800thumb/adafruit_products_hand_tools_Untitled-1.jpg?1448318617)

The iOS app will then display the current orientation info for roll and yaw.

![](https://cdn-learn.adafruit.com/assets/assets/000/026/186/medium800/hand_tools_Screen_Shot_2015-06-30_at_11.44.17_AM.png?1435679281)

You can use the _Set Offset_ button to zero the graph at the current orientation. All readings will then be relative to that offset.

# Sending Live Sensor Data to iOS with BLE

## Going Further

The iOS app is split into separate Javascript modules for charting, audio, bluetooth, and sensor readings. Some of these might not be useful for your specific needs, but they are a good starting point for hardware interaction over BLE.

```auto
├── README.md
├── arduino_ble
│   └── arduino_ble.ino
├── config.xml
├── hooks
│   └── README.md
├── install.sh
└── www
    ├── css
    │   └── index.css
    ├── index.html
    └── js
        ├── audio.js
        ├── chart.js
        ├── d3.js
        ├── index.js
        └── reading.js

5 directories, 12 files
```

If you need to modify any of the files in the www folder, you will need to run **cordova build** &nbsp;after every change, and re-upload the app to your device with XCode.

The main BLE connection and data parsing portion&nbsp;of the code can be found in **www/js/index.js.**

```auto
  proto.processData = function(data) {

    past_low = this.low_battery;

    data = data.split(',');

    this.yaw.update(data[0]);
    this.roll.update(parseFloat(data[2]) + 180);
    this.low_battery = parseInt(data[3]) < 3300 ? true : false;

    if(this.low_battery && !past_low)
      navigator.notification.alert('The Bluefruit\'s battery is low', null, 'Warning');

    this.update();

  };
```

You can see that the data parsed out here is sent in the CSV format from the main loop in the **arduino\_ble.ino** sketch.

```auto
  ble.print("AT+BLEUARTTX=");
  ble.print(event.orientation.x, 1);
  ble.print(",");
  ble.print(event.orientation.y, 1);
  ble.print(",");
  ble.print(event.orientation.z, 1);
  ble.print(",");
  ble.print(battery, DEC);
  ble.println("|");
```

You can&nbsp;modify this portion of the sketch to send data from any sensor to iOS.

## Resources

You may find the following resources helpful when developing or modifying a BLE Cordova Application.

- [Cordova Documentation](http://cordova.apache.org/docs/en/5.0.0/)
- [Bluetooth Serial Plugin](https://github.com/don/BluetoothSerial)
- [Phonegap Documentation](http://docs.phonegap.com/en/edge/guide_platforms_index.md.html)&nbsp;- Phonegap is based on Cordova, so a large portion&nbsp;of the documenatation, tutorials and plugins apply to both Cordova and Phonegap.
- [Debugging in Phonegap](https://github.com/phonegap/phonegap/wiki/Debugging-in-PhoneGap)
- [BNO055 Guide](../../../../adafruit-bno055-absolute-orientation-sensor)
- [Bluefruit LE UART Friend Guide](../../../../introducing-the-adafruit-bluefruit-le-uart-friend)


## Featured Products

### Adafruit Bluefruit LE UART Friend - Bluetooth® Low Energy

[Adafruit Bluefruit LE UART Friend - Bluetooth® Low Energy](https://www.adafruit.com/product/2479)
Would you like to add powerful&nbsp;and easy-to-use Bluetooth® Low Energy to your robot, art or other electronics project? Heck yeah! With Bluetooth® Low Energy now included in modern smart phones and tablets, its fun to add wireless connectivity. So what you really need is the new...

In Stock
[Buy Now](https://www.adafruit.com/product/2479)
[Related Guides to the Product](https://learn.adafruit.com/products/2479/guides)
### Adafruit Pro Trinket - 3V 12MHz

[Adafruit Pro Trinket - 3V 12MHz](https://www.adafruit.com/product/2010)
 **Deprecation Warning: The Pro Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Pro Trinket so that people can maintain some older projets, we no longer recommend it.** Please...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2010)
[Related Guides to the Product](https://learn.adafruit.com/products/2010/guides)
### Lithium Ion Polymer Battery - 3.7v 500mAh

[Lithium Ion Polymer Battery - 3.7v 500mAh](https://www.adafruit.com/product/1578)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 500mAh for a total of about 1.9 Wh. If you need a larger (or smaller!) battery, <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1578)
[Related Guides to the Product](https://learn.adafruit.com/products/1578/guides)
### Adafruit 9-DOF Absolute Orientation IMU Fusion Breakout - BNO055

[Adafruit 9-DOF Absolute Orientation IMU Fusion Breakout - BNO055](https://www.adafruit.com/product/2472)
If you've ever ordered and wire up a 9-DOF sensor, chances are you've also realized the challenge of turning the sensor data from an accelerometer, gyroscope and magnetometer into actual "3D space orientation"! Orientation is a hard problem to solve. The sensor fusion...

In Stock
[Buy Now](https://www.adafruit.com/product/2472)
[Related Guides to the Product](https://learn.adafruit.com/products/2472/guides)

## Related Guides

- [Bluefruit LE Connect for iOS and Android](https://learn.adafruit.com/bluefruit-le-connect.md)
- [Bluetooth-Controlled NeoPixel Goggles](https://learn.adafruit.com/bluetooth-neopixel-goggles.md)
- [Build a Bluetooth App using Swift 5](https://learn.adafruit.com/build-a-bluetooth-app-using-swift-5.md)
- [Introducing the Adafruit Bluefruit LE UART Friend](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Comparing Gyroscope Datasheets](https://learn.adafruit.com/comparing-gyroscope-datasheets.md)
- [Adafruit BNO055 Absolute Orientation Sensor](https://learn.adafruit.com/adafruit-bno055-absolute-orientation-sensor.md)
- [BNO055 Absolute Orientation Sensor with Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/bno055-absolute-orientation-sensor-with-raspberry-pi-and-beaglebone-black.md)
- [Bluefruit LE Python Library](https://learn.adafruit.com/bluefruit-le-python-library.md)
- [Adafruit Ultimate GPS featherwing](https://learn.adafruit.com/adafruit-ultimate-gps-featherwing.md)
- [Adafruit Speaker Bonnet for Raspberry Pi](https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi.md)
- [Adafruit PCT2075 Temperature Sensor](https://learn.adafruit.com/adafruit-pct2075-temperature-sensor.md)
- [Adafruit IoT Button with NeoPixel BFF](https://learn.adafruit.com/adafruit-iot-button-with-neopixel-bff.md)
- [NeoKey Socket Breakout with NeoPixel for MX and CHOC Key Switches](https://learn.adafruit.com/neokey-breakout.md)
- [Introducing ItsyBitsy M0 Express](https://learn.adafruit.com/introducing-itsy-bitsy-m0.md)
