Skip to content

Commit

Permalink
Merge pull request #445 from Flo100/bleBatterylevel
Browse files Browse the repository at this point in the history
Added ble Battery level to measurements
  • Loading branch information
doudar authored Feb 24, 2023
2 parents 0d50b15 + 703024f commit 49a3ba7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
-
- Added battery monitoring of BLE devices by @Flo100.
### Changed
- Filesystem no longer updates when auto-update is unchecked.
- Holding shifter buttons on boot now erases LittleFS as well as resetting settings.
Expand Down
1 change: 1 addition & 0 deletions include/BLE_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class SpinBLEClient {
void resetDevices();
void postConnect();
void FTMSControlPointWrite(const uint8_t *pData, int length);
void handleBattInfo(NimBLEClient *pClient, bool updateNow);
};
class MyAdvertisedDeviceCallback : public NimBLEAdvertisedDeviceCallbacks {
public:
Expand Down
2 changes: 2 additions & 0 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class RuntimeParameters {

public:
Measurement watts;
Measurement pm_batt;
Measurement hr;
Measurement hr_batt;
Measurement cad;
Measurement resistance;

Expand Down
3 changes: 3 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@
// If ble devices are both setup, how often to attempt a reconnect.
#define BLE_RECONNECT_INTERVAL 15

// Interval for polling ble battery updates
#define BATTERY_UPDATE_INTERVAL_MILLIS 300000

// Initial and web scan duration.
#define DEFAULT_SCAN_DURATION 10

Expand Down
4 changes: 4 additions & 0 deletions lib/SS2K/include/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#define HEARTSERVICE_UUID NimBLEUUID((uint16_t)0x180D)
#define HEARTCHARACTERISTIC_UUID NimBLEUUID((uint16_t)0x2A37)

//BatteryLevel Service
#define BATTERYSERVICE_UUID NimBLEUUID((uint16_t)0x180F) // heart rate sensor service uuid, as defined in gatt specifications
#define BATTERYCHARACTERISTIC_UUID NimBLEUUID ((uint16_t)0x2A19)

// Cycling Power Service
#define CSCSERVICE_UUID NimBLEUUID((uint16_t)0x1816)
#define CSCMEASUREMENT_UUID NimBLEUUID((uint16_t)0x2A5B)
Expand Down
48 changes: 41 additions & 7 deletions src/BLE_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void onNotify(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t
// BLE Client loop task
void bleClientTask(void *pvParameters) {
for (;;) {
vTaskDelay(BLE_CLIENT_DELAY / portTICK_PERIOD_MS); // Delay a second between loops.
vTaskDelay(BLE_CLIENT_DELAY / portTICK_PERIOD_MS); // Delay a second between loops.
if (spinBLEClient.doScan && (spinBLEClient.scanRetries > 0) && !NimBLEDevice::getScan()->isScanning()) {
spinBLEClient.scanRetries--;
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Initiating Scan from Client Task:");
Expand Down Expand Up @@ -267,19 +267,22 @@ bool SpinBLEClient::connectToServer() {
** Remove as you see fit for your needs */

void MyClientCallback::onConnect(NimBLEClient *pClient) {
// Currently Not Used
// additional characteristic subscriptions.
spinBLEClient.handleBattInfo(pClient, true);
}

void MyClientCallback::onDisconnect(NimBLEClient *pclient) {
void MyClientCallback::onDisconnect(NimBLEClient *pClient) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Disconnect Called");

if (spinBLEClient.intentionalDisconnect) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Intentional Disconnect");
spinBLEClient.intentionalDisconnect = false;
return;
}
if (!pclient->isConnected()) {
NimBLEAddress addr = pclient->getPeerAddress();

//cleanup spinBLE.myDevices
if (!pClient->isConnected()) {
NimBLEAddress addr = pClient->getPeerAddress();
// auto addr = BLEDevice::getDisconnectedClient()->getPeerAddress();
SS2K_LOG(BLE_CLIENT_LOG_TAG, "This disconnected client Address %s", addr.toString().c_str());
for (size_t i = 0; i < NUM_BLE_DEVICES; i++) {
Expand All @@ -288,13 +291,16 @@ void MyClientCallback::onDisconnect(NimBLEClient *pclient) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Detected %s Disconnect", spinBLEClient.myBLEDevices[i].serviceUUID.toString().c_str());
spinBLEClient.myBLEDevices[i].doConnect = true;
if ((spinBLEClient.myBLEDevices[i].charUUID == CYCLINGPOWERMEASUREMENT_UUID) || (spinBLEClient.myBLEDevices[i].charUUID == FITNESSMACHINEINDOORBIKEDATA_UUID) ||
(spinBLEClient.myBLEDevices[i].charUUID == FLYWHEEL_UART_RX_UUID) || (spinBLEClient.myBLEDevices[i].charUUID == ECHELON_SERVICE_UUID)) {
(spinBLEClient.myBLEDevices[i].charUUID == FLYWHEEL_UART_RX_UUID) || (spinBLEClient.myBLEDevices[i].charUUID == ECHELON_SERVICE_UUID) ||
(spinBLEClient.myBLEDevices[i].charUUID == CYCLINGPOWERSERVICE_UUID)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Deregistered PM on Disconnect");
rtConfig.pm_batt.setValue(0);
spinBLEClient.connectedPM = false;
break;
}
if ((spinBLEClient.myBLEDevices[i].charUUID == HEARTCHARACTERISTIC_UUID)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Deregistered HR on Disconnect");
rtConfig.hr_batt.setValue(0);
spinBLEClient.connectedHR = false;
break;
}
Expand Down Expand Up @@ -507,7 +513,8 @@ void SpinBLEClient::postConnect() {
myBLEDevices[i].postConnected = true;
NimBLEClient *pClient = NimBLEDevice::getClientByPeerAddress(myBLEDevices[i].peerAddress);
if ((this->myBLEDevices[i].charUUID == CYCLINGPOWERMEASUREMENT_UUID) || (this->myBLEDevices[i].charUUID == FITNESSMACHINEINDOORBIKEDATA_UUID) ||
(this->myBLEDevices[i].charUUID == FLYWHEEL_UART_RX_UUID) || (this->myBLEDevices[i].charUUID == ECHELON_DATA_UUID)) {
(this->myBLEDevices[i].charUUID == FLYWHEEL_UART_RX_UUID) || (this->myBLEDevices[i].charUUID == ECHELON_DATA_UUID) ||
(this->myBLEDevices[i].charUUID == CYCLINGPOWERSERVICE_UUID)) {
this->connectedPM = true;
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Registered PM on Connect");

Expand Down Expand Up @@ -613,3 +620,30 @@ void SpinBLEAdvertisedDevice::print() {
strcat(logBufP, "|");
SS2K_LOG(BLE_CLIENT_LOG_TAG, "%s", String(logBuf));
}

// Poll BLE devices for battCharacteristic if available and read value.
void SpinBLEClient::handleBattInfo(NimBLEClient *pClient, bool updateNow=false) {
static unsigned long last_battery_update = 0;
if ((millis() - last_battery_update >= BATTERY_UPDATE_INTERVAL_MILLIS) || (last_battery_update == 0) || updateNow) {
last_battery_update = millis();
if (pClient->getService(HEARTSERVICE_UUID)) { // get battery level at first connect
BLERemoteCharacteristic *battCharacteristic = pClient->getService(BATTERYSERVICE_UUID)->getCharacteristic(BATTERYCHARACTERISTIC_UUID);
if (battCharacteristic != nullptr) {
std::string value = battCharacteristic->readValue();
rtConfig.hr_batt.setValue((uint8_t)value[0]);
SS2K_LOG(BLE_CLIENT_LOG_TAG, "HRM battery updated %d", (int)value[0]);
} else {
rtConfig.hr_batt.setValue(0);
}
} else if (pClient->getService(CYCLINGPOWERMEASUREMENT_UUID) || pClient->getService(CYCLINGPOWERSERVICE_UUID)) { // get batterylevel at first connect
BLERemoteCharacteristic *battCharacteristic = pClient->getService(BATTERYSERVICE_UUID)->getCharacteristic(BATTERYCHARACTERISTIC_UUID);
if (battCharacteristic != nullptr) {
std::string value = battCharacteristic->readValue();
rtConfig.pm_batt.setValue((uint8_t)value[0]);
SS2K_LOG(BLE_CLIENT_LOG_TAG, "PM battery updated %d", (int)value[0]);
} else {
rtConfig.pm_batt.setValue(0);
}
}
}
}
6 changes: 4 additions & 2 deletions src/BLE_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Main.h"
#include "SS2KLog.h"
#include "BLE_Common.h"

#include <Constants.h>
#include <math.h>
#include <sensors/SensorData.h>
#include <sensors/SensorDataFactory.h>
Expand Down Expand Up @@ -54,8 +54,10 @@ void BLECommunications(void *pvParameters) {
pData[i] = incomingNotifyData.data[i];
}
collectAndSet(pRemoteBLECharacteristic->getUUID(), myAdvertisedDevice.serviceUUID, pRemoteBLECharacteristic->getRemoteService()->getClient()->getPeerAddress(), pData, length);

}

spinBLEClient.handleBattInfo(pClient, false);

} else if (!pClient->isConnected()) { // This shouldn't ever be
// called...
if (pClient->disconnect() == 0) { // 0 is a successful disconnect
Expand Down

0 comments on commit 49a3ba7

Please sign in to comment.