Skip to content

Commit

Permalink
Update to use latest NimBLE Library
Browse files Browse the repository at this point in the history
  • Loading branch information
thorrak committed Dec 20, 2024
1 parent 6d7b6d2 commit 3149323
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ lib_deps =
; https://github.com/thorrak/ESPAsyncWebServer.git
https://github.com/thorrak/Arduino-Log.git ; // Need this until ArduinoLog merges https://github.com/thijse/Arduino-Log/pull/21
lib_deps_bluetooth =
h2zero/NimBLE-Arduino @ ^1.4.1 ; https://github.com/h2zero/NimBLE-Arduino.git
https://github.com/h2zero/NimBLE-Arduino.git#master ; Need this until this gets merged: https://github.com/h2zero/NimBLE-Arduino/commit/c7ce2892280f5eb9bcfe541ff9765d527472a6da
; h2zero/NimBLE-Arduino @ ^2.1.0 ; https://github.com/h2zero/NimBLE-Arduino.git

; Let's also include some other common build flags for ease-of-use
build_flags =
Expand Down
5 changes: 4 additions & 1 deletion src/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,11 @@ DeviceDefinition DeviceManager::readJsonIntoDeviceDef(const JsonDocument& doc) {
break;
#ifdef HAS_BLUETOOTH
case DEVICE_HARDWARE_BLUETOOTH_INKBIRD:
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>(), 0);
break;
case DEVICE_HARDWARE_BLUETOOTH_TILT:
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>());
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>(), 1);
break;
#endif
#ifdef EXTERN_SENSOR_ACTUATOR_SUPPORT
Expand Down
8 changes: 6 additions & 2 deletions src/DeviceManagerFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ void DeviceConfig::fromJson(JsonDocument json_doc) {
parseBytes(hw.address, json_doc[DeviceDefinitionKeys::address].as<const char *>(), 8);
copyArray(json_doc[DeviceDefinitionKeys::address], hw.address);
#ifdef HAS_BLUETOOTH
} else if(json_doc[DeviceDefinitionKeys::address].is<std::string>() && (deviceHardware == DEVICE_HARDWARE_BLUETOOTH_INKBIRD || deviceHardware == DEVICE_HARDWARE_BLUETOOTH_TILT)) {
hw.btAddress = NimBLEAddress(json_doc[DeviceDefinitionKeys::address].as<std::string>());
} else if(json_doc[DeviceDefinitionKeys::address].is<std::string>() && (deviceHardware == DEVICE_HARDWARE_BLUETOOTH_INKBIRD)) {
// Inkbirds use address type 0 ("public") which (incorrectly!) indicates they bought a MAC block
hw.btAddress = NimBLEAddress(json_doc[DeviceDefinitionKeys::address].as<std::string>(), 0);
} else if(json_doc[DeviceDefinitionKeys::address].is<std::string>() && (deviceHardware == DEVICE_HARDWARE_BLUETOOTH_TILT)) {
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
hw.btAddress = NimBLEAddress(json_doc[DeviceDefinitionKeys::address].as<std::string>(), 1);
#endif
#ifdef EXTERN_SENSOR_ACTUATOR_SUPPORT
} else if(json_doc[DeviceDefinitionKeys::address].is<const char *>() && json_doc[DeviceDefinitionKeys::child_id].is<const char *>() && (deviceHardware == DEVICE_HARDWARE_TPLINK_SWITCH)) {
Expand Down
9 changes: 6 additions & 3 deletions src/EepromStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include "Display.h"

#ifdef HAS_BLUETOOTH
NimBLEAddress NoTiltDevice = NimBLEAddress("00:00:00:00:00:00");
// Tilts have address type 1, so replicating that here (even though this is wrong based on the address)
NimBLEAddress NoTiltDevice = NimBLEAddress("00:00:00:00:00:00", 1);
#endif


Expand Down Expand Up @@ -338,7 +339,8 @@ void ExtendedSettings::loadFromSpiffs() {
if(json_doc[ExtendedSettingsKeys::glycol].is<bool>()) glycol = json_doc[ExtendedSettingsKeys::glycol];
if(json_doc[ExtendedSettingsKeys::largeTFT].is<bool>()) largeTFT = json_doc[ExtendedSettingsKeys::largeTFT];
#ifdef HAS_BLUETOOTH
if(json_doc[ExtendedSettingsKeys::tiltGravSensor].is<std::string>()) tiltGravSensor = NimBLEAddress(json_doc[ExtendedSettingsKeys::tiltGravSensor].as<std::string>());
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
if(json_doc[ExtendedSettingsKeys::tiltGravSensor].is<std::string>()) tiltGravSensor = NimBLEAddress(json_doc[ExtendedSettingsKeys::tiltGravSensor].as<std::string>(), 1);
#endif
}

Expand Down Expand Up @@ -367,7 +369,8 @@ void ExtendedSettings::processSettingKeypair(JsonPair kv) {
}
#ifdef HAS_BLUETOOTH
else if (kv.key() == ExtendedSettingsKeys::tiltGravSensor) {
NimBLEAddress addr = NimBLEAddress(kv.value().as<std::string>());
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
NimBLEAddress addr = NimBLEAddress(kv.value().as<std::string>(), 1);
setTiltGravSensor(addr);
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ uint8_t processExtendedSettingsJson(const JsonDocument& json, bool triggerUpstre
// Tilt Gravity Sensor
if(json[ExtendedSettingsKeys::tiltGravSensor].is<std::string>()) {
// Validate that it's valid
if(extendedSettings.tiltGravSensor != json[ExtendedSettingsKeys::tiltGravSensor].as<std::string>()) {
extendedSettings.setTiltGravSensor(NimBLEAddress(json[ExtendedSettingsKeys::tiltGravSensor].as<std::string>()));
if(extendedSettings.tiltGravSensor != NimBLEAddress(json[ExtendedSettingsKeys::tiltGravSensor].as<std::string>(), 1)) {
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
extendedSettings.setTiltGravSensor(NimBLEAddress(json[ExtendedSettingsKeys::tiltGravSensor].as<std::string>(), 1));
saveSettings = true;
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/wireless/BTScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ std::list<tilt> lTilts;
////////////////////////////


void load_inkbird_from_advert(NimBLEAdvertisedDevice* advertisedDevice);
void load_tilt_from_advert(NimBLEAdvertisedDevice* advertisedDevice);
void load_inkbird_from_advert(const NimBLEAdvertisedDevice* advertisedDevice);
void load_tilt_from_advert(const NimBLEAdvertisedDevice* advertisedDevice);

/** Handles callbacks when advertisments are received */
class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
class ScanCallbacks: public NimBLEScanCallbacks {
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
bt_scanner.last_detected_device_at = esp_timer_get_time();
// Inkbird IBS-TH2 (sps) and Inkbird IBS-TH1 (tps)
if((advertisedDevice->getName().rfind("sps",0) == 0 || advertisedDevice->getName().rfind("tps",0) == 0) && advertisedDevice->getManufacturerData().length() == 9) {
Expand All @@ -55,7 +55,7 @@ class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
};


void load_inkbird_from_advert(NimBLEAdvertisedDevice* advertisedDevice)
void load_inkbird_from_advert(const NimBLEAdvertisedDevice* advertisedDevice)
{
// The advertisement string is the "manufacturer data" part of the following:
// example: f208361300f28b6408
Expand Down Expand Up @@ -95,7 +95,8 @@ void load_inkbird_from_advert(NimBLEAdvertisedDevice* advertisedDevice)
// Since we need to fake one of the MAC addresses and the internal sensor will ALWAYS be connected/reported, what we can do
// is fake the address of the EXTERNAL sensor. This way, the true MAC address will always be present (and always report the
// internal sensor's temperature) and in the case of failure of the probe, the external sensor will just stop reporting.
inkbird *ib_external = bt_scanner.get_or_create_inkbird(NimBLEAddress(advertisedDevice->getAddress() + 0x010000000000));
// NOTE - Technically address type 0 here is wrong, but it matches what Inkbird uses otherwise
inkbird *ib_external = bt_scanner.get_or_create_inkbird(NimBLEAddress(advertisedDevice->getAddress() + 0x010000000000, 0));
ib->update(alt_temp, hum, bat, advertisedDevice->getRSSI());
ib_external->update(temp, hum, bat, advertisedDevice->getRSSI());
} else {
Expand All @@ -105,7 +106,7 @@ void load_inkbird_from_advert(NimBLEAdvertisedDevice* advertisedDevice)
return;
}

void load_tilt_from_advert(NimBLEAdvertisedDevice* advertisedDevice)
void load_tilt_from_advert(const NimBLEAdvertisedDevice* advertisedDevice)
{
// The advertisement string is the "manufacturer data" part of the following:
//Advertised Device: Name: Tilt, Address: 88:c2:55:ac:26:81, manufacturer data: 4c000215a495bb40c5b14b44b5121370f02d74de005004d9c5
Expand Down Expand Up @@ -174,11 +175,11 @@ void btScanner::init()
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
NimBLEScan* pBLEScan = NimBLEDevice::getScan(); // Create/get the scan
// NOTE - The below probably creates a memory leak with deinit (but deinit is never called in our code).
pBLEScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks()); // Initialize the callbacks
pBLEScan->setScanCallbacks(new ScanCallbacks()); // Initialize the callbacks
pBLEScan->setMaxResults(0);
pBLEScan->setActiveScan(true); // Required for some devices (including Inkbird sensors) - active scan actively queries devices for more info following detection.
pBLEScan->setInterval(97); // Select prime numbers to reduce risk of frequency beat pattern with ibeacon advertisement interval
pBLEScan->setWindow(37); // Set to less or equal setInterval value. Leave reasonable gap to allow WiFi some time.
// pBLEScan->setInterval(97); // Select prime numbers to reduce risk of frequency beat pattern with ibeacon advertisement interval
// pBLEScan->setWindow(37); // Set to less or equal setInterval value. Leave reasonable gap to allow WiFi some time.
}


Expand All @@ -188,8 +189,8 @@ bool btScanner::scan()
return false;
if (NimBLEDevice::getScan()->isScanning())
return false;
delay(400);
if (NimBLEDevice::getScan()->start(BLE_SCAN_TIME, nullptr, false))
delay(200);
if (NimBLEDevice::getScan()->start(BLE_SCAN_TIME, false, true))
return true; //Scan successfully started.
return false; //Scan failed to start
}
Expand Down
3 changes: 2 additions & 1 deletion src/wireless/BTScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

#include "Inkbird.h"
#include "Tilt.h"
#include <list>

#define BLE_SCAN_TIME 60 // Seconds to scan (0=continuous scanning)
#define BLE_SCAN_TIME 60*1000 // Milliseconds to scan (0=continuous scanning)
#define SCAN_FAIL_THRESHHOLD (2*60*1000*1000) // If we don't detect anything in 2 minutes, then the scanner failed.

class btScanner
Expand Down

0 comments on commit 3149323

Please sign in to comment.