Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/b6b526c7579320c726d3 to your computer and use it in GitHub Desktop.
Save gbuesing/b6b526c7579320c726d3 to your computer and use it in GitHub Desktop.
Arduino BLE temperature sensor w/ nRF8001
// BLEPeripheral: https://github.com/sandeepmistry/arduino-BLEPeripheral
// TimerOne: https://code.google.com/p/arduino-timerone/
#include <TimerOne.h>
#include <SPI.h>
#include <BLEPeripheral.h>
// Pins used for Adafruit nrf8001 breakout
// See https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/hooking-everything-up
#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService tempService = BLEService("CCC0");
BLEIntCharacteristic tempCharacteristic = BLEIntCharacteristic("CCC1", BLERead | BLENotify);
BLEDescriptor tempDescriptor = BLEDescriptor("2901", "Celsius");
volatile bool readTemperature = false;
void setup() {
Serial.begin(115200);
blePeripheral.setLocalName("Temperature");
blePeripheral.setAdvertisedServiceUuid(tempService.uuid());
blePeripheral.addAttribute(tempService);
blePeripheral.addAttribute(tempCharacteristic);
blePeripheral.addAttribute(tempDescriptor);
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
blePeripheral.begin();
Timer1.initialize(1000000); // in milliseconds, i.e. 1 second
Timer1.attachInterrupt(timerHandler);
}
void loop() {
blePeripheral.poll();
if (readTemperature) {
setTempCharacteristicValue();
readTemperature = false;
}
}
void timerHandler() {
readTemperature = true;
}
void setTempCharacteristicValue() {
int temp = readTempC();
tempCharacteristic.setValue(temp);
Serial.println(temp);
}
int readTempC() {
// Stubbing out for demo with random value generator
// Replace with actual sensor reading code
return random(100);
}
void blePeripheralConnectHandler(BLECentral& central) {
Serial.print(F("Connected event, central: "));
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
Serial.print(F("Disconnected event, central: "));
Serial.println(central.address());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment