#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 readAndSendSensorData() {
// Đọc dữ liệu nhiệt độ và độ ẩm từ cảm biến DHT11
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Kiểm tra nếu không đọc được dữ liệu
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Gửi dữ liệu nhiệt độ và độ ẩm qua Bluetooth
SerialBT.print("Temperature: ");
SerialBT.print(temperature);
SerialBT.print("°C, Humidity: ");
SerialBT.print(humidity);
SerialBT.println("%");
}
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() {
// Đọc và gửi dữ liệu từ cảm biến
readAndSendSensorData();
// Chờ trong 2 giây trước khi lặp lại vòng lặp
delay(2000);
}