connecting arduino & phones with bluetooth &...

56
Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented by / Don Coleman @doncoleman

Upload: others

Post on 12-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Connecting Arduino & Phoneswith Bluetooth & Cordova

Philly ETE - April 23, 2014Presented by / Don Coleman @doncoleman

Page 2: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 3: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 4: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 5: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Apache CordovaPhoneGap

Page 6: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

npm install -g cordova

Page 7: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

http://docs.cordova.io

Page 8: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Arduino

Page 9: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 10: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 11: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 12: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 13: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 14: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 15: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

#include <SoftwareSerial.h>

#define RxD 6 #define TxD 7

SoftwareSerial bluetooth(RxD,TxD); int counter = 0;

Page 16: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

void setup() { Serial.begin(9600); bluetooth.begin(9600); Serial.println("Bluetooth Counter\n"); }

Page 17: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

void loop() { Serial.println(counter); bluetooth.println(counter); counter++; delay(1000); }

Page 18: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Bluetooth SerialCordova Plugin

https://github.com/don/BluetoothSerial

Page 19: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Installing

$ cordova plugin install \ com.megster.cordova.bluetoothserial

Page 20: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Connecting

var macAddress = "00:00:AA:BB:CC:DD";

bluetoothSerial.connect( macAddress, connected, disconnected);

Page 21: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Sending Data

bluetoothSerial.write("Hello Arduino");

Page 22: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Receiving Data

var onMessage = function(data) { console.log(data); };

bluetoothSerial.subscribe("\n", onMessage, onFailure);

Page 23: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Phone receives data from Arduino

Demo

Page 24: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 25: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Listing Devices

bluetoothSerial.list(success, failure);

Page 26: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

List shows paired devices[{ "class": 276, "id": "10:BF:48:CB:00:00", "address": "10:BF:48:CB:00:00", "name": "Nexus 7"}, { "class": 7936, "id": "00:06:66:4D:00:00", "address": "00:06:66:4D:00:00", "name": "RN42"}]

Page 27: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

What about iOS?

Page 28: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Bluetooth Low Energyaka Bluetooth Smart

Page 29: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

BLE doesn't have SPPbut available hardware has "serial like" services

Page 30: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

UART ServiceRX - write without responseTX - read, notify

Page 31: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Discovering devices

bluetoothSerial.list(success, failure);

No pairing, finds devices

Page 32: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

List discovered devices[{ "id": "CC410A23-2865-F03E-FC6A-4C17E858E11E", "uuid": "CC410A23-2865-F03E-FC6A-4C17E858E11E", "name": "Biscuit", "rssi": -68},{ "id": "ED7127D4-593B-490E-9DA7-959FE78EC603", "uuid": "ED7127D4-593B-490E-9DA7-959FE78EC603", "name": "UART", "rssi": -47}]

Page 33: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Once you're connectedthere's no difference

(except BLE only handles small chunks of data)

Page 34: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

BLE hardwareRedBearLab BLEminiRedBearLab BLE ShieldAdafruit Bluefruit LE

Page 35: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

c red, green, blue

Page 36: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

c0,0,255\n

Page 37: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Phone controls LED strip on Arduino

Demo

Page 38: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Control NeoPixel LED on Arduino from Android

Page 39: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

onColorChange: function (evt) { var c = app.getColor(); rgbText.innerText = c; // 0,0,255 previewColor.style.backgroundColor = "rgb(" + c + ")"; app.sendToArduino(c); },

Page 40: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

sendToArduino: function(c) { bluetoothSerial.write("c" + c + "\n"); },

Page 41: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

void loop() {

if (bluetooth.find("c")) { red = bluetooth.parseInt(); green = bluetooth.parseInt(); blue = bluetooth.parseInt(); showColor(red, green, blue); } }

Page 43: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

BluetoothSerial PluginBluetooth Classic for AndroidBluetooth LE for iOSList or discover devicesConnect to a peripheralWrite to send dataSubscribe to read data

Page 44: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

http://rfduino.com

Page 45: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

RFduino Cordova Pluginhttps://github.com/don/corodva-plugin-rfduino

Page 46: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

rfduino.discover(seconds, success, failure);

{ "name": "RFduino", "uuid": "BD922605-1B07-4D55-8D09-B66653E51BBA", "advertising": "echo", "rssi": -79}

Page 47: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

rfduino.connect(uuid, success, failure);

Page 48: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

rfduino.write("hello", success, failure);

Page 49: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

rfduino.onData(success, failure);

Page 50: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

var onData = function(arrayBuffer) { var a = new Float32Array(arrayBuffer); celsius = a[0]; fahrenheit = celsius * 1.8 + 32; }

Page 51: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 52: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

RFduino PluginBluetooth LE for iOS and AndroidDiscover devicesConnect to a peripheralWrite to send dataSubscribe to read data

Page 53: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented

Future

Page 54: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 55: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented
Page 56: Connecting Arduino & Phones with Bluetooth & Cordovachariotsolutions.com/wp-content/uploads/...Connecting Arduino & Phones with Bluetooth & Cordova Philly ETE - April 23, 2014 Presented