From no name for this wiki
Creating a switch with pebble, android, arduino and BLE (Bloe Tooth Low Energy)
Arduino Code
/*********************************************************************
description: Switch
hardware: nRF8001 Bluetooth Low Energy Breakout
Setup: PIN 13: Connected to SCK of BLE (Clock)
PIN 12: Connected to MISO of BLE (Master Input, Slave Output, sending data to arduino
from peripherals)
PIN 11: Connected to MOSI of BLE (Master Output, Slave Inut, sending data form peripherals to arduino).
PIN 10: Connected to REQ of BLE (this is basically what the nRF8001 considers the 'SPI Chip Select' pin, its an input)
PIN 09: Connected to RST of BLE ( (reset) - this is the reset pin input)
PIN 02: Connected to RDY of BLE ((ready) - this is the data-ready pin,
an interrupt output from the breakout to the microcontroller
letting it know that data is ready to read)
GND: Connected to GND of BLE
5V: Connected to VIN of BLE
*********************************************************************/
// This version uses the internal data queing so you can treat it like Serial (kinda)!
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
int SWITCH_PIN = 7;
void setup(void)
{
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
pinMode(SWITCH_PIN, OUTPUT);
digitalWrite(SWITCH_PIN, HIGH);
BTLEserial.begin();
}
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void sendDataToBleCient()
{
if (Serial.available()) {
// Read a line from Serial
Serial.setTimeout(100); // 100 millisecond timeout
String s = Serial.readString();
// We need to convert the line to bytes, no more than 20 at this time
uint8_t sendbuffer[20];
s.getBytes(sendbuffer, 20);
char sendbuffersize = min(20, s.length());
Serial.print(F("\n* Sending -> \""));
Serial.print((char *)sendbuffer);
Serial.println("\"");
// write the data
BTLEserial.write(sendbuffer, sendbuffersize);
}
}
void receieveDataFromBleClient()
{
boolean doSwitch = false;
// Lets see if there's any data for us!
if (BTLEserial.available()) {
Serial.print("* ");
Serial.print(BTLEserial.available());
Serial.println(F(" bytes available from BTLE"));
doSwitch = true;
}
if(doSwitch == true)
{
int currentState = digitalRead(SWITCH_PIN);
int newState = LOW;
if(currentState == LOW)
{
newState = HIGH;
}
digitalWrite(SWITCH_PIN, newState);
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
Serial.print(c);
}
}
aci_evt_opcode_t updateBLEDeviecStatus()
{
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
return status;
}
void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
aci_evt_opcode_t status = updateBLEDeviecStatus();
if (status == ACI_EVT_CONNECTED) {
//reveieve Data
receieveDataFromBleClient();
// Next up, see if we have any data to get from the Serial console
sendDataToBleCient();
}
}