Skip to content

Instantly share code, notes, and snippets.

@philipphager
Created December 23, 2016 11:07
Show Gist options
  • Save philipphager/46bcd752dbd5855e03776da80d3ec9d0 to your computer and use it in GitHub Desktop.
Save philipphager/46bcd752dbd5855e03776da80d3ec9d0 to your computer and use it in GitHub Desktop.
Demo implementation of a plant-a-lot peripheral according to the peripheral sepcs using Arduino CurieBLE
/**
Exemplary implementation of a plant-a-lot peripheral according to the
plant-a-lot peripheral sepcification using Arduino CurieBLE.
Peripheral specification: https://github.com/philipphager/plant-a-lot-peripheral
Official Genuino 101 docs: https://www.arduino.cc/en/Guide/Arduino101
Official CurieBLE docs: https://www.arduino.cc/en/Reference/CurieBLE
*/
#include <CurieBLE.h>
// Arduino is used as BLE peripheral.
BLEPeripheral peripheral;
// Service providing plant identifiers (id).
BLEService infoService("ec0e");
// Characteristic for plant id - UUID, readable by central
BLEUnsignedCharCharacteristic plantIdCharacteristic("ec0f", BLERead);
// Service providing plant sensor values (humidity, lucidity etc.).
BLEService sensorService("ec1e");
// Characteristics for sensor values - UUID, read and subscribable by central
BLEUnsignedCharCharacteristic humidityCharacteristic("ec1f", BLERead | BLENotify);
BLEUnsignedCharCharacteristic lucidityCharacteristic("ec1d", BLERead | BLENotify);
long lastUpdatedMillis = 0;
int updateInterval = 1000; // Read sensor values every 1000ms.
int oldHumidityLevel = 0;
int oldLucidityLevel = 0;
void setup() {
// Init. serial communication for debugging.
Serial.begin(9600);
// Configure device name & advertising settings.
peripheral.setLocalName("Plant A Lot");
peripheral.setAdvertisedServiceUuid(infoService.uuid());
// Add service and its characteristics to peripheral.
// Note that all characterisitcs added UNTIL a new service
// is added are assigned to the previous service.
peripheral.addAttribute(infoService);
peripheral.addAttribute(plantIdCharacteristic);
// Set initial value.
plantIdCharacteristic.setValue(5);
peripheral.addAttribute(sensorService);
peripheral.addAttribute(humidityCharacteristic);
peripheral.addAttribute(lucidityCharacteristic);
humidityCharacteristic.setValue(0);
lucidityCharacteristic.setValue(0);
// Start advertising.
peripheral.begin();
}
void loop() {
BLECentral central = peripheral.central();
// If connected to a BLE central (plant-a-lot server).
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
// As long as a connection to the server (BLE central) exists.
while (central.connected()) {
long currentMillis = millis();
// Read sensor values, if enough time elapsed.
if ((currentMillis - lastUpdatedMillis) >= updateInterval) {
lastUpdatedMillis = currentMillis;
updateHumidityLevel();
updateLucidityLevel();
}
}
}
}
void updateHumidityLevel() {
int humidity = analogRead(A0);
int humidityLevel = map(humidity, 0, 1023, 0, 100);
// Only send new sensor values to peripheral.
if (humidityLevel != oldHumidityLevel) {
humidityCharacteristic.setValue(humidityLevel);
oldHumidityLevel = humidityLevel;
Serial.print("Humidity level: ");
Serial.println(humidityLevel);
}
}
void updateLucidityLevel() {
int lucidity = analogRead(A1);
int lucidityLevel = map(lucidity, 0, 1023, 0, 100);
// Only send new sensor values to peripheral.
if (lucidityLevel != oldLucidityLevel) {
lucidityCharacteristic.setValue(lucidityLevel);
oldLucidityLevel = lucidityLevel;
Serial.print("Lucidity level: ");
Serial.println(lucidityLevel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment