#include "BluetoothSerial.h"
#include <DHT.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_BLE_Temperature_Humidity");
dht.begin();
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop()
{
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (SerialBT.available()) {
char command = SerialBT.read();
if (command == 'T' || command == 't') { // Yêu cầu nhiệt độ
if (!isnan(temperature)) {
SerialBT.print("Temperature: ");
SerialBT.print(temperature);
SerialBT.println(" °C");
} else {
SerialBT.println("Failed to read temperature!");
}
} else if (command == 'H' || command == 'h') { // Yêu cầu độ ẩm
if (!isnan(humidity)) {
SerialBT.print("Humidity: ");
SerialBT.print(humidity);
SerialBT.println(" %");
} else {
SerialBT.println("Failed to read humidity!");
}
}
}
delay(2000);
}