Skip to content

Commit

Permalink
Update NimBLE-Arduino to 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Dec 19, 2024
1 parent 7fa00dd commit 826bc02
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
2 changes: 1 addition & 1 deletion main/Zblufi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ bool startBlufi() {
return false;
}

if (NimBLEDevice::getInitialized()) {
if (NimBLEDevice::isInitialized()) {
NimBLEDevice::deinit(true);
delay(50);
}
Expand Down
33 changes: 20 additions & 13 deletions main/ZgatewayBLEConnect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ bool zBLEConnect::writeData(BLEAction* action) {
}

std::vector<uint8_t> buf;
buf.reserve(len / 2);
for (auto i = 0; i < len; i += 2) {
std::string temp = action->value.substr(i, 2);
buf.push_back((uint8_t)strtoul(temp.c_str(), nullptr, 16));
char sVal[3] = {action->value[i], action->value[i + 1], 0};
buf.push_back((uint8_t)strtoul(sVal, nullptr, 16));
}
return pChar->writeValue((const uint8_t*)&buf[0], buf.size(), !pChar->canWriteNoResponse());
return pChar->writeValue(&buf[0], buf.size(), !pChar->canWriteNoResponse());
}
case BLE_VAL_INT:
return pChar->writeValue(strtol(action->value.c_str(), nullptr, 0), !pChar->canWriteNoResponse());
Expand Down Expand Up @@ -80,23 +81,23 @@ bool zBLEConnect::processActions(std::vector<BLEAction>& actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
BLEdata["id"] = mac_address;
BLEdata["service"] = it.service.toString();
BLEdata["characteristic"] = it.characteristic.toString();

if (it.write) {
Log.trace(F("processing BLE write" CR));
BLEdata["write"] = it.value;
BLEdata["write"] = std::string(it.value);
result = writeData(&it);
} else {
Log.trace(F("processing BLE read" CR));
result = readData(&it);
if (result) {
switch (it.value_type) {
case BLE_VAL_HEX: {
char* pHex = NimBLEUtils::buildHexData(nullptr, (uint8_t*)it.value.c_str(), it.value.length());
BLEdata["read"] = pHex;
free(pHex);
BLEdata["read"] = NimBLEUtils::dataToHexString(it.value.data(), it.value.size());
break;
}
case BLE_VAL_INT: {
Expand Down Expand Up @@ -460,7 +461,7 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
bool result = false;
if (actions.size() > 0) {
for (auto& it : actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
if (it.addr == m_pClient->getPeerAddress()) {
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
NimBLERemoteCharacteristic* pNotifyChar = getCharacteristic(serviceUUID, notifyCharUUID);

Expand Down Expand Up @@ -497,8 +498,10 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["state"] = it.value;
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
BLEdata["id"] = mac_address;
BLEdata["state"] = std::string(it.value);
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
}
Expand Down Expand Up @@ -592,7 +595,9 @@ bool SBBT_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
String mac_address = it.addr.toString().c_str();
mac_address.toUpperCase();
BLEdata["id"] = mac_address;
if (value != -99 || value != -1)
BLEdata["tilt"] = value;
if (value == 50) {
Expand Down Expand Up @@ -694,7 +699,9 @@ bool SBCU_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
String mac_address = it.addr.toString().c_str();
mac_address.toUpperCase();
BLEdata["id"] = mac_address;
if (value != -99 || value != -1)
BLEdata["position"] = value;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
Expand Down
54 changes: 27 additions & 27 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
OpenMQTTGateway - ESP8266 or Arduino program for home automation
Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Send and receiving command by MQTT
This gateway enables to:
Expand Down Expand Up @@ -573,15 +573,15 @@ void XMWSDJ04MMCDiscovery(const char* mac, const char* sensorModel_id) {}
//core on which the BLE detection task will run
static int taskCore = 0;

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice* advertisedDevice) {
BLEAdvertisedDevice* ad = new BLEAdvertisedDevice(*advertisedDevice);
class ScanCallbacks : public NimBLEScanCallbacks {
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
NimBLEAdvertisedDevice* ad = new NimBLEAdvertisedDevice(*advertisedDevice);
if (xQueueSend(BLEQueue, &ad, 0) != pdTRUE) {
Log.error(F("BLEQueue full" CR));
delete (ad);
}
}
};
} scanCallbacks;

std::string convertServiceData(std::string deviceServiceData) {
int serviceDataLength = (int)deviceServiceData.length();
Expand Down Expand Up @@ -640,16 +640,14 @@ void procBLETask(void* pvParameters) {
if (advertisedDevice->haveName())
BLEdata["name"] = (char*)advertisedDevice->getName().c_str();
if (advertisedDevice->haveManufacturerData()) {
char* manufacturerdata = BLEUtils::buildHexData(NULL, (uint8_t*)advertisedDevice->getManufacturerData().data(), advertisedDevice->getManufacturerData().length());
BLEdata["manufacturerdata"] = manufacturerdata;
free(manufacturerdata);
BLEdata["manufacturerdata"] = NimBLEUtils::dataToHexString((uint8_t*)advertisedDevice->getManufacturerData().data(),
advertisedDevice->getManufacturerData().length());
}
if (advertisedDevice->haveRSSI())
BLEdata["rssi"] = (int)advertisedDevice->getRSSI();
BLEdata["rssi"] = (int)advertisedDevice->getRSSI();
if (advertisedDevice->haveTXPower())
BLEdata["txpower"] = (int8_t)advertisedDevice->getTXPower();
if (advertisedDevice->haveRSSI() && BTConfig.presenceEnable) {
hass_presence(BLEdata); // this device has an rssi and with either only sensors or not we can use it for home assistant room presence component
if (BTConfig.presenceEnable) {
hass_presence(BLEdata); // with either only sensors or not we can use it for home assistant room presence component
}
if (advertisedDevice->haveServiceData()) {
int serviceDataCount = advertisedDevice->getServiceDataCount();
Expand Down Expand Up @@ -689,8 +687,7 @@ void BLEscan() {
}
Log.notice(F("Scan begin" CR));
BLEScan* pBLEScan = BLEDevice::getScan();
MyAdvertisedDeviceCallbacks myCallbacks;
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
pBLEScan->setScanCallbacks(&scanCallbacks);
if ((millis() > (timeBetweenActive + BTConfig.intervalActiveScan) || BTConfig.intervalActiveScan == BTConfig.BLEinterval) && !BTConfig.forcePassiveScan) {
pBLEScan->setActiveScan(true);
timeBetweenActive = millis();
Expand All @@ -699,7 +696,7 @@ void BLEscan() {
}
pBLEScan->setInterval(BLEScanInterval);
pBLEScan->setWindow(BLEScanWindow);
BLEScanResults foundDevices = pBLEScan->start(BTConfig.scanDuration / 1000, false);
NimBLEScanResults foundDevices = pBLEScan->getResults(BTConfig.scanDuration, false);
if (foundDevices.getCount())
scanCount++;
Log.notice(F("Found %d devices, scan number %d end" CR), foundDevices.getCount(), scanCount);
Expand Down Expand Up @@ -762,7 +759,7 @@ void BLEconnect() {
for (auto& it : BLEactions) {
if (!it.complete && --it.ttl) {
swap.push_back(it);
} else if (memcmp(it.addr, p->macAdr, sizeof(it.addr)) == 0) {
} else if (it.addr == NimBLEAddress(p->macAdr, p->macType)) {
if (p->sensorModel_id != BLEconectable::id::DT24_BLE &&
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC &&
p->sensorModel_id != BLEconectable::id::LYWSD03MMC &&
Expand Down Expand Up @@ -845,7 +842,9 @@ void coreTask(void* pvParameters) {
}

void setupBTTasksAndBLE() {
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize);
# endif
BLEDevice::init("");
xTaskCreatePinnedToCore(
procBLETask, /* Function to implement the task */
Expand Down Expand Up @@ -1357,7 +1356,7 @@ void immediateBTAction(void* pvParameters) {
if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(QueueSemaphoreTimeOutTask)) == pdTRUE) {
// swap the vectors so only this device is processed
std::vector<BLEdevice*> dev_swap;
dev_swap.push_back(getDeviceByMac(BLEactions.back().addr));
dev_swap.push_back(getDeviceByMac(BLEactions.back().addr.toString().c_str()));
std::swap(devices, dev_swap);

std::vector<BLEAction> act_swap;
Expand Down Expand Up @@ -1387,7 +1386,9 @@ void immediateBTAction(void* pvParameters) {
gatewayState = GatewayState::ERROR;
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = BLEactions.back().addr;
String mac_address = BLEactions.back().addr.toString().c_str();
mac_address.toUpperCase();
BLEdata["id"] = mac_address;
BLEdata["success"] = false;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
Expand Down Expand Up @@ -1419,7 +1420,7 @@ void KnownBTActions(JsonObject& BTdata) {
}
BLEAction action;
memset(&action, 0, sizeof(BLEAction));
strcpy(action.addr, (const char*)BTdata["id"]);
action.addr = NimBLEAddress(BTdata["id"].as<std::string>(), BTdata["mac_type"].as<int>());
action.write = true;
action.ttl = 3;
bool res = false;
Expand All @@ -1429,7 +1430,7 @@ void KnownBTActions(JsonObject& BTdata) {
action.value_type = BLE_VAL_STRING;
std::string val = BTdata["cmd"].as<std::string>(); // Fix #1694
action.value = val;
createOrUpdateDevice(action.addr, device_flags_connect,
createOrUpdateDevice(BTdata["id"].as<const char*>(), device_flags_connect,
TheengsDecoder::BLE_ID_NUM::SBS1, 1);
res = true;
}
Expand All @@ -1444,7 +1445,7 @@ void KnownBTActions(JsonObject& BTdata) {
if (res) {
std::string val = BTdata["tilt"].as<std::string>(); // Fix #1694
action.value = val;
createOrUpdateDevice(action.addr, device_flags_connect,
createOrUpdateDevice(BTdata["id"].as<const char*>(), device_flags_connect,
TheengsDecoder::BLE_ID_NUM::SBBT, 1);
}
} else if (BTdata["model_id"] == "W070160X") {
Expand All @@ -1458,7 +1459,7 @@ void KnownBTActions(JsonObject& BTdata) {
if (res) {
std::string val = BTdata["position"].as<std::string>(); // Fix #1694
action.value = val;
createOrUpdateDevice(action.addr, device_flags_connect,
createOrUpdateDevice(BTdata["id"].as<const char*>(), device_flags_connect,
TheengsDecoder::BLE_ID_NUM::SBCU, 1);
}
}
Expand All @@ -1479,7 +1480,6 @@ void XtoBTAction(JsonObject& BTdata) {
BLEAction action;
memset(&action, 0, sizeof(BLEAction));
action.ttl = BTdata.containsKey("ttl") ? (uint8_t)BTdata["ttl"] : 1;
action.addr_type = BTdata.containsKey("mac_type") ? BTdata["mac_type"].as<int>() : 0;
action.value_type = BLE_VAL_STRING;
if (BTdata.containsKey("value_type")) {
String vt = BTdata["value_type"];
Expand All @@ -1502,7 +1502,7 @@ void XtoBTAction(JsonObject& BTdata) {
BTdata.containsKey("ble_write_service") &&
BTdata.containsKey("ble_write_char") &&
BTdata.containsKey("ble_write_value")) {
strcpy(action.addr, (const char*)BTdata["ble_write_address"]);
action.addr = NimBLEAddress(BTdata["ble_write_address"].as<std::string>(), BTdata.containsKey("mac_type") ? BTdata["mac_type"].as<int>() : 0);
action.service = NimBLEUUID((const char*)BTdata["ble_write_service"]);
action.characteristic = NimBLEUUID((const char*)BTdata["ble_write_char"]);
std::string val = BTdata["ble_write_value"].as<std::string>(); // Fix #1694
Expand All @@ -1512,7 +1512,7 @@ void XtoBTAction(JsonObject& BTdata) {
} else if (BTdata.containsKey("ble_read_address") &&
BTdata.containsKey("ble_read_service") &&
BTdata.containsKey("ble_read_char")) {
strcpy(action.addr, (const char*)BTdata["ble_read_address"]);
action.addr = NimBLEAddress(BTdata["ble_read_address"].as<std::string>(), BTdata.containsKey("mac_type") ? BTdata["mac_type"].as<int>() : 0);
action.service = NimBLEUUID((const char*)BTdata["ble_read_service"]);
action.characteristic = NimBLEUUID((const char*)BTdata["ble_read_char"]);
action.write = false;
Expand All @@ -1521,9 +1521,9 @@ void XtoBTAction(JsonObject& BTdata) {
return;
}

createOrUpdateDevice(action.addr, device_flags_connect,
createOrUpdateDevice(action.addr.toString().c_str(), device_flags_connect,
UNKWNON_MODEL,
action.addr_type);
action.addr.getType());

BLEactions.push_back(action);
if (BTdata.containsKey("immediate") && BTdata["immediate"].as<bool>()) {
Expand Down
18 changes: 9 additions & 9 deletions main/config_BT.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
/*
Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface
Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Send and receiving command by MQTT
This files enables to set your parameter for the bluetooth low energy gateway (beacons detection)
Copyright: (c)Florian ROBERT
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
Expand All @@ -34,6 +34,7 @@ extern String stateBTMeasures(bool);

#ifdef ESP32
# include "NimBLEDevice.h"

#endif

/*-----------BT TOPICS & COMPILATION PARAMETERS-----------*/
Expand Down Expand Up @@ -208,9 +209,8 @@ enum ble_val_type {
};

struct BLEAction {
std::string value;
char addr[18];
int addr_type;
NimBLEAttValue value;
NimBLEAddress addr;
NimBLEUUID service;
NimBLEUUID characteristic;
bool write;
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ arduinolog = https://github.com/1technophile/Arduino-Log.git#f634509
picomqtt = PicoMQTT@1.1.1
rc-switch = https://github.com/1technophile/rc-switch.git#98537e9
newremoteswitch = https://github.com/1technophile/NewRemoteSwitch.git#8eb980e
ble = https://github.com/h2zero/NimBLE-Arduino.git#1.4.1
ble = https://github.com/h2zero/NimBLE-Arduino.git#2.1.1
irremoteesp = IRremoteESP8266@2.8.5
lora = https://github.com/sandeepmistry/arduino-LoRa.git#f4a1d27
esppilight = ESPiLight@0.17.0
Expand Down

0 comments on commit 826bc02

Please sign in to comment.