Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit d070168

Browse files
DHJeffchiararuggeri
authored andcommitted
Battery service (#53)
* Added read battery capacity function * Added battery service example
1 parent 4108222 commit d070168

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

libraries/BAT/bat.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,16 @@ double bat::analogRead(void) {
6262

6363
return voltage;
6464
}
65+
66+
int bat::capacity(void) {
67+
double calculate_voltage;
68+
double get_voltage;
69+
int capacity;
70+
get_voltage = analogRead();
71+
calculate_voltage = get_voltage - 2.9;
72+
capacity = calculate_voltage / 0.0124;
73+
if (capacity > 100)
74+
capacity = 100;
75+
76+
return capacity;
77+
}

libraries/BAT/bat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class bat {
4141
public:
4242
bat(void);
4343
double analogRead(void);
44+
int capacity(void);
4445
private:
4546
uint8_t recBuffer[REC_BUF_LENGTH];
4647

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

libraries/BAT/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bat KEYWORD1
1313
#######################################
1414

1515
analogRead KEYWORD2
16+
capacity KEYWORD2
1617

1718
#######################################
1819
# Constants (LITERAL1)

0 commit comments

Comments
 (0)