Skip to content

Commit 42782e6

Browse files
committed
intitial work for testing.
1 parent 0959669 commit 42782e6

File tree

4 files changed

+218
-157
lines changed

4 files changed

+218
-157
lines changed

main/ZgatewayBLEConnect.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef zBLEConnect_h
2+
#define zBLEConnect_h
3+
4+
#ifdef ESP32
5+
# include "ArduinoJson.h"
6+
# include "NimBLEDevice.h"
7+
8+
extern void pubBT(JsonObject& data);
9+
10+
class zBLEConnect {
11+
public:
12+
NimBLEClient* m_pClient;
13+
TaskHandle_t m_taskHandle = nullptr;
14+
zBLEConnect(NimBLEAddress& addr) { m_pClient = NimBLEDevice::createClient(addr); }
15+
virtual ~zBLEConnect() { NimBLEDevice::deleteClient(m_pClient); }
16+
virtual void publishData() {}
17+
virtual NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID& service, const NimBLEUUID& characteristic);
18+
};
19+
20+
class LYWSD03MMC_connect : public zBLEConnect {
21+
void notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify);
22+
23+
public:
24+
LYWSD03MMC_connect(NimBLEAddress& addr) : zBLEConnect(addr) {}
25+
void publishData() override;
26+
};
27+
28+
class DT24_connect : public zBLEConnect {
29+
std::vector<uint8_t> m_data;
30+
void notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify);
31+
32+
public:
33+
DT24_connect(NimBLEAddress& addr) : zBLEConnect(addr) {}
34+
void publishData() override;
35+
};
36+
37+
#endif //ESP32
38+
#endif //zBLEConnect_h

