#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
BluetoothSerial SerialBT;
String receivedMessage;
const String turnON ="1";
const String turnOFF ="0";
const int LEDpin = 13;
#define DHTPIN 27 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
SerialBT.begin("ESP32_LED_Control"); // Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin, HIGH);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
SerialBT.println(F("Failed to read from DHT sensor!"));
return;
}
if (SerialBT.available()) {
receivedMessage = SerialBT.readString();
SerialBT.print("Received: ");
SerialBT.println(receivedMessage);
SerialBT.print(F("Temperature: "));
SerialBT.println(t);
if (receivedMessage == turnON) {
SerialBT.println("LED ON");
digitalWrite(LEDpin, HIGH); // turn the LED ON
}
if (receivedMessage == turnOFF) {
SerialBT.println("LED OFF");
digitalWrite(LEDpin, LOW); // turn the LED off
}
}
delay(20);
}