Created
May 28, 2023 05:54
-
-
Save yamamaya/b152aa788cd588a42d81d1e308394019 to your computer and use it in GitHub Desktop.
Experimental code to acquire data from SwitchBot Thermometer/Hygrometer by M5Stack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5Stack.h> | |
#include <BLEDevice.h> | |
static BLEUUID SWITCHBOT_ServiceDataUUID( "0000fd3d-0000-1000-8000-00805f9b34fb" ); | |
#define SWITCHBOT_CompanyID 0x0969 | |
#define SWITCHBOT_DeviceType 'T' | |
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { | |
void onResult(BLEAdvertisedDevice advertisedDevice) { | |
Serial.print("BLE Advertised Device found: "); | |
Serial.println(advertisedDevice.toString().c_str()); | |
bool CompanyID_match = false; | |
if ( advertisedDevice.haveManufacturerData() ) { | |
std::string data = advertisedDevice.getManufacturerData(); | |
if ( data.length() >= 2 ) { | |
uint16_t CompanyID = data[0] | ( (uint16_t)data[1] << 8 ); | |
if ( CompanyID == SWITCHBOT_CompanyID ) { | |
CompanyID_match = true; | |
} | |
} | |
} | |
std::string ServiceData; | |
bool ServiceDataUUID_match = false; | |
if ( advertisedDevice.haveServiceData() ) { | |
ServiceData = advertisedDevice.getServiceData(); | |
if ( advertisedDevice.getServiceDataUUID().equals( SWITCHBOT_ServiceDataUUID ) ) { | |
ServiceDataUUID_match = true; | |
} | |
} | |
bool DeviceType_match = false; | |
if ( ServiceDataUUID_match ) { | |
if ( ServiceData[0] == SWITCHBOT_DeviceType ) { | |
DeviceType_match = true; | |
} | |
} | |
if ( CompanyID_match && ServiceDataUUID_match && DeviceType_match ) { | |
uint8_t battery = ServiceData[2] & 0x7f; | |
float temperature = ( ServiceData[4] & 0x7f ) + ( ServiceData[3] & 0x0f ) * 0.1F; | |
if ( ( ServiceData[4] & 0x80 ) == 0 ) { | |
temperature = -temperature; | |
} | |
uint8_t humidity = ServiceData[5] & 0x7f; | |
Serial.println( "Found a meter!" ); | |
BLEDevice::getScan()->stop(); | |
M5.Lcd.println( "Found a meter!" ); | |
M5.Lcd.print( "MAC: " ); | |
M5.Lcd.println( advertisedDevice.getAddress().toString().c_str() ); | |
M5.Lcd.print( "Temp: " ); | |
M5.Lcd.println( temperature ); | |
M5.Lcd.print( "Hum: " ); | |
M5.Lcd.println( humidity ); | |
M5.Lcd.print( "Batt: " ); | |
M5.Lcd.println( battery ); | |
} | |
} | |
}; | |
void setup() { | |
Serial.begin( 115200 ); | |
M5.begin(); | |
M5.Lcd.setTextSize( 2 ); | |
M5.Lcd.println( "BLE scan start." ); | |
BLEDevice::init( "" ); | |
BLEScan* pBLEScan = BLEDevice::getScan(); | |
pBLEScan->setAdvertisedDeviceCallbacks( new MyAdvertisedDeviceCallbacks( )); | |
pBLEScan->setInterval( 510 ); | |
pBLEScan->setWindow( 449 ); | |
pBLEScan->setActiveScan( true ); | |
pBLEScan->start( 5, false ); | |
} | |
void loop() { | |
delay( 1000 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment