Hi everyone,
i recently got an Arduino nano 33 IoT and I wanted to experiment on Bluetooth Low Energy.
My expertiment consisted on hooking up to the arduino some temperature and pressure sensors and logging data every minute using BLE connectivity offered by the board.
I modified the BatteryLevel.ino example in order to retrive temperature and pressure from a BMP280 and transmit them via BLE.
The point was that if I try to put into sleep the board after it transmits data I am not able to hold the connection from the peripheral device.
My goal is to battery power that device, when connected to a 3.7 V battery it draws ~ 50 mA.
Code is following,
Thank you in advance for the advices!
#include <ArduinoBLE.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#define SERVICE_UUID "517c08ac-f21c-4f78-b7f4-4d34f151f6ac"
#define T_UUID "8dbff573-85e7-4d42-97a1-f778e10f988a"
#define P_UUID "0b7a7690-c3e5-4952-816d-680fcc46e3ce"
#define DELAY 2000;
/*
* Ble services setup
*/
BLEService WeatherLogs(SERVICE_UUID);
BLEFloatCharacteristic Pressure(P_UUID, BLERead | BLENotify);
BLEFloatCharacteristic Temperature(T_UUID, BLERead | BLENotify);
/**
* Barometric pressure and temperature sensor setup
*/
Adafruit_BMP280 bmp; // use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
float T = 0;
float P = 0;
long prevMillis = 0;
long currMillis{0};
long interval = 2000;
void blink_bl(unsigned int d){
delay(d);
digitalWrite(LED_BUILTIN, LOW);
delay(d);
digitalWrite(LED_BUILTIN, HIGH);
}
void setup() {
//Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) {
digitalWrite(LED_BUILTIN, HIGH);
//Serial.println("starting BLE failed!");
while (1);
}
unsigned status;
status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
if(!status){
blink_bl(200);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
BLE.setLocalName("Pressure_Temp_Sensor");
BLE.setAdvertisedService(WeatherLogs);
WeatherLogs.addCharacteristic(Pressure);
WeatherLogs.addCharacteristic(Temperature);
BLE.addService(WeatherLogs);
BLE.advertise();
//Serial.println("Waiting for connection");
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
BLEDevice central = BLE.central();
if(central){
//Serial.print("Connected to central, MAC : ");
//Serial.println(central.address());
//digitalWrite(LED_BUILTIN, HIGH);
BLE.stopAdvertise();
while(central.connected()){
//updateReadings();
currMillis = millis();
long delta = currMillis - prevMillis;
if(delta >= interval){
prevMillis = currMillis;
updateReadings();
}
/*
* if I change the previous code to
* updateReadings();
* delay(60000); // or other sleep functions
*
* it stops working
*/
}
BLE.advertise();
digitalWrite(LED_BUILTIN, LOW);
}
}
void readFromSensors(){
sensors_event_t temp_event, pressure_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pressure_event);
T = temp_event.temperature - 1.0f;
P = pressure_event.pressure;
}
void updateReadings(){
readFromSensors();
Pressure.writeValue(P);
Temperature.writeValue(T);
}