This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52 Bluefruit + BME280 , Bluefruit API problem

Hi,

I am trying to make a weather station with nRF52 feather and BME280.
Currently I am making a measurement program. The base code is Custom:Heart Rate Monitor Example from Bluefruit API.

[url]learn.adafruit.com/.../url]

However, I could not update the measurement value. It seems that my code can not update the BLE Characteristic value.
It will be great help if you review my code teach me hot to fix the problem.

Here is the code.

Thanks so much.
Best regards,

S-H Choe
[code]#include <bluefruit.h>

//Service
BLEService weathers = BLEService(0xBBB0);
// Characteristic
BLECharacteristic tempc = BLECharacteristic(0xBBB1);
BLECharacteristic humidc = BLECharacteristic(0xBBB2);
BLECharacteristic pressc = BLECharacteristic(0xBBB3);

BLEDis bledis; // DIS (Device Information Service) helper class instance

// BME280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C

unsigned long delayTime;

double tempvalue = 01;
double humidvalue = 0;
double pressvalue = 0;

// Advanced function prototypes

void startAdv(void);
void setupweathers(void);
void connect_callback(uint16_t conn_handle);
void disconnect_callback(uint16_t conn_handle, uint8_t reason);

void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 Weather Station");
Serial.println("-----------------------\n");
// Initialize bme280
bme.begin();
// Initialise the Bluefruit module
Serial.println("Initialise the Bluefruit nRF52 module");
Bluefruit.begin();

// Set the advertised device name (keep it short!)
Serial.println("Setting Device Name to 'Feather52 Weather Station'");
Bluefruit.setName("HGST Weather");

// Set the connect/disconnect callback handlers
Bluefruit.setConnectCallback(connect_callback);
Bluefruit.setDisconnectCallback(disconnect_callback);

// Configure and Start the Device Information Service
Serial.println("Configuring the Device Information Service");
bledis.setManufacturer("HGST Japan");
bledis.setModel("Sensor Bluefruit52");
bledis.begin();

// Setup the Weather Station service using
// BLEService and BLECharacteristic classes
Serial.println("Configuring the Weather Service");
setupweathers();

// Setup the advertising packet(s)
Serial.println("Setting up the advertising payload(s)");
startAdv();

Serial.println("Ready Player One!!!");
Serial.println("\nAdvertising");

}


void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();

// Include Weather Service UUID
Bluefruit.Advertising.addService(weathers);

// Include Name
Bluefruit.Advertising.addName();

/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* developer.apple.com/.../_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}


void setupweathers(void)
{
// Configure Weather Station
// Weather Station Service (0xBBB0)
// Supported Characteristics:
// Name UUID Requirement Properties
// ---------------------------- ------ ----------- ----------
// Temperature 0xBBB1 Mandatory Read, Notify
// Humidity 0xBBB2 Mandatory Read, Notify
// Pressure 0xBBB3 Mandatory Read, Notify


// Service begin
weathers.begin();
// Note: You must call .begin() on the BLEService before calling .begin() on
// any characteristic(s) within that service definition.. Calling .begin() on
// a BLECharacteristic will cause it to be added to the last BLEService that
// was 'begin()'ed!

// Configure Temp characteristic
// Properties = Notify & Read
tempc.setProperties(CHR_PROPS_NOTIFY);
tempc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
tempc.setFixedLen(1);
tempc.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates
tempc.begin();
// Data format
//uint8_t tempdata[1] = {0}; // Set the characteristic to use decimal values,
uint16_t tempdata[1] = {0}; // Set the characteristic to use decimal values,

tempc.notify(tempdata, sizeof(tempdata)); // Use .notify instead of .write!

// Configure humidity characteristic
// Properties = Notify & Read
humidc.setProperties(CHR_PROPS_NOTIFY);
humidc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
humidc.setFixedLen(1);
humidc.begin();
// Data format
uint8_t humiddata[1] = {0}; // Set the characteristic to use decimal values,
humidc.notify(humiddata, sizeof(humiddata)); // Use .notify instead of .write!

// Configure Pressure characteristic
// Properties = Notify & Read
pressc.setProperties(CHR_PROPS_NOTIFY);
pressc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
pressc.setFixedLen(1);
pressc.begin();
// Data format
uint8_t pressdata[1] = {0x40}; // Set the characteristic to use decimal values,
pressc.notify(pressdata, sizeof(pressdata)); // Use .notify instead of .write!

}

void connect_callback(uint16_t conn_handle)
{
char central_name[32] = { 0 };
Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name));

Serial.print("Connected to ");
Serial.println(central_name);
}

void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;

Serial.println("Disconnected");
Serial.println("Advertising!");
}

void cccd_callback(BLECharacteristic& chr, uint16_t cccd_value)
{
// Display the raw request packet
Serial.print("CCCD Updated: ");
//Serial.printBuffer(request->data, request->len);
Serial.print(cccd_value);
Serial.println("");

// Check the characteristic this CCCD update is associated with in case
// this handler is used for multiple CCCD records.
if (chr.uuid == tempc.uuid) {
if (chr.indicateEnabled()) {
Serial.println("Temperature Measurement 'Indicate' enabled");
} else {
Serial.println("Temperature Measurement 'Indicate' disabled");
}
}
}

/*
void loop()
{
digitalToggle(LED_RED);

if ( Bluefruit.connected() ) {

uint16_t tempdata[1] = { tempvalue }; // Sensor connected,

// Note: We use .notify instead of .write!
// If it is connected but CCCD is not enabled
// The characteristic's value is still updated although notification is not sent
if ( tempc.notify(tempdata, sizeof(tempdata)) ){
Serial.print("Temp: "); Serial.println(tempvalue);

}else{
Serial.println("ERROR: Notify not set in the CCCD or not connected!");
}
}

tempvalue = bme.readTemperature();
humidvalue = bme.readHumidity();
pressvalue = bme.readPressure();

// Only send update once per second
delay(1000);
}
*/

void loop()
{
pollSensors();
delay(1000);
}
void pollSensors()
{
tempvalue = bme.readTemperature();
humidvalue = bme.readHumidity();
pressvalue = bme.readPressure();

uint16_t tempdata[1] = { tempvalue };
uint16_t humiddata[1] = { humidvalue };
uint16_t pressdata[1] = { pressvalue };

Serial.print(F("Temp"));
Serial.println(tempvalue);
Serial.print(F("Humid"));
Serial.println(humidvalue);
Serial.print(F("Press"));
Serial.println(pressvalue);


}


[/code]

Related