1+ /*
2+ BatteryServicePeripheral.ino
3+ Written by Chiara Ruggeri (chiara@arduino.org)
4+ Jeff Wu (jeff@arduino.org)
5+
6+ This example shows how to read a characteristic to get battery capacity of Primo board
7+ Disabling STM32 enter to standby mode when Primo board is powered only by battery.
8+ You can use nRFConnect app to read the characteristic
9+ https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-Connect-for-mobile-previously-called-nRF-Master-Control-Panel
10+
11+ In this example allows you to get battery capacity of Primo board. It will blink every 200 ms when the board is advertising.
12+ It will be on when the board is connected to a central. It will be off when the board is disconnected.
13+
14+ This example code is in the public domain.
15+ */
16+
17+ #include < BLEPeripheral.h>
18+ #include < stm32pwr.h>
19+ #include < bat.h>
20+
21+ // create peripheral instance
22+ BLEPeripheralRole blePeripheral = BLEPeripheralRole();
23+
24+ // create service
25+ BLEService batteryService = BLEService(" 180F" );
26+
27+ // create battery level characteristic
28+ BLECharCharacteristic batLevelChar = BLECharCharacteristic(" 2A19" , BLERead | BLENotify);
29+
30+ bat primo_bat;
31+
32+ void setup () {
33+ // start serial port at 9600 bps
34+ Serial.begin (9600 );
35+
36+ // disable STM32 enter to standby mode
37+ stm32pwr.disableStandbyMode ();
38+
39+ // initialize BLE led
40+ pinMode (BLE_LED, OUTPUT);
41+
42+ // set advertised local name and service UUID
43+ blePeripheral.setLocalName (" Battery information" );
44+ blePeripheral.setAdvertisedServiceUuid (batteryService.uuid ());
45+
46+ // add service and characteristic
47+ blePeripheral.addAttribute (batteryService);
48+ blePeripheral.addAttribute (batLevelChar);
49+
50+ // begin initialization
51+ blePeripheral.begin ();
52+
53+ Serial.println (F (" BLE Battery Service Peripheral" ));
54+ }
55+
56+ void loop () {
57+ // retrieve the peripheral status in order to blink only when advertising
58+ if (blePeripheral.status () == ADVERTISING){
59+ digitalWrite (BLE_LED, LOW);
60+ delay (200 );
61+ digitalWrite (BLE_LED, HIGH);
62+ delay (200 );
63+ }
64+ else { // if we are not advertising, we are connected
65+ double bat_voltage;
66+ // get battery voltage value
67+ bat_voltage = primo_bat.analogRead ();
68+ // print battery voltage vaule on serial port
69+ Serial.print (" Battery Voltage:" );
70+ Serial.println (bat_voltage,3 );
71+
72+ int bat_capacity;
73+ digitalWrite (BLE_LED, HIGH);
74+ // get battery capacity
75+ bat_capacity = primo_bat.capacity ();
76+ // retrieve battery level and update the characteristic
77+ batLevelChar.setValue (bat_capacity);
78+ Serial.println (bat_capacity);
79+ delay (1000 );
80+ }
81+ }
0 commit comments