Skip to content

Commit c94fe1c

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

File tree

4 files changed

+220
-157
lines changed

4 files changed

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

0 commit comments

Comments
 (0)