Skip to content

Commit

Permalink
Working...now how to solve on a merge....
Browse files Browse the repository at this point in the history
  • Loading branch information
doudar committed Mar 20, 2024
1 parent 571febd commit 5e20e76
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/BLE_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MyCallbacks : public NimBLECharacteristicCallbacks {

class ss2kCustomCharacteristicCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *);
void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
};

class ss2kCustomCharacteristic {
Expand Down
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default_envs = release
[esp32doit]
lib_ldf_mode = chain
lib_compat_mode = strict
platform = espressif32 @ 6.5.0
platform = espressif32 @ 6.0.1
board = esp32doit-devkit-v1
framework = arduino
board_build.partitions = min_spiffs.csv
Expand All @@ -26,10 +26,10 @@ build_flags =
!python build_date_macro.py
-D CONFIG_BT_NIMBLE_MAX_CONNECTIONS=6
-D CONFIG_MDNS_STRICT_MODE=1
-D CORE_DEBUG_LEVEL=1
-D CORE_DEBUG_LEVEL=5
-D ARDUINO_SERIAL_EVENT_TASK_STACK_SIZE=3500
lib_deps =
https://github.com/h2zero/NimBLE-Arduino/archive/refs/tags/1.4.1.zip
https://github.com/h2zero/NimBLE-Arduino/archive/refs/tags/1.4.0.zip
https://github.com/teemuatlut/TMCStepper/archive/refs/tags/v0.7.3.zip
https://github.com/bblanchon/ArduinoJson/archive/refs/tags/v6.20.0.zip
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/archive/refs/tags/V1.3.0.zip
Expand Down
27 changes: 18 additions & 9 deletions src/BLE_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,28 +283,37 @@ bool SpinBLEClient::connectToServer() {
pChr = pSvc->getCharacteristic(charUUID);

if (pChr) { /** make sure it's not null */

if (pChr->canRead()) {
NimBLEAttValue value = pChr->readValue();
const int kLogBufMaxLength = 250;
char logBuf[kLogBufMaxLength];
int logBufLength = ss2k_log_hex_to_buffer(value.data(), value.length(), logBuf, 0, kLogBufMaxLength);
logBufLength += snprintf(logBuf + logBufLength, kLogBufMaxLength - logBufLength, " <-initial Value");
SS2K_LOG(BLE_COMMON_LOG_TAG, "%s", logBuf);
}
if (pChr->canNotify()) {
// if(!pChr->registerForNotify(notifyCB)) {
if (!pChr->subscribe(true, onNotify)) {
/** Disconnect if subscribe failed */
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Notifications Failed for %s", pClient->getPeerAddress().toString().c_str());
spinBLEClient.myBLEDevices[device_number].reset();
pClient->deleteServices();
NimBLEDevice::getScan()->erase(pClient->getPeerAddress());
NimBLEDevice::deleteClient(pClient);
return false;
}
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Notifications Subscribed for %s", pClient->getPeerAddress().toString().c_str());
} else if (pChr->canIndicate()) {
/** Send false as first argument to subscribe to indications instead of notifications */
// if(!pChr->registerForNotify(notifyCB, false)) {
if (!pChr->subscribe(false, onNotify)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Indications Failed for %s", pClient->getPeerAddress().toString().c_str());
/** Disconnect if subscribe failed */
spinBLEClient.myBLEDevices[device_number].reset();
pClient->deleteServices();
NimBLEDevice::getScan()->erase(pClient->getPeerAddress());
NimBLEDevice::deleteClient(pClient);
return false;
}
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Indications Subscribed for %s", pClient->getPeerAddress().toString().c_str());
}
this->reconnectTries = MAX_RECONNECT_TRIES;
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Successful %s subscription.", pChr->getUUID().toString().c_str());
Expand Down Expand Up @@ -556,7 +565,7 @@ void SpinBLEClient::removeDuplicates(NimBLEClient *pClient) {
void SpinBLEClient::resetDevices(NimBLEClient *pClient) {
for (auto &_BLEd : spinBLEClient.myBLEDevices) {
if (pClient->getPeerAddress() == _BLEd.peerAddress) {
SS2K_LOGW(BLE_CLIENT_LOG_TAG, "Reset Client Slot: %d", i);
SS2K_LOGW(BLE_CLIENT_LOG_TAG, "Reset Client: %s", _BLEd.peerAddress.toString().c_str());
_BLEd.reset();
}
}
Expand Down Expand Up @@ -632,13 +641,13 @@ void SpinBLEClient::postConnect() {
return;
}

// Start Training
// If we would like to control an external FTMS trainer. With most spin bikes we would want this off, but it's useful if you want to use the SmartSpin2k as an appliance.
if (userConfig->getFTMSControlPointWrite()) {
writeCharacteristic->writeValue(FitnessMachineControlPointProcedure::RequestControl, 1);
vTaskDelay(BLE_NOTIFY_DELAY / portTICK_PERIOD_MS);
writeCharacteristic->writeValue(FitnessMachineControlPointProcedure::StartOrResume, 1);
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Activated FTMS Training.");
}
writeCharacteristic->writeValue(FitnessMachineControlPointProcedure::StartOrResume, 1);
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Updating Connection Params for: %s", _BLEd.peerAddress.toString().c_str());
BLEDevice::getServer()->updateConnParams(pClient->getConnId(), 120, 120, 2, 1000);
spinBLEClient.handleBattInfo(pClient, true);
Expand Down Expand Up @@ -780,15 +789,15 @@ void SpinBLEClient::keepAliveBLE_HID(NimBLEClient *pClient) {
void SpinBLEClient::checkBLEReconnect() {
if ((String(userConfig->getConnectedHeartMonitor()) != "none") && !(spinBLEClient.connectedHRM)) {
this->doScan = true;
SS2K_LOG(BLE_CLIENT_LOG_TAG,"No HRM Connected");
SS2K_LOG(BLE_CLIENT_LOG_TAG, "No HRM Connected");
}
if ((String(userConfig->getConnectedPowerMeter()) != "none") && !(spinBLEClient.connectedPM)) {
this->doScan = true;
SS2K_LOG(BLE_CLIENT_LOG_TAG,"No PM Connected");
SS2K_LOG(BLE_CLIENT_LOG_TAG, "No PM Connected");
}
if ((String(userConfig->getConnectedRemote()) != "none") && !(spinBLEClient.connectedRemote)) {
this->doScan = true;
SS2K_LOG(BLE_CLIENT_LOG_TAG,"No Rem Connected");
SS2K_LOG(BLE_CLIENT_LOG_TAG, "No Rem Connected");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/BLE_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void updateCyclingPowerMeasurementChar() {
150; // Data(18), Sep(data/2), Arrow(3), CharId(37), Sep(3), CharId(37), Sep(3),Name(8), Prefix(2), CD(10), SEP(1), PW(8), Suffix(2), Nul(1), rounded up
char logBuf[kLogBufCapacity];
const size_t cyclingPowerMeasurementLength = sizeof(cyclingPowerMeasurement) / sizeof(cyclingPowerMeasurement[0]);
logCharacteristic(logBuf, kLogBufCapacity, cyclingPowerMeasurement, cyclingPowerMeasurementLength, FITNESSMACHINESERVICE_UUID, fitnessMachineIndoorBikeData->getUUID(),
logCharacteristic(logBuf, kLogBufCapacity, cyclingPowerMeasurement, cyclingPowerMeasurementLength, CYCLINGPOWERSERVICE_UUID, cyclingPowerMeasurementCharacteristic->getUUID(),
"CPS(CPM)[ CD(%.2f) PW(%d) ]", cadence > 0 ? fmodf(cadence, 1000.0) : 0, power % 10000);
}

Expand Down
2 changes: 1 addition & 1 deletion src/BLE_Setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
void setupBLE() { // Common BLE setup for both client and server
SS2K_LOG(BLE_SETUP_LOG_TAG, "Starting Arduino BLE Client application...");
BLEDevice::init(userConfig->getDeviceName());
BLEDevice::setMTU(515);
BLEDevice::setMTU(515); //-- enabling this is very important for BLE firmware updates.
FTMSWrite = "";
spinBLEClient.start();
startBLEServer();
Expand Down
4 changes: 4 additions & 0 deletions src/Custom_Characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist
ss2kCustomCharacteristic::process(rxValue);
}

void ss2kCustomCharacteristicCallbacks::onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue){
NimBLEDevice::setMTU(515);
}

void ss2kCustomCharacteristic::notify(char _item) {
std::string returnValue = {cc_read, _item};
process(returnValue);
Expand Down
2 changes: 1 addition & 1 deletion src/HTTP_Server_Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void startWifi() {
// This below is to compensate for a bug in platform = espressif32 @ 6.5.0 . Hopefully it's fixed soon.
// The Symptoms are that we cannot connect in AP mode unless .eraseAp is called. Then Station needs to get initialized and dumped again before it will connect. Dumbest thing
// ever.
WiFi.eraseAP();
//WiFi.eraseAP();
_staSetup();
WiFi.disconnect(true, true);
WiFi.mode(WIFI_MODE_NULL);
Expand Down

0 comments on commit 5e20e76

Please sign in to comment.