Book Image

Arduino Android Blueprints

Book Image

Arduino Android Blueprints

Overview of this book

Table of Contents (17 chapters)
Arduino Android Blueprints
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Writing the Arduino sketch


We will now write the sketch to control the relay from an Android device. Note that this is the same sketch as in the previous chapter, so you can skip it if you already did this part for the last chapter. The following is the complete sketch for this part:

// Control Arduino board from BLE

// Libraries
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <aREST.h>

// Pins
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2   // Should be pin 2 or 3
#define ADAFRUITBLE_RST 9

// Relay pin
const int relay_pin = 7;

// Create aREST instance
aREST rest = aREST();

// BLE instance
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

void setup(void)
{  
  // Start Serial
  Serial.begin(115200);

  // Start BLE
  BTLEserial.begin();
 
  // Give name and ID to device
  rest.set_id("001");
  rest.set_name("relay_control");
  
   // Init relay pin
  pinMode(relay_pin,OUTPUT);
}

void loop() {  
  
  //...