SwitchBot 온습도계의 데이터를 M5StickC(ESP32)+Arduino로 취득
이 작은 BLE 온습도계가 BLE로 데이터가 잡히고 전지도 자주 가지고 싸다! 그렇기 때문에 시험에 사 보았습니다.
게다가 지금 아마존에서 판매 중이므로 지금 구입 → → → → htps : // 아 mz 응. 및 / 2Y 후 SYx
M5StickC+Arduino로 데이터를 잡았습니다.
SwitchBotBleReader.ino.c
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
//BLEAddress addr01 = BLEAddress("xx:xx:xx:xx:xx:xx");
//BLEAddress addr02 = BLEAddress("yy:yy:yy:yy:yy:yy");
BLEUUID serviceUUID = BLEUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b");
BLEUUID serviceDataUUID = BLEUUID("00000d00-0000-1000-8000-00805f9b34fb");
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
/*
if(advertisedDevice.getAddress().equals(addr01) || advertisedDevice.getAddress().equals(addr02)){
// OK
}else{
return;
}
printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
*/
if(!advertisedDevice.haveServiceUUID()) return;
if(!advertisedDevice.getServiceUUID().equals(serviceUUID)) return;
printf("SwitchBot Meter!\n");
if(!advertisedDevice.haveServiceData()) return;
printf("ServiceDataUUID=%s\n", advertisedDevice.getServiceDataUUID().toString().c_str());
std::string s = advertisedDevice.getServiceData();
printf("ServiceData len=%d [", s.length());
for(int i=0; i<s.length(); i++){
printf("%02X ", s.c_str()[i]);
}
printf("]\n");
if(!advertisedDevice.getServiceDataUUID().equals(serviceDataUUID)) return;
const char* servicedata = s.c_str();
int battery = servicedata[2] & 0b01111111;
bool isTemperatureAboveFreezing = servicedata[4] & 0b10000000;
float temperature = ( servicedata[3] & 0b00001111 ) / 10.0 + ( servicedata[4] & 0b01111111 );
if(!isTemperatureAboveFreezing){
temperature = -temperature;
}
int humidity = servicedata[5] & 0b01111111;
bool isEncrypted = ( servicedata[0] & 0b10000000 ) >> 7;
bool isDualStateMode = ( servicedata[1] & 0b10000000 ) >> 7;
bool isStatusOff = ( servicedata[1] & 0b01000000 ) >> 6;
bool isTemperatureHighAlert = ( servicedata[3] & 0b10000000 ) >> 7;
bool isTemperatureLowAlert = ( servicedata[3] & 0b01000000 ) >> 6;
bool isHumidityHighAlert = ( servicedata[3] & 0b00100000 ) >> 5;
bool isHumidityLowAlert = ( servicedata[3] & 0b00010000 ) >> 4;
bool isTemperatureUnitF = ( servicedata[5] & 0b10000000 ) >> 7;
printf("----\n");
printf("address: %s\n", advertisedDevice.getAddress().toString().c_str());
printf("battery: %d\n", battery);
printf("temperature: %.1f\n", temperature);
printf("humidity: %d\n", humidity);
printf("\n");
printf("isEncrypted: %d\n", isEncrypted);
printf("isDualStateMode: %d\n", isDualStateMode);
printf("isStatusOff: %d\n", isStatusOff);
printf("isTemperatureHighAlert: %d\n", isTemperatureHighAlert);
printf("isTemperatureLowAlert: %d\n", isTemperatureLowAlert);
printf("isHumidityHighAlert: %d\n", isHumidityHighAlert);
printf("isHumidityLowAlert: %d\n", isHumidityLowAlert);
printf("isTemperatureUnitF: %d\n", isTemperatureUnitF);
printf("----\n");
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), true);
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
// Serial.print("Devices found: ");
// Serial.println(foundDevices.getCount());
// Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
데이터 포맷은 이 기사가 상세합니다.
htps : // 이 m / wa rp che / / ms / 11 e c9 be f21f5b965b 3
참고 트윗
htps : // 라고 해서 r. 이 m/우선 t/s 타츠 s/1272349274173407235
Reference
이 문제에 관하여(SwitchBot 온습도계의 데이터를 M5StickC(ESP32)+Arduino로 취득), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/takeru@github/items/f42381e8482c3bf484e7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)