Created
March 25, 2015 23:14
-
-
Save sandeepmistry/d2e8687a6993e9a110b7 to your computer and use it in GitHub Desktop.
BLEPeripheral + NeoPixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import libraries (BLEPeripheral depends on SPI) | |
#include <SPI.h> | |
#include <BLEPeripheral.h> | |
#include <Adafruit_NeoPixel.h> | |
// define pins (varies per shield/board) | |
#define BLE_REQ 10 | |
#define BLE_RDY 2 | |
#define BLE_RST 9 | |
// Neopixel pin | |
#define NEOPIXEL_PIN 6 | |
#define NEOPIXEL_NUMPIXELS 16 | |
// create peripheral instance, see pinouts above | |
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST); | |
// create service | |
BLEService ledService = BLEService("19b10000e8f2537e4f6cd104768a1214"); | |
// create switch characteristic | |
BLECharacteristic colorCharacteristic = BLECharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite, 3); | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); | |
void setup() { | |
Serial.begin(9600); | |
#if defined (__AVR_ATmega32U4__) | |
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board | |
#endif | |
pixels.begin(); | |
// set advertised local name and service UUID | |
blePeripheral.setLocalName("NeoPixel"); | |
blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); | |
// add service and characteristic | |
blePeripheral.addAttribute(ledService); | |
blePeripheral.addAttribute(colorCharacteristic); | |
// begin initialization | |
blePeripheral.begin(); | |
Serial.println(F("BLE NeoPixel Peripheral")); | |
} | |
void loop() { | |
BLECentral central = blePeripheral.central(); | |
if (central) { | |
// central connected to peripheral | |
Serial.print(F("Connected to central: ")); | |
Serial.println(central.address()); | |
while (central.connected()) { | |
// central still connected to peripheral | |
if (colorCharacteristic.written()) { | |
for(int i = 0; i < NEOPIXEL_NUMPIXELS; i++) { | |
char r = colorCharacteristic.value()[0]; | |
char g = colorCharacteristic.value()[1]; | |
char b = colorCharacteristic.value()[2]; | |
pixels.setPixelColor(i, pixels.Color(r, g, b)); | |
pixels.show(); | |
} | |
} | |
} | |
// central disconnected | |
Serial.print(F("Disconnected from central: ")); | |
Serial.println(central.address()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment