Skip to content

Instantly share code, notes, and snippets.

@ngxson
Created December 8, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngxson/7518f65b9f276dcf6d8c8e6ecd4e8004 to your computer and use it in GitHub Desktop.
Save ngxson/7518f65b9f276dcf6d8c8e6ecd4e8004 to your computer and use it in GitHub Desktop.
ATC_MiThermometer.ino
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; // In seconds
BLEScan * pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveServiceData()) {
char buff[20];
int datalen;
// get advertisement UUID
datalen = (*advertisedDevice.getServiceDataUUID().getNative()).len;
memcpy(buff, &(*advertisedDevice.getServiceDataUUID().getNative()).uuid, datalen);
char tempUUID[] = {
0x1a,
0x18,
0x0
};
// check if the service UUID is 0x1a18 or not
if (strncmp(buff, tempUUID, datalen) == 0) {
// get advertisement data
datalen = advertisedDevice.getServiceData().length();
memcpy(buff, advertisedDevice.getServiceData().c_str(), datalen);
int tempRaw = buff[7] | (buff[6] << 8);
float temperature = tempRaw / 10.0;
int humidity = buff[8];
int battery = buff[9];
String output = "";
output.concat("Temperature: ");
output.concat(temperature);
output.concat("*C - Humidity: ");
output.concat(humidity);
output.concat("% - Battery: ");
output.concat(battery);
output.concat("%");
Serial.println(output);
Serial.printf(">>>> Serrvice Data ");
for (int i = 0; i < datalen; i++) {
Serial.printf("%0x ", advertisedDevice.getServiceData().c_str()[i]);
}
Serial.println();
Serial.println(advertisedDevice.getRSSI());
}
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(false); // active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
pBLEScan->clearResults();
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment