Skip to content

Commit 6bb65ca

Browse files
NorthernMan54Northern Man
andauthored
DT24 bluetooth Voltmeter (#975)
* DT24 bluetooth Voltmeter * Initial checkin of Home Assistant Discovery Co-authored-by: Northern Man <sgracey@Heisenberg.local>
1 parent 9c9ef12 commit 6bb65ca

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

docs/prerequisites/devices.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ OpenMQTTGateway is able to scan all the BLE devices that advertise their data so
4343
| XIAOMI Mi band (1)||steps|
4444
| iNode Energy Meter (1)||power/energy/battery|
4545
| Thermobeacon|WS02|temperature/humidity/volt|
46+
| ATorch Battery Capacity Monitor|DT24|volt/amp/watt|
4647

4748
Exhaustive list [here](https://compatible.openmqttgateway.com/index.php/devices/ble-devices/)
4849

main/ZgatewayBT.ino

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,21 @@ void WS02Discovery(const char* mac, const char* sensorModel) {
550550
createDiscoveryFromList(mac, WS02sensor, WS02parametersCount, "WS02", "SensorBlue", sensorModel);
551551
}
552552

553+
void DT24Discovery(const char* mac, const char* sensorModel) {
554+
# define DT24parametersCount 5
555+
Log.trace(F("DT24Discovery" CR));
556+
const char* DT24sensor[DT24parametersCount][8] = {
557+
{"sensor", "DT24-volt", mac, "power", jsonVolt, "", "", "V"},
558+
{"sensor", "DT24-amp", mac, "power", jsonCurrent, "", "", "A"},
559+
{"sensor", "DT24-watt", mac, "power", jsonPower, "", "", "W"},
560+
{"sensor", "DT24-watt-hour", mac, "power", jsonEnergy, "", "", "kWh"},
561+
{"sensor", "DT24-price", mac, "", jsonMsg, "", "", ""}
562+
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
563+
};
564+
565+
createDiscoveryFromList(mac, DT24sensor, DT24parametersCount, "DT24", "ATorch", sensorModel);
566+
}
567+
553568
# else
554569
void MiFloraDiscovery(const char* mac, const char* sensorModel) {}
555570
void VegTrugDiscovery(const char* mac, const char* sensorModel) {}
@@ -572,6 +587,7 @@ void LYWSD03MMCDiscovery(const char* mac, const char* sensorModel) {}
572587
void MHO_C401Discovery(const char* mac, const char* sensorModel) {}
573588
void INodeEMDiscovery(const char* mac, const char* sensorModel) {}
574589
void WS02Discovery(const char* mac, const char* sensorModel) {}
590+
void DT24Discovery(const char* mac, const char* sensorModel) {}
575591
# endif
576592

577593
# ifdef ESP32
@@ -700,6 +716,8 @@ void notifyCB(
700716
if (!ProcessLock) {
701717
Log.trace(F("Callback from %s characteristic" CR), pBLERemoteCharacteristic->getUUID().toString().c_str());
702718

719+
// This logic needs refactoring and should be changed to a device/model approach based on the sensor macAdr lookup. Growing this beyond 2 devices will get too cumbersome
720+
703721
if (length == 5) {
704722
Log.trace(F("Device identified creating BLE buffer" CR));
705723
JsonObject& BLEdata = getBTJsonObject();
@@ -722,6 +740,34 @@ void notifyCB(
722740
BLEdata.set("volt", (float)(((pData[4] * 256) + pData[3]) / 1000.0));
723741
BLEdata.set("batt", (float)(((((pData[4] * 256) + pData[3]) / 1000.0) - 2.1) * 100));
724742

743+
pubBT(BLEdata);
744+
} else if (length == 20) {
745+
// DT24-BLE data format
746+
// https://github.com/NiceLabs/atorch-console/blob/master/docs/protocol-design.md#dc-meter-report
747+
// Data comes as two packets ( 20 and 16 ), and am only processing first
748+
Log.trace(F("Device identified creating BLE buffer" CR));
749+
JsonObject& BLEdata = getBTJsonObject();
750+
String mac_adress = pBLERemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString().c_str();
751+
mac_adress.toUpperCase();
752+
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
753+
BLEdevice* p = *it;
754+
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
755+
if (p->sensorModel == LYWSD03MMC)
756+
BLEdata.set("model", "LYWSD03MMC");
757+
else if (p->sensorModel == MHO_C401)
758+
BLEdata.set("model", "MHO_C401");
759+
else if (p->sensorModel == DT24)
760+
BLEdata.set("model", "DT24");
761+
}
762+
}
763+
BLEdata.set("id", (char*)mac_adress.c_str());
764+
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_adress.c_str());
765+
BLEdata.set("volt", (float)(((pData[4] * 256 * 256) + (pData[5] * 256) + pData[6]) / 10.0));
766+
BLEdata.set("amp", (float)(((pData[7] * 256 * 256) + (pData[8] * 256) + pData[9]) / 1000.0));
767+
BLEdata.set("watt", (float)(((pData[10] * 256 * 256) + (pData[11] * 256) + pData[12]) / 10.0));
768+
BLEdata.set("watt hour", (float)(((pData[13] * 256 * 256 * 256) + (pData[14] * 256 * 256) + (pData[15] * 256) + pData[16]) / 100.0));
769+
BLEdata.set("price", (float)(((pData[17] * 256 * 256) + (pData[18] * 256) + pData[19]) / 100.0));
770+
725771
pubBT(BLEdata);
726772
} else {
727773
Log.notice(F("Device not identified" CR));
@@ -772,6 +818,40 @@ void BLEconnect() {
772818
}
773819
}
774820
NimBLEDevice::deleteClient(pClient);
821+
} else if (p->sensorModel == DT24) {
822+
Log.trace(F("Model to connect found: %s" CR), p->macAdr);
823+
NimBLEClient* pClient;
824+
pClient = BLEDevice::createClient();
825+
BLEUUID serviceUUID("ffe0");
826+
BLEUUID charUUID("ffe1");
827+
BLEAddress sensorAddress(p->macAdr);
828+
if (!pClient->connect(sensorAddress)) {
829+
Log.notice(F("Failed to find client: %s" CR), p->macAdr);
830+
} else {
831+
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
832+
if (!pRemoteService) {
833+
Log.notice(F("Failed to find service UUID: %s" CR), serviceUUID.toString().c_str());
834+
} else {
835+
Log.trace(F("Found service: %s" CR), serviceUUID.toString().c_str());
836+
// Obtain a reference to the characteristic in the service of the remote BLE server.
837+
if (pClient->isConnected()) {
838+
Log.trace(F("Client isConnected, freeHeap: %d" CR), ESP.getFreeHeap());
839+
BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
840+
if (!pRemoteCharacteristic) {
841+
Log.notice(F("Failed to find characteristic UUID: %s" CR), charUUID.toString().c_str());
842+
} else {
843+
if (pRemoteCharacteristic->canNotify()) {
844+
Log.trace(F("Registering notification" CR));
845+
pRemoteCharacteristic->subscribe(true, notifyCB);
846+
delay(BLE_CNCT_TIMEOUT);
847+
} else {
848+
Log.notice(F("Failed registering notification" CR));
849+
}
850+
}
851+
}
852+
}
853+
}
854+
NimBLEDevice::deleteClient(pClient);
775855
}
776856
}
777857
Log.notice(F("BLE Connect end" CR));
@@ -1075,6 +1155,7 @@ void launchBTDiscovery() {
10751155
if (p->sensorModel == LYWSD03MMC || p->sensorModel == LYWSD03MMC_ATC || p->sensorModel == LYWSD03MMC_PVVX) LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC");
10761156
if (p->sensorModel == MHO_C401) MHO_C401Discovery(macWOdots.c_str(), "MHO_C401");
10771157
if (p->sensorModel == INODE_EM) INodeEMDiscovery(macWOdots.c_str(), "INODE_EM");
1158+
if (p->sensorModel == DT24) DT24Discovery(macWOdots.c_str(), "DT24-BLE");
10781159
p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice
10791160
} else {
10801161
if (!isDiscovered(p)) {
@@ -1310,6 +1391,14 @@ JsonObject& process_bledata(JsonObject& BLEdata) {
13101391
createOrUpdateDevice(mac, device_flags_init, IBT4XS);
13111392
return process_inkbird_4xs(BLEdata);
13121393
}
1394+
Log.trace(F("Is it a DT24?" CR));
1395+
if (strcmp(name, "DT24-BLE") == 0) {
1396+
Log.trace(F("DT24 data reading data reading" CR));
1397+
BLEdata.set("model", "DT24");
1398+
if (device->sensorModel == -1)
1399+
createOrUpdateDevice(mac, device_flags_init, DT24);
1400+
return BLEdata;
1401+
}
13131402
}
13141403
Log.trace(F("Is it a iNode Energy Meter?" CR));
13151404
if (strlen(manufacturerdata) == 26 && ((long)value_from_hex_data(manufacturerdata, 0, 4, true) & 0xFFF9) == 0x8290) {

main/config_BT.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ enum ble_sensor_model {
143143
WS02,
144144
IBSTH2,
145145
IBT4XS,
146+
DT24,
146147
};
147148

148149
/*-------------------PIN DEFINITIONS----------------------*/

0 commit comments

Comments
 (0)