#include "BLEDevice.h"
#include "DHT.h"
#define UART_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define STRING_BUF_SIZE 100
// The digital pin we're connected to.
#define DHTPIN 8
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
BLEService UartService(UART_SERVICE_UUID);
BLECharacteristic Rx(CHARACTERISTIC_UUID_RX);
BLECharacteristic Tx(CHARACTERISTIC_UUID_TX);
BLEAdvertData advdata;
BLEAdvertData scndata;
bool notify = false;
void writeCB (BLECharacteristic* chr, uint8_t connID) {
printf("Characteristic %s write by connection %d :\n", chr->getUUID().str(), connID);
if (chr->getDataLen() > 0) {
Serial.print("Received string: ");
Serial.print(chr->readString());
Serial.println();
}
}
void notifCB (BLECharacteristic* chr, uint8_t connID, uint16_t cccd) {
if (cccd & GATT_CLIENT_CHAR_CONFIG_NOTIFY) {
printf("Notifications enabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = true;
} else {
printf("Notifications disabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = false;
}
}
void setup() {
Serial.begin(115200);
advdata.addFlags();
advdata.addCompleteName("AMEBA_BLE_DEV");
scndata.addCompleteServices(BLEUUID(UART_SERVICE_UUID));
Rx.setWriteProperty(true);
Rx.setWriteCallback(writeCB);
Rx.setBufferLen(STRING_BUF_SIZE);
Tx.setReadProperty(true);
Tx.setNotifyProperty(true);
Tx.setCCCDCallback(notifCB);
Tx.setBufferLen(STRING_BUF_SIZE);
UartService.addCharacteristic(Rx);
UartService.addCharacteristic(Tx);
BLE.init();
BLE.configAdvert()->setAdvData(advdata);
BLE.configAdvert()->setScanRspData(scndata);
BLE.configServer(1);
BLE.addService(UartService);
BLE.beginPeripheral();
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String msg = ("Humidity: " + String((int) h) + "%\t" + "Temperature: " + String((int) t) + "°C\n");
Tx.writeString(msg);
if (BLE.connected(0) && notify) {
Tx.notify(0);
}
delay(5000);
}