Skip to content

Instantly share code, notes, and snippets.

@danasf
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save danasf/9439614 to your computer and use it in GitHub Desktop.

Select an option

Save danasf/9439614 to your computer and use it in GitHub Desktop.
BLE!!!!
// THIS SKETCH WORKS!
#include <OctoWS2811.h>
#define BTSERIAL Serial2
#define DEBUG true
#define NUM_BYTES (17*3)
int i=0;
byte inBytes[NUM_BYTES];
boolean stringComplete = false;
uint32_t hexcolor;
const int ledsPerStrip = 15;
DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 strip(ledsPerStrip, displayMemory, drawingMemory, config);
void setup() {
// initialize serial:
BTSERIAL.begin(9600);
if(DEBUG) { Serial.begin(9600); Serial.println("Here we go, v1.0"); }
// Initialize all pixels to 'off'
strip.begin();
strip.show();
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
processData();
strip.show();
}
listenForBTData();
}
void listenForBTData() {
while (BTSERIAL.available()) {
// get the new byte:
byte inByte = BTSERIAL.read();
if(DEBUG) { Serial.print(inByte); }
// add it to the inputString:
inBytes[i]= inByte;
i++;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (i == NUM_BYTES) {
stringComplete = true;
}
}
}
void processData() {
int j=0;
int light=0;
Serial.print("Data Received");
for(int q=0; q < i; q++) {
if(j == 3) {
// red, green, blue
hexcolor= ((inBytes[j-1] << 16) + (inBytes[j] << 8) + inBytes[j-2]);
strip.setPixel(light, hexcolor);
if(DEBUG) {
Serial.print(q);
Serial.print(" bytes received ");
Serial.println("Color Code:");
Serial.print(hexcolor);
Serial.println("");
}
j=0;
light++;
} else {
j++;
}
}
// clear the string:
i=0;
stringComplete = false;
light=0;
memset(&inBytes[0], 0, sizeof(inBytes));
}
/*
Software Serial Bluetooth
Connect Bluetooth RX,TX to Arduino Pins 10,11
Requires NeoPixel Library
https://github.com/adafruit/Adafruit_NeoPixel
*/
#include <Adafruit_NeoPixel.h>
#define BTSERIAL Serial2
// RX, TX
int red,green,blue;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(15, 2, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// initialize serial:
BTSERIAL.begin(9600);
// set strip off
colorSet(strip.Color(0, 0,0), 0);
colorSet(strip.Color(255, 0,0), 0);
}
void loop() {
// if there's any serial available, read it:
while (BTSERIAL.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int red = BTSERIAL.parseInt();
// do it again:
int green = BTSERIAL.parseInt();
// do it again:
int blue = BTSERIAL.parseInt();
// look for the newline. That's the end of your statement
if (BTSERIAL.read() == '\n') {
// constrain the values to 0 - 255
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
// fill strip
colorSet(strip.Color(red, green, blue), 0);
// send some data back
BTSERIAL.println("received:"+String(red)+","+String(green)+","+String(blue));
}
}
}
// Fill strip with a color
void colorSet(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(wait);
}
var noble = require('noble');
var peripheralUuid = "07048004fe374b5ba3bb588e8254a9be";
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
console.log("Scanning...");
} else {
console.log("Bluetooth isn't on!");
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
if (peripheral.uuid === peripheralUuid) {
console.log("found light bulb!");
noble.stopScanning();
console.log('matching services and characteristics...');
peripheral.connect(function(error) {
// main service for serial
var serviceUUIDs = ["ffe0"];
var characteristicUUIDs = ["ffe1"];
peripheral.discoverSomeServicesAndCharacteristics(serviceUUIDs, characteristicUUIDs,function(err,services,chars) {
var buf = new Buffer("0,255,0\n", "ascii");
console.log(buf);
chars[0].write(buf, false);
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment