I’m trying to set up a very simple project where I read from a humidity sensor and send the data via BLE. I’m planning on having this running on a battery but the current power consumption (15mA) of the board is too high for that to be viable.
I have another device connecting and reading the data every 15min and I was expecting the board to go to some sort of sleep mode when no one is connected to it.
Is there a way I can reduce this?
(I pretty much just copied the LEDCallback example and added the sensor reading piece)
#include <ArduinoBLE.h>
#define MIN_VAL 305
#define MAX_VAL 677
#define SAMPLE_RATE 8
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLEShortCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
void setup() {
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set the local name peripheral advertises
BLE.setLocalName("LEDCallback");
// set the UUID for the service this peripheral advertises
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// start advertising
BLE.advertise();
}
int lastVal = 0;
void loop() {
BLE.poll();
int reading = getReading();
if(lastVal != reading) {
lastVal = reading;
switchCharacteristic.setValue(reading);
}
}
int getReading() {
int val = 0;
for (int i = 0; i < SAMPLE_RATE; i++)
{
val += analogRead(A0);
delay(1);
}
val /= SAMPLE_RATE;
val = map(val, MIN_VAL, MAX_VAL, 100, 0);
return constrain(val, 0, 100);
}