Skip to content

Instantly share code, notes, and snippets.

@labajo
Last active April 14, 2018 10:55
Show Gist options
  • Save labajo/f3a7d279ade485b4046c9de5c7c820f8 to your computer and use it in GitHub Desktop.
Save labajo/f3a7d279ade485b4046c9de5c7c820f8 to your computer and use it in GitHub Desktop.
BLE Nrf51822 BMP280
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// TimerOne library: https://code.google.com/p/arduino-timerone/
// DHT library: https://github.com/adafruit/DHT-sensor-library
#include <SPI.h>
#include <BLEPeripheral.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
// define pins (varies per shield/board)
#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9
#define DHTTYPE DHT11
#define DHTPIN 3
Adafruit_BMP280 bme;
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService tempService = BLEService("CCC0");
BLEFloatCharacteristic tempCharacteristic = BLEFloatCharacteristic("CCC1", BLERead | BLENotify);
BLEDescriptor tempDescriptor = BLEDescriptor("2901", "Temp Celsius");
BLEService pressureService = BLEService("DDD0");
BLEFloatCharacteristic pressureCharacteristic = BLEFloatCharacteristic("DDD1", BLERead | BLENotify);
BLEDescriptor pressureDescriptor = BLEDescriptor("2901", "Pressure Milibars");
BLEService altitudeService = BLEService("EEE0");
BLEFloatCharacteristic altitudeCharacteristic = BLEFloatCharacteristic("EEE1", BLERead | BLENotify);
BLEDescriptor altitudeDescriptor = BLEDescriptor("2901", "Altitude");
float lastTempReading;
float lastPressureReading;
float lastAltitudeReading;
long previousMillis = 0;
void setup() {
Serial.begin(115200);
#if defined (__AVR_ATmega32U4__)
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
#endif
blePeripheral.setLocalName("BLE_Sensor");
blePeripheral.setAdvertisedServiceUuid(tempService.uuid());
blePeripheral.addAttribute(tempService);
blePeripheral.addAttribute(tempCharacteristic);
blePeripheral.addAttribute(tempDescriptor);
blePeripheral.setAdvertisedServiceUuid(pressureService.uuid());
blePeripheral.addAttribute(pressureService);
blePeripheral.addAttribute(pressureCharacteristic);
blePeripheral.addAttribute(pressureDescriptor);
blePeripheral.setAdvertisedServiceUuid(altitudeService.uuid());
blePeripheral.addAttribute(altitudeService);
blePeripheral.addAttribute(altitudeCharacteristic);
blePeripheral.addAttribute(altitudeDescriptor);
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
blePeripheral.begin();
Serial.println(F("BLE Temperature Sensor Peripheral"));
if (!bme.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
}
void loop() {
blePeripheral.poll();
long currentMillis = millis();
if (currentMillis - previousMillis >= 5000) {
previousMillis = currentMillis;
setTempCharacteristicValue();
setPressureCharacteristicValue();
setAltitudeCharacteristicValue();
}
}
void setTempCharacteristicValue() {
float reading = bme.readTemperature();
// float reading = random(100);
if (!isnan(reading) && significantChange(lastTempReading, reading, 0.05)) {
tempCharacteristic.setValue(reading);
Serial.print(F("Temperature: ")); Serial.print(reading); Serial.println(F("C"));
lastTempReading = reading;
}
}
void setPressureCharacteristicValue() {
float reading = bme.readPressure();
if (!isnan(reading) && significantChange(lastPressureReading, reading, 0.2)) {
pressureCharacteristic.setValue(reading);
Serial.print(F("Pressure: ")); Serial.print(reading); Serial.println(F("mB"));
lastPressureReading = reading;
}
}
void setAltitudeCharacteristicValue() {
float reading = bme.readAltitude(942.32);
if (!isnan(reading) && significantChange(lastAltitudeReading, reading, 1.0)) {
altitudeCharacteristic.setValue(reading);
Serial.print(F("Altitude: ")); Serial.print(reading); Serial.println(F("m"));
lastAltitudeReading = reading;
}
}
boolean significantChange(float val1, float val2, float threshold) {
return (abs(val1 - val2) >= threshold);
}
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