main/ZgatewayBLEConnect.ino

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#ifdef ESP32
2+
# include "User_config.h"
3+
# ifdef ZgatewayBT
4+
# include "ArduinoJson.h"
5+
# include "ArduinoLog.h"
6+
# include "ZgatewayBLEConnect.h"
7+
# include "config_BT.h"
8+
9+
# define convertTemp_CtoF(c) ((c * 1.8) + 32)
10+
11+
extern bool ProcessLock;
12+
extern std::vector<BLEdevice*> devices;
13+
14+
NimBLERemoteCharacteristic* zBLEConnect::getCharacteristic(const NimBLEUUID& service,
15+
const NimBLEUUID& characteristic) {
16+
BLERemoteCharacteristic* pRemoteCharacteristic = nullptr;
17+
if (!m_pClient || !m_pClient->connect()) {
18+
Log.notice(F("Connect to: %s failed" CR), m_pClient->getPeerAddress().toString().c_str());
19+
} else {
20+
BLERemoteService* pRemoteService = m_pClient->getService(service);
21+
if (!pRemoteService) {
22+
Log.notice(F("Failed to find service UUID: %s" CR), service.toString().c_str());
23+
} else {
24+
Log.trace(F("Found service: %s" CR), service.toString().c_str());
25+
Log.trace(F("Client isConnected, freeHeap: %d" CR), ESP.getFreeHeap());
26+
pRemoteCharacteristic = pRemoteService->getCharacteristic(characteristic);
27+
if (!pRemoteCharacteristic) {
28+
Log.notice(F("Failed to find characteristic UUID: %s" CR), characteristic.toString().c_str());
29+
}
30+
}
31+
}
32+
33+
return pRemoteCharacteristic;
34+
}
35+
36+
/*-----------------------LYWSD03MMC && MHO_C401 HANDLING-----------------------*/
37+
void LYWSD03MMC_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
38+
if (!ProcessLock) {
39+
Log.trace(F("Callback from %s characteristic" CR), pChar->getUUID().toString().c_str());
40+
41+
if (length == 5) {
42+
Log.trace(F("Device identified creating BLE buffer" CR));
43+
JsonObject& BLEdata = getBTJsonObject();
44+
String mac_adress = m_pClient->getPeerAddress().toString().c_str();
45+
mac_adress.toUpperCase();
46+
for (std::vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
47+
BLEdevice* p = *it;
48+
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
49+
if (p->sensorModel == LYWSD03MMC)
50+
BLEdata.set("model", "LYWSD03MMC");
51+
else if (p->sensorModel == MHO_C401)
52+
BLEdata.set("model", "MHO_C401");
53+
}
54+
}
55+
BLEdata.set("id", (char*)mac_adress.c_str());
56+
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_adress.c_str());
57+
BLEdata.set("tempc", (float)((pData[0] | (pData[1] << 8)) * 0.01));
58+
BLEdata.set("tempf", (float)(convertTemp_CtoF((pData[0] | (pData[1] << 8)) * 0.01)));
59+
BLEdata.set("hum", (float)(pData[2]));
60+
BLEdata.set("volt", (float)(((pData[4] * 256) + pData[3]) / 1000.0));
61+
BLEdata.set("batt", (float)(((((pData[4] * 256) + pData[3]) / 1000.0) - 2.1) * 100));
62+
63+
pubBT(BLEdata);
64+
} else {
65+
Log.notice(F("Device not identified" CR));
66+
}
67+
} else {
68+
Log.trace(F("Callback process canceled by processLock" CR));
69+
}
70+
xTaskNotifyGive(m_taskHandle);
71+
}
72+
73+
void LYWSD03MMC_connect::publishData() {
74+
NimBLEUUID serviceUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6");
75+
NimBLEUUID charUUID("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6");
76+
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
77+
78+
if (pChar && pChar->canNotify()) {
79+
Log.trace(F("Registering notification" CR));
80+
pChar->subscribe(true, std::bind(&LYWSD03MMC_connect::notifyCB, this,
81+
std::placeholders::_1, std::placeholders::_2,
82+
std::placeholders::_3, std::placeholders::_4));
83+
m_taskHandle = xTaskGetCurrentTaskHandle();
84+
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(BLE_CNCT_TIMEOUT));
85+
} else {
86+
Log.notice(F("Failed registering notification" CR));
87+
}
88+
}
89+
90+
/*-----------------------DT24 HANDLING-----------------------*/
91+
void DT24_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
92+
if (!ProcessLock) {
93+
Log.trace(F("Callback from %s characteristic" CR), pChar->getUUID().toString().c_str());
94+
if (length == 20) {
95+
m_data.assign(pData, pData + length);
96+
return;
97+
} else if (m_data.size() == 20 && length == 16) {
98+
m_data.insert(m_data.end(), pData, pData + length);
99+
100+
// DT24-BLE data format
101+
// https://github.com/NiceLabs/atorch-console/blob/master/docs/protocol-design.md#dc-meter-report
102+
// Data comes as two packets ( 20 and 16 ), and am only processing first
103+
Log.trace(F("Device identified creating BLE buffer" CR));
104+
JsonObject& BLEdata = getBTJsonObject();
105+
String mac_adress = m_pClient->getPeerAddress().toString().c_str();
106+
mac_adress.toUpperCase();
107+
BLEdata.set("model", "DT24");
108+
BLEdata.set("id", (char*)mac_adress.c_str());
109+
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_adress.c_str());
110+
BLEdata.set("volt", (float)(((m_data[4] * 256 * 256) + (m_data[5] * 256) + m_data[6]) / 10.0));
111+
BLEdata.set("current", (float)(((m_data[7] * 256 * 256) + (m_data[8] * 256) + m_data[9]) / 1000.0));
112+
BLEdata.set("power", (float)(((m_data[10] * 256 * 256) + (m_data[11] * 256) + m_data[12]) / 10.0));
113+
BLEdata.set("energy", (float)(((m_data[13] * 256 * 256 * 256) + (m_data[14] * 256 * 256) + (m_data[15] * 256) + m_data[16]) / 100.0));
114+
BLEdata.set("price", (float)(((m_data[17] * 256 * 256) + (m_data[18] * 256) + m_data[19]) / 100.0));
115+
116+
pubBT(BLEdata);
117+
} else {
118+
Log.notice(F("Device not identified" CR));
119+
}
120+
} else {
121+
Log.trace(F("Callback process canceled by processLock" CR));
122+
}
123+
xTaskNotifyGive(m_taskHandle);
124+
}
125+
126+
void DT24_connect::publishData() {
127+
NimBLEUUID serviceUUID("ffe0");
128+
NimBLEUUID charUUID("ffe1");
129+
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
130+
131+
if (pChar && pChar->canNotify()) {
132+
Log.trace(F("Registering notification" CR));
133+
pChar->subscribe(true, std::bind(&DT24_connect::notifyCB, this,
134+
std::placeholders::_1, std::placeholders::_2,
135+
std::placeholders::_3, std::placeholders::_4));
136+
m_taskHandle = xTaskGetCurrentTaskHandle();
137+
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(BLE_CNCT_TIMEOUT));
138+
} else {
139+
Log.notice(F("Failed registering notification" CR));
140+
}
141+
}
142+
# endif //ZgatewayBT
143+
#endif //ESP32

0 commit comments

Comments
 (0)