不久前,我设法获得了著名的小米温度和湿度传感器。这些传感器之所以受到广泛欢迎,是因为它们的价格相当低廉,使用起来非常方便,而且它们还知道如何通过BLE协议将读数传输到同一Mi Home。另外,整个Internet上到处都是杂乱的选项,可以将这些传感器连接到Home Assistant,MajorDoMo和其他系统。
但是在我看来这还不够,我想以自己的方式做所有事情(不要问我为什么和为什么,我只是想这样做)。即,我想从悬挂在房屋周围的传感器读取数据,并且与它们一起工作有点有趣。因此,我翻遍了电子垃圾箱,在那里找到了ESP32模块。

一个快速的谷歌显示:ESP32是我需要的。它知道如何通过Arduino IDE进行蓝牙和WiFi编程,这将使我能够从传感器获取读数,并在需要时通过WiFi将其发送(至少到家庭服务器,甚至到云)。此外,发现了一个非常快速且简单的教程,仅解决了我的问题。但事实证明,并非一切都那么简单...
第一个问题
, . … , .
, ESP32, . ( , ) . , . , BLE . , , - .
BLE 2- . (discover mode) (connection mode). , Bluetooth . . - . , .
Xiaomi , . . . , .
- ?
. , (advertising ).
void initBluetooth()
{
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(0x50);
pBLEScan->setWindow(0x30);
}
:
void onResult(BLEAdvertisedDevice advertisedDevice)
{
if (advertisedDevice.haveName() && advertisedDevice.haveServiceData() && !advertisedDevice.getName().compare("MJ_HT_V1")) {
std::string strServiceData = advertisedDevice.getServiceData();
uint8_t cServiceData[100];
char charServiceData[100];
strServiceData.copy((char *)cServiceData, strServiceData.length(), 0);
Serial.printf("\n\nAdvertised Device: %s\n", advertisedDevice.toString().c_str());
for (int i=0;i<strServiceData.length();i++) {
sprintf(&charServiceData[i*2], "%02x", cServiceData[i]);
}
std::stringstream ss;
ss << "fe95" << charServiceData;
Serial.print("Payload:");
Serial.println(ss.str().c_str());
char eventLog[256];
unsigned long value, value2;
char charValue[5] = {0,};
switch (cServiceData[11]) {
case 0x04:
sprintf(charValue, "%02X%02X", cServiceData[15], cServiceData[14]);
value = strtol(charValue, 0, 16);
if(METRIC)
{
current_temperature = (float)value/10;
}else
{
current_temperature = CelciusToFahrenheit((float)value/10);
}
displayTemperature();
break;
case 0x06:
sprintf(charValue, "%02X%02X", cServiceData[15], cServiceData[14]);
value = strtol(charValue, 0, 16);
current_humidity = (float)value/10;
displayHumidity();
Serial.printf("HUMIDITY_EVENT: %s, %d\n", charValue, value);
break;
case 0x0A:
sprintf(charValue, "%02X", cServiceData[14]);
value = strtol(charValue, 0, 16);
Serial.printf("BATTERY_EVENT: %s, %d\n", charValue, value);
break;
case 0x0D:
sprintf(charValue, "%02X%02X", cServiceData[15], cServiceData[14]);
value = strtol(charValue, 0, 16);
if(METRIC)
{
current_temperature = (float)value/10;
}else
{
current_temperature = CelciusToFahrenheit((float)value/10);
}
displayTemperature();
Serial.printf("TEMPERATURE_EVENT: %s, %d\n", charValue, value);
sprintf(charValue, "%02X%02X", cServiceData[17], cServiceData[16]);
value2 = strtol(charValue, 0, 16);
current_humidity = (float)value2/10;
displayHumidity();
Serial.printf("HUMIDITY_EVENT: %s, %d\n", charValue, value2);
break;
}
}
}
, - .
switch, 11 service data . , 11 . .
advertising (payload). , , . . payload ( ):
020106121695fe5020aa01ab9f0231342d580a10014309094d4a5f48545f563105030f180a180916ffffc8b33f8a48db
. ( 0x02) . , ( ). . ( ) .
0x16, service data, .. , . 2:
121695fe5020aa01ab9f0231342d580a100143
0916ffffc8b33f8a48db
, , 11 , , switch (0x0A). , , . . , , .
?
- , , , . ESP32 Arduino. , , getServiceData , . .. , payload service data. , . , ( 1.0.4). Arduino IDE ESP32 Boards Manager . getServiceData() service data. , . , .
. . , , . , payload service data ( findServiceData).
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
uint8_t* findServiceData(uint8_t* data, size_t length, uint8_t* foundBlockLength) {
uint8_t* rightBorder = data + length;
while (data < rightBorder) {
uint8_t blockLength = *data;
if (blockLength < 5) {
data += (blockLength+1);
continue;
}
uint8_t blockType = *(data+1);
uint16_t serviceType = *(uint16_t*)(data + 2);
if (blockType == 0x16 && serviceType == 0xfe95) {
*foundBlockLength = blockLength-3;
return data+4;
}
data += (blockLength+1);
}
return nullptr;
}
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (!advertisedDevice.haveName() || advertisedDevice.getName().compare("MJ_HT_V1"))
return;
uint8_t* payload = advertisedDevice.getPayload();
size_t payloadLength = advertisedDevice.getPayloadLength();
Serial.printf("\n\nAdvertised Device: %s\n", advertisedDevice.toString().c_str());
printBuffer(payload, payloadLength);
uint8_t serviceDataLength=0;
uint8_t* serviceData = findServiceData(payload, payloadLength, &serviceDataLength);
if (serviceData == nullptr) {
return;
}
Serial.printf("Found service data len: %d\n", serviceDataLength);
printBuffer(serviceData, serviceDataLength);
switch (serviceData[11])
{
case 0x0D:
{
float temp = *(uint16_t*)(serviceData + 11 + 3) / 10.0;
float humidity = *(uint16_t*)(serviceData + 11 + 5) / 10.0;
Serial.printf("Temp: %f Humidity: %f\n", temp, humidity);
}
break;
case 0x04:
{
float temp = *(uint16_t*)(serviceData + 11 + 3) / 10.0;
Serial.printf("Temp: %f\n", temp);
}
break;
case 0x06:
{
float humidity = *(uint16_t*)(serviceData + 11 + 3) / 10.0;
Serial.printf("Humidity: %f\n", humidity);
}
break;
case 0x0A:
{
int battery = *(serviceData + 11 + 3);
Serial.printf("Battery: %d\n", battery);
}
break;
default:
break;
}
}
};
, . - ESP32 StackOverflow, . , . , -, , , . , .
- , , - . , , , .
.
UPD: