Forked from dkarchmer/sample-arduino-101-notify-random.ino
Last active
July 13, 2016 00:40
-
-
Save don/32c49cc60245a962aa9c67597bd7ab06 to your computer and use it in GitHub Desktop.
Sample Arduino 101 code advertising BLE characteristic with 'BLERead | BLENotify'
This file contains 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
//Sample using LiquidCrystal library | |
//#include <LiquidCrystal.h> | |
#include <CurieBLE.h> | |
// select the pins used on the LCD panel | |
//LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | |
// define some values used by the panel and buttons | |
int lcd_key = 0; | |
int adc_key_in = 0; | |
/* */ | |
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) | |
BLEService ioTileService("19b10000e8f2537e4f6cd104768a1214"); // BLE IOTile Service | |
// BLE Battery Level Characteristic" | |
BLEIntCharacteristic ioTileChar("19b10001e8f2537e4f6cd104768a1214", BLERead | BLENotify); | |
long previousMillis = 0; // last time the battery level was checked, in ms | |
void setup() | |
{ | |
// lcd.begin(16, 2); // start the library | |
Serial.begin(9600); // initialize serial communication | |
randomSeed(analogRead(0)); | |
/* Set a local name for the BLE device | |
This name will appear in advertising packets | |
and can be used by remote devices to identify this BLE device | |
The name can be changed but maybe be truncated based on space left in advertisement packet */ | |
blePeripheral.setLocalName("IOTile"); | |
blePeripheral.setAdvertisedServiceUuid(ioTileService.uuid()); // add the service UUID | |
blePeripheral.addAttribute(ioTileService); // Add the BLE Battery service | |
blePeripheral.addAttribute(ioTileChar); // add the battery level characteristic | |
ioTileChar.setValue(0); // initial value for this characteristic | |
// lcd.setCursor(0,0); | |
// lcd.print("Advertising..."); | |
Serial.println("Bluetooth device active, waiting for connections..."); | |
Serial.println(ioTileService.uuid()); | |
Serial.println(String(BLENotify)); | |
/* Now activate the BLE device. It will start continuously transmitting BLE | |
advertising packets and will be visible to remote BLE central devices | |
until it receives a new connection */ | |
blePeripheral.begin(); | |
} | |
void loop() | |
{ | |
// lcd.setCursor(0,1); // move to the begining of the second line | |
// listen for BLE peripherals to connect: | |
BLECentral central = blePeripheral.central(); | |
// if a central is connected to peripheral: | |
if (central) { | |
// lcd.clear(); | |
// lcd.setCursor(0,0); | |
// lcd.print("Connected..."); | |
Serial.println(String(BLENotify)); | |
// print the central's MAC address: | |
Serial.print("Connected to central: "); | |
Serial.println(central.address()); | |
// check every 2000ms | |
// as long as the central is still connected: | |
while (central.connected()) { | |
long currentMillis = millis(); | |
// if 2000ms have passed, check the battery level: | |
if (currentMillis - previousMillis >= 2000) { | |
previousMillis = currentMillis; | |
Serial.println("updating characteristic"); | |
updateCharacteristic(); | |
} | |
} | |
Serial.print("Disconnected from central: "); | |
Serial.println(central.address()); | |
// lcd.clear(); | |
// lcd.setCursor(0,0); | |
// lcd.print("Advertising..."); | |
} | |
} | |
void updateCharacteristic() { | |
int randNumber = (int)random(300); | |
Serial.print("Measurement: "); // print it | |
Serial.println(randNumber); | |
// lcd.setCursor(0,1); | |
// lcd.print(" "); | |
// lcd.setCursor(0,1); | |
// lcd.print(String(randNumber, DEC)); | |
ioTileChar.setValue(randNumber); // and update the battery level characteristic | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment