Skip to content

Instantly share code, notes, and snippets.

@kix
Created March 6, 2020 19:13
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 kix/b6885f603d1cbf154c10ba56cd7fc645 to your computer and use it in GitHub Desktop.
Save kix/b6885f603d1cbf154c10ba56cd7fc645 to your computer and use it in GitHub Desktop.
#include <PMserial.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLE2902.h>
#define SERVICE_UUID "faf6c799-5567-4f54-89fb-c7188b3f3b88"
#define CHARACTERISTIC_PM01_UUID "cc4f2f18-7b99-4fae-b46d-3568a42cb3f8"
#define CHARACTERISTIC_PM25_UUID "d7f6bea5-4e01-458d-9395-9e42ddd00b69"
#define CHARACTERISTIC_PM10_UUID "4e79550a-da13-4cc3-905c-2c4426d305be"
BLECharacteristic *pCharacteristicPm01;
BLECharacteristic *pCharacteristicPm25;
BLECharacteristic *pCharacteristicPm10;
bool deviceConnected = false;
uint8_t pm01 = 0;
uint8_t pm25 = 0;
uint8_t pm10 = 0;
SerialPM pms(PMSx003, Serial1);
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
pms.init(); // config serial port
Serial.print("PM1.0, PM2.5, PM10");
BLEDevice::init("PMSense");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristicPm01 = pService->createCharacteristic(
CHARACTERISTIC_PM01_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_INDICATE | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicPm10 = pService->createCharacteristic(
CHARACTERISTIC_PM10_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_INDICATE | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicPm25 = pService->createCharacteristic(
CHARACTERISTIC_PM25_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_INDICATE | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicPm01->addDescriptor(new BLE2902());
pCharacteristicPm10->addDescriptor(new BLE2902());
pCharacteristicPm25->addDescriptor(new BLE2902());
pService->start();
pServer->getAdvertising()->start();
}
void loop() {
Serial1.begin(9600, SERIAL_8N1, 16, 17);
pms.read();
Serial1.end();
if (deviceConnected) {
pm01 = (uint8_t) pms.pm01;
pCharacteristicPm01->setValue(&pm01, 1);
pCharacteristicPm01->notify();
pm25 = (uint8_t) pms.pm25;
pCharacteristicPm25->setValue(&pm25, 1);
pCharacteristicPm25->notify();
pm10 = (uint8_t) pms.pm10;
pCharacteristicPm10->setValue(&pm10, 1);
pCharacteristicPm10->notify();
}
Serial.print(pms.pm01);Serial.print(F(", "));
Serial.print(pms.pm25);Serial.print(F(", "));
Serial.print(pms.pm10);Serial.print("\n");
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment