Any ideas on how I may reduce my power consumption? I measure a quiescent current draw of 7mA running the following code. I'm happy to reduce the TX power because my project only requires a range of a couple of meters. Is there a "setTxPower(low) type of function I may use? I'm running on a Seeed XIAO BLE Sense (board manager 2.6.1).
#include <TimeLib.h>
#include <ArduinoBLE.h>
BLEService allService("813e67ed-3cf4-4f27-82e5-a65799933158"); // Bluetooth® Low Energy Batt Voltage Service
BLEService timeService("d2e4ba7e-5f59-4d44-acfc-5726f4ab965a"); // Bluetooth® Low Energy Time Service
// Bluetooth® Low Energy Voltage Characteristic - custom 128-bit UUID, read and writable by central
BLEStringCharacteristic battCharacteristic("8aa8f8ba-c1a5-421d-87c9-79fa3a231027", BLERead | BLENotify, 80);
BLEStringCharacteristic timeCharacteristic("b4103f2b-1be3-411b-b362-65683ac4a7f1", BLERead | BLENotify, 60);
int analogV1_counts;
int analogV2_counts;
int Batt_counts;
int timeNow;
int secondCounter = 0;
int timeCounter;
String reading1, reading2;
void setup() {
Serial.begin(115200);
analogReadResolution(12);
analogReference(AR_INTERNAL2V4); // Default=3.30V, INTERNAL=0.60V, 1V2=1.2V, 2V4=2.4V
if (!BLE.begin()) { // Begin BLE initialization
Serial.println("Starting XIAO BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("XIAO BLE");
BLE.setAdvertisedService(allService);
BLE.setAdvertisedService(timeService);
// add the characteristic to the service
allService.addCharacteristic(battCharacteristic);
allService.addCharacteristic(timeCharacteristic);
// add service
BLE.addService(allService);
BLE.addService(timeService);
// start advertising
BLE.advertise();
Serial.println("XIAO BLE active, waiting for connections...");
delay(10);
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
delay(1);
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// Recheck if a central is connected to peripheral:
while (central.connected()) {
digitalWrite(LED_BLUE, LOW); // turn on blue LED
timeNow = now(); // count in seconds from start of program
analogV1_counts = analogRead(A0); // read voltage V1 at pin A0 from sensor
analogV2_counts = analogRead(A1); // read voltage V2 at pin A1 from sensor
delay(10); // delay increases brightness for the LED
digitalWrite(LED_BLUE, HIGH); // turn off blue LED
// Change analog voltage int data to string
reading1 = "Time," + (String)timeNow + ",V1," + (String)analogV1_counts + ",V2," + (String)analogV2_counts;
// Check if 120 seconds (2 minutes) have passed. If yes, send Batt volts by BLE
if (timeCounter == 120) {
Batt_counts = analogRead(A2); // read external battery at pin A2
reading2 = "Time," + (String)timeNow + ",Batt," + (String)Batt_counts;
battCharacteristic.writeValue(reading2);
secondCounter = 0;
Serial.println("2min_interval," + reading2);
}
timeCharacteristic.writeValue(reading1); //update reading1 BLE characteristic
Serial.println(reading1);
timeCounter += 2; // add 2 seconds to timeCounter every loop
delay(1990); // sum of delays is about 2 seconds per loop
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
delay(1000);
}
}