From c004fe907e59d3c39448657f66902c043b525de9 Mon Sep 17 00:00:00 2001 From: h2zero Date: Wed, 18 Dec 2024 20:20:47 -0700 Subject: [PATCH] Update NimBLE-Arduino to 2.1.1 --- environments.ini | 12 ++++----- main/Zblufi.ino | 2 +- main/ZgatewayBLEConnect.ino | 33 ++++++++++++++--------- main/ZgatewayBT.ino | 54 ++++++++++++++++++------------------- main/config_BT.h | 18 ++++++------- platformio.ini | 2 +- 6 files changed, 64 insertions(+), 57 deletions(-) diff --git a/environments.ini b/environments.ini index cf462a9434..26fdc4ba15 100644 --- a/environments.ini +++ b/environments.ini @@ -126,10 +126,10 @@ build_flags = custom_description = BLE gateway with external antenna and Ethernet/WiFi connectivity, [user guide](https://tbridge02.theengs.io/) custom_hardware = Theengs Bridge gateway ethernet -; DISCLAIMER: This is the default environment for Theengs Plug. +; DISCLAIMER: This is the default environment for Theengs Plug. ; Any modifications to this configuration are not covered by warranty. ; Proceed with caution when making changes. -; +; ; SAFETY WARNING: Do not modify values related to current limits MAX_CURRENT_ACTUATOR or other ; safety-critical parameters. Doing so may lead to fire hazards or damage to the device. [env:theengs-plug] @@ -143,7 +143,7 @@ lib_deps = ${libraries.decoder} ${libraries.adafruit_neopixel} ${libraries.rn8209} -build_flags = +build_flags = ${env.build_flags} ${com-esp32.build_flags} '-DZgatewayBT="BT"' @@ -1811,7 +1811,7 @@ build_flags = '-DCONFIG_ESP_CONSOLE_UART=ESP_CONSOLE_USB_SERIAL_JTAG' [env:esp32c3-m5stamp] -; https://docs.m5stack.com/en/core/stamp_c3 +; https://docs.m5stack.com/en/core/stamp_c3 platform = ${com.esp32_platform} board = esp32-c3-devkitm-1 board_build.partitions = min_spiffs.csv @@ -1834,7 +1834,7 @@ custom_description = BLE gateway on ESP32C3 custom_hardware = M5Stamp C3 [env:esp32c3u-m5stamp] -; https://docs.m5stack.com/en/core/stamp_c3u +; https://docs.m5stack.com/en/core/stamp_c3u platform = ${com.esp32_platform} board = esp32-c3-devkitm-1 board_build.partitions = min_spiffs.csv @@ -1898,8 +1898,8 @@ extra_scripts = ${com-esp32.extra_scripts} board_build.partitions = partitions/min_spiffs.csv lib_deps = ${com-esp32.lib_deps} - ${libraries.ble} ${libraries.decoder} + h2zero/esp-nimble-cpp@2.0.1 build_flags = ${com-esp32.build_flags} '-DZgatewayBT="BT"' diff --git a/main/Zblufi.ino b/main/Zblufi.ino index cfba591a2f..b4d9deb79e 100644 --- a/main/Zblufi.ino +++ b/main/Zblufi.ino @@ -386,7 +386,7 @@ bool startBlufi() { return false; } - if (NimBLEDevice::getInitialized()) { + if (NimBLEDevice::isInitialized()) { NimBLEDevice::deinit(true); delay(50); } diff --git a/main/ZgatewayBLEConnect.ino b/main/ZgatewayBLEConnect.ino index 2f18f20b55..c2b0382f04 100644 --- a/main/ZgatewayBLEConnect.ino +++ b/main/ZgatewayBLEConnect.ino @@ -44,11 +44,12 @@ bool zBLEConnect::writeData(BLEAction* action) { } std::vector 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()); @@ -80,13 +81,15 @@ bool zBLEConnect::processActions(std::vector& actions) { if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) { DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER); JsonObject BLEdata = BLEdataBuffer.to(); - 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)); @@ -94,9 +97,7 @@ bool zBLEConnect::processActions(std::vector& actions) { 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: { @@ -460,7 +461,7 @@ bool SBS1_connect::processActions(std::vector& 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); @@ -497,8 +498,10 @@ bool SBS1_connect::processActions(std::vector& actions) { if (result || it.ttl <= 1) { StaticJsonDocument BLEdataBuffer; JsonObject BLEdata = BLEdataBuffer.to(); - 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); } @@ -592,7 +595,9 @@ bool SBBT_connect::processActions(std::vector& actions) { if (result || it.ttl <= 1) { StaticJsonDocument BLEdataBuffer; JsonObject BLEdata = BLEdataBuffer.to(); - 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) { @@ -694,7 +699,9 @@ bool SBCU_connect::processActions(std::vector& actions) { if (result || it.ttl <= 1) { StaticJsonDocument BLEdataBuffer; JsonObject BLEdata = BLEdataBuffer.to(); - 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); diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 6317482432..0bdc928c79 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -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: @@ -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(); @@ -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(); @@ -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(); @@ -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); @@ -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 && @@ -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 */ @@ -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 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 act_swap; @@ -1387,7 +1386,9 @@ void immediateBTAction(void* pvParameters) { gatewayState = GatewayState::ERROR; StaticJsonDocument BLEdataBuffer; JsonObject BLEdata = BLEdataBuffer.to(); - 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); @@ -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(), BTdata["mac_type"].as()); action.write = true; action.ttl = 3; bool res = false; @@ -1429,7 +1430,7 @@ void KnownBTActions(JsonObject& BTdata) { action.value_type = BLE_VAL_STRING; std::string val = BTdata["cmd"].as(); // Fix #1694 action.value = val; - createOrUpdateDevice(action.addr, device_flags_connect, + createOrUpdateDevice(BTdata["id"].as(), device_flags_connect, TheengsDecoder::BLE_ID_NUM::SBS1, 1); res = true; } @@ -1444,7 +1445,7 @@ void KnownBTActions(JsonObject& BTdata) { if (res) { std::string val = BTdata["tilt"].as(); // Fix #1694 action.value = val; - createOrUpdateDevice(action.addr, device_flags_connect, + createOrUpdateDevice(BTdata["id"].as(), device_flags_connect, TheengsDecoder::BLE_ID_NUM::SBBT, 1); } } else if (BTdata["model_id"] == "W070160X") { @@ -1458,7 +1459,7 @@ void KnownBTActions(JsonObject& BTdata) { if (res) { std::string val = BTdata["position"].as(); // Fix #1694 action.value = val; - createOrUpdateDevice(action.addr, device_flags_connect, + createOrUpdateDevice(BTdata["id"].as(), device_flags_connect, TheengsDecoder::BLE_ID_NUM::SBCU, 1); } } @@ -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() : 0; action.value_type = BLE_VAL_STRING; if (BTdata.containsKey("value_type")) { String vt = BTdata["value_type"]; @@ -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(), BTdata.containsKey("mac_type") ? BTdata["mac_type"].as() : 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(); // Fix #1694 @@ -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(), BTdata.containsKey("mac_type") ? BTdata["mac_type"].as() : 0); action.service = NimBLEUUID((const char*)BTdata["ble_read_service"]); action.characteristic = NimBLEUUID((const char*)BTdata["ble_read_char"]); action.write = false; @@ -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()) { diff --git a/main/config_BT.h b/main/config_BT.h index 9d90ecd5fd..b3126ac2f8 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -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 @@ -34,6 +34,7 @@ extern String stateBTMeasures(bool); #ifdef ESP32 # include "NimBLEDevice.h" + #endif /*-----------BT TOPICS & COMPILATION PARAMETERS-----------*/ @@ -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; diff --git a/platformio.ini b/platformio.ini index 84533a533e..e4853eb5f6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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