2
2
#include < ArduinoBLE.h>
3
3
4
4
constexpr auto printInterval { 4000ul };
5
- constexpr auto batteryMeasureInterval { 5000ul };
5
+ constexpr auto batteryUpdateInterval { 4000ul };
6
6
int8_t batteryChargeLevel = -1 ;
7
7
int8_t batteryPercentage = -1 ;
8
8
float batteryVoltage = -1 .0f ;
9
+ int8_t runsOnBattery = -1 ; // Using an int to be able to represent an unknown state.
10
+ int8_t batteryIsCharging = -1 ; // Using an int to be able to represent an unknown state.
9
11
10
12
11
13
#define DEVICE_NAME " NiclaSenseME"
@@ -15,10 +17,39 @@ BLEService service(DEVICE_UUID("0000"));
15
17
BLEIntCharacteristic batteryPercentageCharacteristic (DEVICE_UUID(" 1001" ), BLERead | BLENotify);
16
18
BLEFloatCharacteristic batteryVoltageCharacteristic (DEVICE_UUID(" 1002" ), BLERead | BLENotify);
17
19
BLEIntCharacteristic batteryChargeLevelCharacteristic (DEVICE_UUID(" 1003" ), BLERead | BLENotify);
20
+ BLEBooleanCharacteristic runsOnBatteryCharacteristic (DEVICE_UUID(" 1004" ), BLERead | BLENotify);
21
+ BLEBooleanCharacteristic isChargingCharacteristic (DEVICE_UUID(" 1005" ), BLERead | BLENotify);
18
22
19
- bool updateBatteryLevel ( bool enforceNewReading = false ) {
23
+ bool updateBatteryStatus () {
20
24
static auto updateTimestamp = millis ();
21
- bool intervalFired = millis () - updateTimestamp >= batteryMeasureInterval;
25
+ bool intervalFired = millis () - updateTimestamp >= batteryUpdateInterval;
26
+ bool isFirstReading = runsOnBattery == -1 || batteryIsCharging == -1 ;
27
+
28
+ if (intervalFired || isFirstReading) {
29
+ Serial.println (" Checking the battery status..." );
30
+ updateTimestamp = millis ();
31
+ int8_t isCharging = nicla::getOperatingStatus () == OperatingStatus::Charging;
32
+ int8_t batteryPowered = nicla::runsOnBattery ();
33
+ bool valueUpdated = false ;
34
+
35
+ if (batteryIsCharging != isCharging) {
36
+ batteryIsCharging = isCharging;
37
+ valueUpdated = true ;
38
+ }
39
+
40
+ if (runsOnBattery != batteryPowered) {
41
+ runsOnBattery = batteryPowered;
42
+ valueUpdated = true ;
43
+ }
44
+
45
+ return valueUpdated;
46
+ }
47
+
48
+ return false ;
49
+ }
50
+
51
+ static auto updateTimestamp = millis();
52
+ bool intervalFired = millis() - updateTimestamp >= batteryUpdateInterval;
22
53
bool isFirstReading = batteryPercentage == -1 || batteryVoltage == -1 .0f ;
23
54
24
55
if (intervalFired || isFirstReading || enforceNewReading) {
@@ -112,6 +143,22 @@ void onBatteryChargeLevelCharacteristicRead(BLEDevice central, BLECharacteristic
112
143
batteryChargeLevelCharacteristic.writeValue (batteryChargeLevel);
113
144
}
114
145
146
+ void onRunsOnBatteryCharacteristicRead (BLEDevice central, BLECharacteristic characteristic) {
147
+ Serial.println (" Checking if device runs on battery..." );
148
+ updateBatteryStatus ();
149
+ Serial.print (" Runs on battery: " );
150
+ Serial.println (runsOnBattery == 1 ? " Yes" : " No" );
151
+ runsOnBatteryCharacteristic.writeValue (runsOnBattery == 1 );
152
+ }
153
+
154
+ void onIsChargingCharacteristicRead (BLEDevice central, BLECharacteristic characteristic) {
155
+ Serial.println (" Checking if battery is charging..." );
156
+ updateBatteryStatus ();
157
+ Serial.print (" Battery is charging: " );
158
+ Serial.println (batteryIsCharging == 1 ? " Yes" : " No" );
159
+ isChargingCharacteristic.writeValue (batteryIsCharging == 1 );
160
+ }
161
+
115
162
void onCharacteristicSubscribed (BLEDevice central, BLECharacteristic characteristic) {
116
163
Serial.println (" Device subscribed to characteristic: " + String (characteristic.uuid ()));
117
164
}
@@ -150,6 +197,16 @@ void setupBLE() {
150
197
batteryChargeLevelCharacteristic.setEventHandler (BLESubscribed, onCharacteristicSubscribed);
151
198
batteryChargeLevelCharacteristic.writeValue (batteryChargeLevel);
152
199
200
+ service.addCharacteristic (runsOnBatteryCharacteristic);
201
+ runsOnBatteryCharacteristic.setEventHandler (BLERead, onRunsOnBatteryCharacteristicRead);
202
+ runsOnBatteryCharacteristic.setEventHandler (BLESubscribed, onCharacteristicSubscribed);
203
+ runsOnBatteryCharacteristic.writeValue (runsOnBattery == 1 );
204
+
205
+ service.addCharacteristic (isChargingCharacteristic);
206
+ isChargingCharacteristic.setEventHandler (BLERead, onIsChargingCharacteristicRead);
207
+ isChargingCharacteristic.setEventHandler (BLESubscribed, onCharacteristicSubscribed);
208
+ isChargingCharacteristic.writeValue (batteryIsCharging == 1 );
209
+
153
210
BLE.addService (service);
154
211
BLE.advertise ();
155
212
}
@@ -177,6 +234,7 @@ void loop()
177
234
178
235
if (BLE.connected ()) {
179
236
bool newBatteryLevelAvailable = updateBatteryLevel ();
237
+ bool newBatteryStatusAvailable = updateBatteryStatus ();
180
238
181
239
if (batteryPercentageCharacteristic.subscribed () && newBatteryLevelAvailable) {
182
240
Serial.print (" Battery Percentage: " );
@@ -195,7 +253,19 @@ void loop()
195
253
Serial.println (batteryChargeLevel);
196
254
batteryChargeLevelCharacteristic.writeValue (batteryChargeLevel);
197
255
}
198
-
256
+
257
+ if (runsOnBatteryCharacteristic.subscribed () && newBatteryStatusAvailable) {
258
+ Serial.print (" Runs on battery: " );
259
+ Serial.println (runsOnBattery == 1 ? " Yes" : " No" );
260
+ runsOnBatteryCharacteristic.writeValue (runsOnBattery == 1 );
261
+ }
262
+
263
+ if (isChargingCharacteristic.subscribed () && newBatteryStatusAvailable) {
264
+ Serial.print (" Battery is charging: " );
265
+ Serial.println (batteryIsCharging == 1 ? " Yes" : " No" );
266
+ isChargingCharacteristic.writeValue (batteryIsCharging == 1 );
267
+ }
268
+
199
269
return ;
200
270
}
201
271
0 commit comments