Arduino BLE programming

How to collect and publish data using Bluetooth Low Energy with Arduino

#include <ArduinoBLE.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

Adafruit_BME280 sensor;

#define SCIENCE_KIT_UUID(val) ("555a0002-" val
"-467a-9538-01f0652c74e8")

BLEService service(SCIENCE_KIT_UUID("0000"));
BLEFloatCharacteristic tempChar(SCIENCE_KIT_UUID("0014"),
BLENotify);
BLEFloatCharacteristic pressChar(SCIENCE_KIT_UUID("0015"),
BLENotify);
BLECharacteristic bme280Data(SCIENCE_KIT_UUID("0020"),
BLENotify, 3 * sizeof(float));
BLEService service(SCIENCE_KIT_UUID(“0000”));
void setup() {   
BLE.begin();
sensor.begin(0x76);
delay(2000);
BLE.setLocalName("ArduinoGO");
BLE.setDeviceName("ArduinoGO");
BLE.setAdvertisedService(service);
service.addCharacteristic(temperatureCharacteristic);
service.addCharacteristic(pressureCharacteristic);
service.addCharacteristic(bme280Data);
BLE.addService(service);
BLE.advertise();
}
bool firstConnection = true;  void loop() {   
while (BLE.connected()) {
// once a client connects stay in the loop and publish data
if (firstConnection) {
BLEDevice central = BLE.central();
firstConnection = false;
}
publishData();
delay(1000);
}
}
void publishData() {
unsigned long t0 = micros();
float T = sensor.readTemperature();
float p = sensor.readPressure()/1.0e2;
unsigned long t1 = micros();

// publish to BLE
temperatureCharacteristic.writeValue(T);
pressureCharacteristic.writeValue(p);

float pTdata[3];
pTdata[0] = 0.5*(t0+t1);
pTdata[1] = p;
pTdata[2] = T;
bme280Data.writeValue((byte*)pTdata, sizeof(pTdata));
}

Professor of Physics at Sapienza Università di Roma. Member of the CMS and PADME collaborations. Arduino advocate and phyphox ambassador.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store