Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,34 @@ void notifyCB(
BLEdata.set("volt", (float)(((pData[4] * 256) + pData[3]) / 1000.0));
BLEdata.set("batt", (float)(((((pData[4] * 256) + pData[3]) / 1000.0) - 2.1) * 100));

pubBT(BLEdata);
} else if (length == 20) {
// DT24-BLE data format
// https://github.com/NiceLabs/atorch-console/blob/master/docs/protocol-design.md#dc-meter-report
// Data comes as two packets ( 20 and 16 ), and am only processing first
Log.trace(F("Device identified creating BLE buffer" CR));
JsonObject& BLEdata = getBTJsonObject();
String mac_adress = pBLERemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString().c_str();
mac_adress.toUpperCase();
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
if (p->sensorModel == LYWSD03MMC)
BLEdata.set("model", "LYWSD03MMC");
else if (p->sensorModel == MHO_C401)
BLEdata.set("model", "MHO_C401");
else if (p->sensorModel == DT24)
BLEdata.set("model", "DT24");
}
}
BLEdata.set("id", (char*)mac_adress.c_str());
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_adress.c_str());
BLEdata.set("volt", (float)(((pData[4] * 256 * 256) + (pData[5] * 256) + pData[6]) / 10.0));
BLEdata.set("amp", (float)(((pData[7] * 256 * 256) + (pData[8] * 256) + pData[9]) / 1000.0));
BLEdata.set("watt", (float)(((pData[10] * 256 * 256) + (pData[11] * 256) + pData[12]) / 10.0));
BLEdata.set("watt hour", (float)(((pData[13] * 256 * 256 * 256) + (pData[14] * 256 * 256) + (pData[15] * 256) + pData[16]) / 100.0));
BLEdata.set("price", (float)(((pData[17] * 256 * 256) + (pData[18] * 256) + pData[19]) / 100.0));

pubBT(BLEdata);
} else {
Log.notice(F("Device not identified" CR));
Expand Down Expand Up @@ -772,6 +800,40 @@ void BLEconnect() {
}
}
NimBLEDevice::deleteClient(pClient);
} else if (p->sensorModel == DT24) {
Log.trace(F("Model to connect found: %s" CR), p->macAdr);
NimBLEClient* pClient;
pClient = BLEDevice::createClient();
BLEUUID serviceUUID("ffe0");
BLEUUID charUUID("ffe1");
BLEAddress sensorAddress(p->macAdr);
if (!pClient->connect(sensorAddress)) {
Log.notice(F("Failed to find client: %s" CR), p->macAdr);
} else {
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (!pRemoteService) {
Log.notice(F("Failed to find service UUID: %s" CR), serviceUUID.toString().c_str());
} else {
Log.trace(F("Found service: %s" CR), serviceUUID.toString().c_str());
// Obtain a reference to the characteristic in the service of the remote BLE server.
if (pClient->isConnected()) {
Log.trace(F("Client isConnected, freeHeap: %d" CR), ESP.getFreeHeap());
BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (!pRemoteCharacteristic) {
Log.notice(F("Failed to find characteristic UUID: %s" CR), charUUID.toString().c_str());
} else {
if (pRemoteCharacteristic->canNotify()) {
Log.trace(F("Registering notification" CR));
pRemoteCharacteristic->subscribe(true, notifyCB);
delay(BLE_CNCT_TIMEOUT);
} else {
Log.notice(F("Failed registering notification" CR));
}
}
}
}
}
NimBLEDevice::deleteClient(pClient);
}
}
Log.notice(F("BLE Connect end" CR));
Expand Down Expand Up @@ -1310,6 +1372,14 @@ JsonObject& process_bledata(JsonObject& BLEdata) {
createOrUpdateDevice(mac, device_flags_init, IBT4XS);
return process_inkbird_4xs(BLEdata);
}
Log.trace(F("Is it a DT24?" CR));
if (strcmp(name, "DT24-BLE") == 0) {
Log.trace(F("DT24 data reading data reading" CR));
BLEdata.set("model", "DT24");
if (device->sensorModel == -1)
createOrUpdateDevice(mac, device_flags_init, DT24);
return BLEdata;
}
}
Log.trace(F("Is it a iNode Energy Meter?" CR));
if (strlen(manufacturerdata) == 26 && ((long)value_from_hex_data(manufacturerdata, 0, 4, true) & 0xFFF9) == 0x8290) {
Expand Down
1 change: 1 addition & 0 deletions main/config_BT.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ enum ble_sensor_model {
WS02,
IBSTH2,
IBT4XS,
DT24,
};

/*-------------------PIN DEFINITIONS----------------------*/
Expand Down