Skip to content

Commit

Permalink
WIP: Add battery level for Nimble
Browse files Browse the repository at this point in the history
  • Loading branch information
caveman99 committed Nov 25, 2023
1 parent b385232 commit d6fc1c3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/nimble/NimbleBluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <NimBLEDevice.h>

NimBLECharacteristic *fromNumCharacteristic;
NimBLECharacteristic *BatteryCharacteristic;
NimBLEServer *bleServer;

static bool passkeyShowing;
Expand Down Expand Up @@ -181,20 +182,34 @@ void NimbleBluetooth::setupService()
FromRadioCharacteristic->setCallbacks(fromRadioCallbacks);

bleService->start();

// Setup the battery service
NimBLEService *batteryService = bleServer->createService(NimBLEUUID((uint16_t)0x180f)); // 0x180F is the Battery Service
BatteryCharacteristic = batteryService->createCharacteristic( // 0x2A19 is the Battery Level characteristic)
(uint16_t)0x2a19, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);

NimBLE2904 *batteryLevelDescriptor = (NimBLE2904 *)BatteryCharacteristic->createDescriptor((uint16_t)0x2904);
batteryLevelDescriptor->setFormat(NimBLE2904::FORMAT_UINT8);
batteryLevelDescriptor->setNamespace(1);
batteryLevelDescriptor->setUnit(0x27ad);

batteryService->start();
}

void NimbleBluetooth::startAdvertising()
{
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
pAdvertising->reset();
pAdvertising->addServiceUUID(MESH_SERVICE_UUID);
pAdvertising->addServiceUUID(NimBLEUUID((uint16_t)0x180f)); // 0x180F is the Battery Service
pAdvertising->start(0);
}

/// Given a level between 0-100, update the BLE attribute
void updateBatteryLevel(uint8_t level)
{
// blebas.write(level);
BatteryCharacteristic->setValue(&level, 1);
BatteryCharacteristic->notify();
}

void NimbleBluetooth::clearBonds()
Expand Down

6 comments on commit d6fc1c3

@vk4ypb
Copy link

@vk4ypb vk4ypb commented on d6fc1c3 Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks on my lilygo T-BEAM AXP2101-V1.2

Constantly reboots.
concole_dump.txt

Please see attached LOG file for console output.

@caveman99
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x4011a897: NimBLEAttValue::setValue(unsigned char const*, unsigned short) at /home/runner/work/firmware/firmware/.pio/libdeps/tbeam/NimBLE-Arduino/src/NimBLEAttValue.h:381
0x4011a897: NimBLECharacteristic::setValue(unsigned char const*, unsigned int) at /home/runner/work/firmware/firmware/.pio/libdeps/tbeam/NimBLE-Arduino/src/NimBLECharacteristic.cpp:553
0x40086bb9: esp_timer_impl_get_counter_reg at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_timer/src/esp_timer_impl_lac.c:118
0x40086bc1: esp_timer_impl_get_counter_reg at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_timer/src/esp_timer_impl_lac.c:128
0x4011a894: NimBLECharacteristic::setValue(unsigned char const*, unsigned int) at /home/runner/work/firmware/firmware/.pio/libdeps/tbeam/NimBLE-Arduino/src/NimBLECharacteristic.cpp:545
0x40104c0c: updateBatteryLevel(unsigned char) at /home/runner/work/firmware/firmware/src/nimble/NimbleBluetooth.cpp:211
0x400e81bb: MeshService::onGPSChanged(meshtastic::GPSStatus const*) at /home/runner/work/firmware/firmware/src/mesh/MeshService.cpp:345
0x400e81c5: MeshService::onGPSChanged(meshtastic::GPSStatus const*) at /home/runner/work/firmware/firmware/src/mesh/MeshService.cpp:345
0x4022e87d: NodeDB::readNextMeshNode(unsigned int&) at /home/runner/work/firmware/firmware/src/mesh/NodeDB.cpp:664
0x400dc1ff: Observable::notifyObservers(meshtastic::GPSStatus const*) at /home/runner/work/firmware/firmware/src/Observer.h:70
0x400dc1ff: GPS::publishUpdate() at /home/runner/work/firmware/firmware/src/gps/GPS.cpp:603
0x400dc3de: GPS::runOnce() at /home/runner/work/firmware/firmware/src/gps/GPS.cpp:708
0x400d97ca: concurrency::OSThread::run() at /home/runner/work/firmware/firmware/src/concurrency/OSThread.cpp:85
0x40108bb1: ThreadController::runOrDelay() at /home/runner/work/firmware/firmware/.pio/libdeps/tbeam/Thread/ThreadController.cpp:59
0x400e4974: loop() at /home/runner/work/firmware/firmware/src/main.cpp:929
0x40144431: loopTask(void*) at /home/runner/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:50

@caveman99
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vk4ypb can you check this? #2964

@vk4ypb
Copy link

@vk4ypb vk4ypb commented on d6fc1c3 Nov 27, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caveman99
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately i can't reproduce that with the current build environment and a T-Beam. can you reset to firmware defaults and try again? take a backup of the config before if its an important device.

@vk4ypb
Copy link

@vk4ypb vk4ypb commented on d6fc1c3 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, tried that to no success. I have looked at your code changes, and it all falls apart in the updateBatteryLevel procedure.

However, I wonder if the issue is not else where, particularly when you set up the battery service. In particular, I have found a reference that states you should be using 128bit UUIDS for the battery service. ie.

0000180F-0000-1000-8000-00805F9B34FB
00002A19-0000-1000-8000-00805F9B34FB

Not sure if this is the issue, I will see if I can modify the code and try.

Peter.

Please sign in to comment.