mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-10 18:24:27 +05:00
refactor: trying to improve connection to BLE devices
This commit is contained in:
@@ -445,6 +445,14 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pClient = NimBLEDevice::createClient();
|
pClient = NimBLEDevice::createClient();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set initial connection parameters:
|
||||||
|
* These settings are safe for 3 clients to connect reliably, can go faster if you have less
|
||||||
|
* connections. Timeout should be a multiple of the interval, minimum is 100ms.
|
||||||
|
* Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 150 * 10ms = 1500ms timeout
|
||||||
|
*/
|
||||||
|
pClient->setConnectionParams(12, 12, 0, 150);
|
||||||
pClient->setConnectTimeout(5000);
|
pClient->setConnectTimeout(5000);
|
||||||
pClient->setSelfDelete(false, true);
|
pClient->setSelfDelete(false, true);
|
||||||
}
|
}
|
||||||
@@ -495,55 +503,59 @@ protected:
|
|||||||
NimBLEUUID charUuid((uint16_t) 0x2A6E);
|
NimBLEUUID charUuid((uint16_t) 0x2A6E);
|
||||||
pChar = pService->getCharacteristic(charUuid);
|
pChar = pService->getCharacteristic(charUuid);
|
||||||
|
|
||||||
if (pChar && pChar->canNotify()) {
|
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
|
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
|
||||||
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
tempNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
pChar->unsubscribe();
|
||||||
if (pChar == nullptr) {
|
tempNotifyCreated = pChar->subscribe(
|
||||||
return;
|
pChar->canNotify(),
|
||||||
}
|
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
||||||
|
if (pChar == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const NimBLERemoteService* pService = pChar->getRemoteService();
|
const NimBLERemoteService* pService = pChar->getRemoteService();
|
||||||
if (pService == nullptr) {
|
if (pService == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NimBLEClient* pClient = pService->getClient();
|
NimBLEClient* pClient = pService->getClient();
|
||||||
if (pClient == nullptr) {
|
if (pClient == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& sSensor = Sensors::settings[sensorId];
|
auto& sSensor = Sensors::settings[sensorId];
|
||||||
|
|
||||||
if (length != 2) {
|
if (length != 2) {
|
||||||
Log.swarningln(
|
Log.swarningln(
|
||||||
|
FPSTR(L_SENSORS_BLE),
|
||||||
|
F("Sensor #%hhu '%s': invalid notification data at temp char (%s) on device %s"),
|
||||||
|
sensorId,
|
||||||
|
sSensor.name,
|
||||||
|
pChar->getUUID().toString().c_str(),
|
||||||
|
pClient->getPeerAddress().toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rawTemp = (pChar->getValue<int16_t>() * 0.01f);
|
||||||
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE),
|
FPSTR(L_SENSORS_BLE),
|
||||||
F("Sensor #%hhu '%s': invalid notification data at temp char (%s) on device %s"),
|
F("Sensor #%hhu '%s': received temp: %.2f"),
|
||||||
sensorId,
|
sensorId, sSensor.name, rawTemp
|
||||||
sSensor.name,
|
|
||||||
pChar->getUUID().toString().c_str(),
|
|
||||||
pClient->getPeerAddress().toString().c_str()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
// set temp
|
||||||
|
Sensors::setValueById(sensorId, rawTemp, Sensors::ValueType::TEMPERATURE, true, true);
|
||||||
|
|
||||||
|
// update rssi
|
||||||
|
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
float rawTemp = (pChar->getValue<int16_t>() * 0.01f);
|
|
||||||
Log.straceln(
|
|
||||||
FPSTR(L_SENSORS_BLE),
|
|
||||||
F("Sensor #%hhu '%s': received temp: %.2f"),
|
|
||||||
sensorId, sSensor.name, rawTemp
|
|
||||||
);
|
|
||||||
|
|
||||||
// set temp
|
|
||||||
Sensors::setValueById(sensorId, rawTemp, Sensors::ValueType::TEMPERATURE, true, true);
|
|
||||||
|
|
||||||
// update rssi
|
|
||||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tempNotifyCreated) {
|
if (tempNotifyCreated) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
@@ -568,55 +580,59 @@ protected:
|
|||||||
NimBLEUUID charUuid((uint16_t) 0x2A1F);
|
NimBLEUUID charUuid((uint16_t) 0x2A1F);
|
||||||
pChar = pService->getCharacteristic(charUuid);
|
pChar = pService->getCharacteristic(charUuid);
|
||||||
|
|
||||||
if (pChar && pChar->canNotify()) {
|
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
|
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
|
||||||
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
tempNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
pChar->unsubscribe();
|
||||||
if (pChar == nullptr) {
|
tempNotifyCreated = pChar->subscribe(
|
||||||
return;
|
pChar->canNotify(),
|
||||||
}
|
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
||||||
|
if (pChar == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const NimBLERemoteService* pService = pChar->getRemoteService();
|
const NimBLERemoteService* pService = pChar->getRemoteService();
|
||||||
if (pService == nullptr) {
|
if (pService == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NimBLEClient* pClient = pService->getClient();
|
NimBLEClient* pClient = pService->getClient();
|
||||||
if (pClient == nullptr) {
|
if (pClient == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& sSensor = Sensors::settings[sensorId];
|
auto& sSensor = Sensors::settings[sensorId];
|
||||||
|
|
||||||
if (length != 2) {
|
if (length != 2) {
|
||||||
Log.swarningln(
|
Log.swarningln(
|
||||||
|
FPSTR(L_SENSORS_BLE),
|
||||||
|
F("Sensor #%hhu '%s': invalid notification data at temp char (%s) on device %s"),
|
||||||
|
sensorId,
|
||||||
|
sSensor.name,
|
||||||
|
pChar->getUUID().toString().c_str(),
|
||||||
|
pClient->getPeerAddress().toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rawTemp = (pChar->getValue<int16_t>() * 0.1f);
|
||||||
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE),
|
FPSTR(L_SENSORS_BLE),
|
||||||
F("Sensor #%hhu '%s': invalid notification data at temp char (%s) on device %s"),
|
F("Sensor #%hhu '%s': received temp: %.2f"),
|
||||||
sensorId,
|
sensorId, sSensor.name, rawTemp
|
||||||
sSensor.name,
|
|
||||||
pChar->getUUID().toString().c_str(),
|
|
||||||
pClient->getPeerAddress().toString().c_str()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
// set temp
|
||||||
|
Sensors::setValueById(sensorId, rawTemp, Sensors::ValueType::TEMPERATURE, true, true);
|
||||||
|
|
||||||
|
// update rssi
|
||||||
|
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
float rawTemp = (pChar->getValue<int16_t>() * 0.1f);
|
|
||||||
Log.straceln(
|
|
||||||
FPSTR(L_SENSORS_BLE),
|
|
||||||
F("Sensor #%hhu '%s': received temp: %.2f"),
|
|
||||||
sensorId, sSensor.name, rawTemp
|
|
||||||
);
|
|
||||||
|
|
||||||
// set temp
|
|
||||||
Sensors::setValueById(sensorId, rawTemp, Sensors::ValueType::TEMPERATURE, true, true);
|
|
||||||
|
|
||||||
// update rssi
|
|
||||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tempNotifyCreated) {
|
if (tempNotifyCreated) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
@@ -653,55 +669,59 @@ protected:
|
|||||||
NimBLEUUID charUuid((uint16_t) 0x2A6F);
|
NimBLEUUID charUuid((uint16_t) 0x2A6F);
|
||||||
pChar = pService->getCharacteristic(charUuid);
|
pChar = pService->getCharacteristic(charUuid);
|
||||||
|
|
||||||
if (pChar && pChar->canNotify()) {
|
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found humidity char (%s) in env service on device %s"),
|
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found humidity char (%s) in env service on device %s"),
|
||||||
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
humidityNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
pChar->unsubscribe();
|
||||||
if (pChar == nullptr) {
|
humidityNotifyCreated = pChar->subscribe(
|
||||||
return;
|
pChar->canNotify(),
|
||||||
}
|
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
||||||
|
if (pChar == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const NimBLERemoteService* pService = pChar->getRemoteService();
|
const NimBLERemoteService* pService = pChar->getRemoteService();
|
||||||
if (pService == nullptr) {
|
if (pService == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NimBLEClient* pClient = pService->getClient();
|
NimBLEClient* pClient = pService->getClient();
|
||||||
if (pClient == nullptr) {
|
if (pClient == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& sSensor = Sensors::settings[sensorId];
|
auto& sSensor = Sensors::settings[sensorId];
|
||||||
|
|
||||||
if (length != 2) {
|
if (length != 2) {
|
||||||
Log.swarningln(
|
Log.swarningln(
|
||||||
|
FPSTR(L_SENSORS_BLE),
|
||||||
|
F("Sensor #%hhu '%s': invalid notification data at humidity char (%s) on device %s"),
|
||||||
|
sensorId,
|
||||||
|
sSensor.name,
|
||||||
|
pChar->getUUID().toString().c_str(),
|
||||||
|
pClient->getPeerAddress().toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rawHumidity = (pChar->getValue<uint16_t>() * 0.01f);
|
||||||
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE),
|
FPSTR(L_SENSORS_BLE),
|
||||||
F("Sensor #%hhu '%s': invalid notification data at humidity char (%s) on device %s"),
|
F("Sensor #%hhu '%s': received humidity: %.2f"),
|
||||||
sensorId,
|
sensorId, sSensor.name, rawHumidity
|
||||||
sSensor.name,
|
|
||||||
pChar->getUUID().toString().c_str(),
|
|
||||||
pClient->getPeerAddress().toString().c_str()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
// set humidity
|
||||||
|
Sensors::setValueById(sensorId, rawHumidity, Sensors::ValueType::HUMIDITY, true, true);
|
||||||
|
|
||||||
|
// update rssi
|
||||||
|
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
float rawHumidity = (pChar->getValue<uint16_t>() * 0.01f);
|
|
||||||
Log.straceln(
|
|
||||||
FPSTR(L_SENSORS_BLE),
|
|
||||||
F("Sensor #%hhu '%s': received humidity: %.2f"),
|
|
||||||
sensorId, sSensor.name, rawHumidity
|
|
||||||
);
|
|
||||||
|
|
||||||
// set humidity
|
|
||||||
Sensors::setValueById(sensorId, rawHumidity, Sensors::ValueType::HUMIDITY, true, true);
|
|
||||||
|
|
||||||
// update rssi
|
|
||||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (humidityNotifyCreated) {
|
if (humidityNotifyCreated) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
@@ -752,55 +772,59 @@ protected:
|
|||||||
NimBLEUUID charUuid((uint16_t) 0x2A19);
|
NimBLEUUID charUuid((uint16_t) 0x2A19);
|
||||||
pChar = pService->getCharacteristic(charUuid);
|
pChar = pService->getCharacteristic(charUuid);
|
||||||
|
|
||||||
if (pChar && pChar->canNotify()) {
|
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found battery char (%s) in battery service on device %s"),
|
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found battery char (%s) in battery service on device %s"),
|
||||||
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
batteryNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
pChar->unsubscribe();
|
||||||
if (pChar == nullptr) {
|
batteryNotifyCreated = pChar->subscribe(
|
||||||
return;
|
pChar->canNotify(),
|
||||||
}
|
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
|
||||||
|
if (pChar == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const NimBLERemoteService* pService = pChar->getRemoteService();
|
const NimBLERemoteService* pService = pChar->getRemoteService();
|
||||||
if (pService == nullptr) {
|
if (pService == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NimBLEClient* pClient = pService->getClient();
|
NimBLEClient* pClient = pService->getClient();
|
||||||
if (pClient == nullptr) {
|
if (pClient == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& sSensor = Sensors::settings[sensorId];
|
auto& sSensor = Sensors::settings[sensorId];
|
||||||
|
|
||||||
if (length != 1) {
|
if (length != 1) {
|
||||||
Log.swarningln(
|
Log.swarningln(
|
||||||
|
FPSTR(L_SENSORS_BLE),
|
||||||
|
F("Sensor #%hhu '%s': invalid notification data at battery char (%s) on device %s"),
|
||||||
|
sensorId,
|
||||||
|
sSensor.name,
|
||||||
|
pChar->getUUID().toString().c_str(),
|
||||||
|
pClient->getPeerAddress().toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto rawBattery = pChar->getValue<uint8_t>();
|
||||||
|
Log.straceln(
|
||||||
FPSTR(L_SENSORS_BLE),
|
FPSTR(L_SENSORS_BLE),
|
||||||
F("Sensor #%hhu '%s': invalid notification data at battery char (%s) on device %s"),
|
F("Sensor #%hhu '%s': received battery: %hhu"),
|
||||||
sensorId,
|
sensorId, sSensor.name, rawBattery
|
||||||
sSensor.name,
|
|
||||||
pChar->getUUID().toString().c_str(),
|
|
||||||
pClient->getPeerAddress().toString().c_str()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
// set battery
|
||||||
|
Sensors::setValueById(sensorId, rawBattery, Sensors::ValueType::BATTERY, true, true);
|
||||||
|
|
||||||
|
// update rssi
|
||||||
|
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
auto rawBattery = pChar->getValue<uint8_t>();
|
|
||||||
Log.straceln(
|
|
||||||
FPSTR(L_SENSORS_BLE),
|
|
||||||
F("Sensor #%hhu '%s': received battery: %.2f"),
|
|
||||||
sensorId, sSensor.name, rawBattery
|
|
||||||
);
|
|
||||||
|
|
||||||
// set battery
|
|
||||||
Sensors::setValueById(sensorId, rawBattery, Sensors::ValueType::BATTERY, true, true);
|
|
||||||
|
|
||||||
// update rssi
|
|
||||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (batteryNotifyCreated) {
|
if (batteryNotifyCreated) {
|
||||||
Log.straceln(
|
Log.straceln(
|
||||||
|
|||||||
Reference in New Issue
Block a user