#include "BluetoothSerial.h"
#include "DHT.h"
String device_name = "Project FLUX";
#define DHTPIN 4
#define DHTTYPE DHT22
#define LED_PIN 16
#define LED_FAN 2
unsigned long previousMillis = 0;
const long interval = 2000;
// Check if Bluetooth is available
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Check Serial Port Profile
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Port Profile for Bluetooth is not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
SerialBT.begin(device_name); //Bluetooth device name
dht.begin();
pinMode(LED_PIN, OUTPUT);
//SerialBT.deleteAllBondedDevices(); // Uncomment this to delete paired devices; Must be called after begin
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
}
void loop() {
String bluetoothCom;
float temperature;
float humidity;
// int cahaya = lightReader();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
temperature = dhtReaderTemperature();
humidity = dhtReaderHumidity();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
}
// if (mode == true){
// lightControl(cahaya);
// }
// tampilanLCD(temperature, humidity, cahaya);
if (SerialBT.available()) {
bluetoothCom = SerialBT.readStringUntil('\n');
Serial.println(bluetoothCom);
if(bluetoothCom == "LAMP ON"){
digitalWrite(LED_PIN, HIGH);
}
else if (bluetoothCom == "LAMP OFF"){
digitalWrite(LED_PIN, LOW);
}
else if (bluetoothCom == "FAN ON"){
digitalWrite(LED_FAN, HIGH);
}
else if (bluetoothCom == "FAN OFF"){
digitalWrite(LED_FAN, LOW);
}
}
delay(300);
}
float dhtReaderTemperature(){
float suhu = dht.readTemperature(); // Membaca suhu dalam Celcius
return suhu;
}
float dhtReaderHumidity(){
float kelembapan = dht.readHumidity(); // Membaca kelembapan dalam persen
return kelembapan;
}