Skip to content

Instantly share code, notes, and snippets.

@hidenba
Created July 13, 2016 22:19
Show Gist options
  • Save hidenba/e3d3fe93a347881188e7e3fd78a0e8f5 to your computer and use it in GitHub Desktop.
Save hidenba/e3d3fe93a347881188e7e3fd78a0e8f5 to your computer and use it in GitHub Desktop.
#include <CurieBLE.h>
const int ledPin = 13; // set ledPin to use on-board LED
BLEPeripheral blePeripheral; // create peripheral instance
BLEService alcoholService("29B10000-E8F2-537E-4F6C-D104768A1214"); // create service
BLEShortCharacteristic alcoholChar("29B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);
const int termLedPin = 13;
const int alcoholSensorPin = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("start setup");
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
// set the local name peripheral advertises
blePeripheral.setLocalName("LED_ALC");
blePeripheral.setAdvertisedServiceUuid(alcoholService.uuid());
blePeripheral.addAttribute(alcoholService);
blePeripheral.addAttribute(alcoholChar);
// assign event handlers for connected, disconnected to peripheral
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
alcoholChar.setEventHandler(BLEWritten, alcoholCharacteristicWritten);
alcoholChar.setValue(0);
// advertise the service
blePeripheral.begin();
Serial.println(("Bluetooth device active, waiting for connections..."));
}
void loop() {
// poll peripheral
blePeripheral.poll();
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void alcoholCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
digitalWrite(termLedPin, HIGH);
int sumValue = 0;
for(int i = 0; i < 150000; i++) {
sumValue = sumValue + analogRead(alcoholSensorPin);
}
digitalWrite(termLedPin, LOW);
int avgValue = 1024 - (sumValue / 150000);
alcoholChar.setValue(avgValue);
Serial.println(avgValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment