Skip to content

Commit 026f4c0

Browse files
committed
Replace battery monitor example with new version
1 parent 7767d3d commit 026f4c0

File tree

2 files changed

+44
-184
lines changed

2 files changed

+44
-184
lines changed

libraries/CurieBle/examples/BatteryMonitor/BatteryMonitor.ino

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,115 +25,94 @@
2525
/* BLE Peripheral Device (this Intel Curie device) */
2626
BLEPeripheral blePeripheral;
2727

28-
/* UUID for Battery service */
29-
#define SERVICE_UUID_BATTERY "180F"
30-
/* UUID for Battery Level characteristic */
31-
#define CHAR_UUID_BATTERY_LEVEL "2A19"
32-
33-
/* Serial port to use for printing informational messages to the user */
34-
#define LOG_SERIAL Serial
35-
36-
/* For convenience, this macro will invoke a specified function call and will
37-
* check the status value returned to ensure it is successful. If not, it will
38-
* print an error message to the serial port and will return from the current function
39-
*/
40-
#define CHECK_STATUS(op) \
41-
do { \
42-
BleStatus status = op; \
43-
if (BLE_STATUS_SUCCESS != status) { \
44-
LOG_SERIAL.print(#op" returned error status: "); \
45-
LOG_SERIAL.println(status); \
46-
return; \
47-
} \
48-
} while(0)
49-
5028
/* BLE Battery Service */
51-
BLEService battSvc(SERVICE_UUID_BATTERY);
29+
BLEService battSvc("180F");
5230

5331
/* BLE Battery Level Characteristic */
54-
BLEUnsignedCharCharacteristic battLvlChar(CHAR_UUID_BATTERY_LEVEL, /* standard 16-bit characteristic UUID */
32+
BLEUnsignedCharCharacteristic battLvlChar("2A19", /* standard 16-bit characteristic UUID */
5533
BLERead | BLENotify /* remote clients will be able to get notifications if this characteristic changes */
5634
);
5735

5836
/* Variable to keep track of last battery level reading from analog input */
59-
unsigned char oldBattLvl = 0;
60-
61-
/* This function will be called when a BLE GAP event is detected by the
62-
* Intel Curie BLE device */
63-
void blePeripheralConnectedEventCb(BLECentral &bleCentral)
64-
{
65-
/* We've got a new connection. Lets print the MAC address of the remote device */
66-
LOG_SERIAL.println("Got CONNECTED event");
67-
LOG_SERIAL.println(bleCentral.address());
68-
}
69-
70-
void blePeripheralDisconnectedEventCb(BLECentral &bleCentral)
71-
{
72-
LOG_SERIAL.println("Got DISCONNECTED event");
73-
}
37+
uint8_t oldBattLvl = 0;
38+
unsigned long previousMillis = 0;
7439

7540
void setup() {
41+
Serial.begin(9600);
42+
7643
pinMode(13, OUTPUT);
77-
LOG_SERIAL.begin(9600);
7844

7945
/* Set a name for the BLE device
8046
* We give it an arbitrary name which will appear in advertising packets
8147
* and can be used by remote peers to identify this BLE device
8248
* The name can be changed but must not exceed 20 characters in length */
83-
CHECK_STATUS(blePeripheral.setLocalName("AE_BATTMON"));
84-
85-
/* Set a function to be called whenever a BLE GAP event occurs */
86-
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectedEventCb);
87-
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectedEventCb);
88-
89-
CHECK_STATUS(blePeripheral.setAdvertisedServiceUuid(battSvc.uuid()));
49+
blePeripheral.setLocalName("AE_BATTMON");
50+
blePeripheral.setAdvertisedServiceUuid(battSvc.uuid());
9051

9152
/* Add the BLE Battery service, and include the UUID in BLE advertising data */
92-
CHECK_STATUS(blePeripheral.addAttribute(battSvc));
53+
blePeripheral.addAttribute(battSvc);
9354

9455
/* This service will have just one characteristic that reflects the current
9556
* percentage-charge level of the "battery" */
96-
CHECK_STATUS(blePeripheral.addAttribute(battLvlChar));
57+
blePeripheral.addAttribute(battLvlChar);
9758

9859
/* Set an initial value for this characteristic; refreshed later the loop() function */
99-
CHECK_STATUS(battLvlChar.setValue(oldBattLvl));
60+
battLvlChar.setValue(oldBattLvl);
10061

10162
/* Now activate the BLE device. It will start continuously transmitting BLE
10263
* advertising packets and thus become visible to remote BLE central devices
10364
* (e.g smartphones) until it receives a new connection */
104-
CHECK_STATUS(blePeripheral.begin());
105-
LOG_SERIAL.println("Bluetooth device active, waiting for connections...");
65+
blePeripheral.begin();
66+
Serial.println("Bluetooth device active, waiting for connections...");
10667
}
10768

10869
void loop() {
109-
static int ledState;
70+
BLECentral central = blePeripheral.central();
11071

111-
blePeripheral.poll();
72+
if (central) {
73+
// central connected to peripheral
74+
Serial.print(F("Connected to central: "));
75+
Serial.println(central.address());
11276

113-
/* Blink the on-board LED (just to show some activity) */
114-
digitalWrite(13, ledState ? HIGH : LOW);
115-
ledState = !ledState;
77+
digitalWrite(13, HIGH);
11678

79+
while (central.connected()) {
80+
// central still connected to peripheral
81+
82+
unsigned long currentMillis = millis();
83+
84+
if (currentMillis - previousMillis >= 200) {
85+
previousMillis = currentMillis;
86+
updateBatteryLevel();
87+
}
88+
}
89+
90+
digitalWrite(13, LOW);
91+
92+
// central disconnected
93+
Serial.print(F("Disconnected from central: "));
94+
Serial.println(central.address());
95+
}
96+
}
97+
98+
void updateBatteryLevel() {
11799
/* Read the current voltage level on the A0 analog input pin.
118100
* This is used here to simulate the charge level of a "battery".
119101
* The following tutorial shows how a potentiometer could be used
120102
* to vary the voltage on an analog input pin:
121103
* https://www.arduino.cc/en/Tutorial/Potentiometer
122104
*/
123-
unsigned char battLvl = map(analogRead(A0), 0, 1023, 0, 100);
105+
uint8_t battLvl = map(analogRead(A0), 0, 1023, 0, 100);
124106

125107
if (battLvl != oldBattLvl) {
126-
LOG_SERIAL.print("Battery Level % is now: ");
127-
LOG_SERIAL.println(battLvl);
128-
108+
Serial.print("Battery Level % is now: ");
109+
Serial.println(battLvl);
110+
129111
/* If the voltage level has changed, we update the value of the
130112
* Battery Level BLE characteristic. Because we have enabled
131113
* notifications for this characteristic, the remote device can
132114
* receive automatic updates when this value is changed. */
133-
CHECK_STATUS(battLvlChar.setValue(battLvl));
115+
battLvlChar.setValue(battLvl);
134116
oldBattLvl = battLvl;
135117
}
136-
137-
/* Repeat the loop every 200ms - can be changed if desired */
138-
delay(200);
139118
}

libraries/CurieBle/examples/BatteryMonitorNewAPI/BatteryMonitorNewAPI.ino

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)