#include "BluetoothSerial.h"

/*
red led = LIV Light -- Used digitalWrite to provide power
blue led = WC light -- Used digitalWrite to provide power
yellow led = FAN    -- Used 220V and Relay to control
green led = Kitchen light  -- Used 220V and Relay to control
On relay, we used NC - Normal Closed port so LOW signal to stop power, HIGH is provide power
*/

int liv_light = 14;
int wc_light  = 27;
int fan = 26;
int kitchen_light = 25;

String inputString = "";

#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;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("HomeKit_Nhom4"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  pinMode(liv_light, OUTPUT);
  digitalWrite(liv_light, LOW);
  pinMode(wc_light, OUTPUT);
  digitalWrite(wc_light, LOW);
  pinMode(fan, OUTPUT);
  digitalWrite(fan, LOW);
  pinMode(kitchen_light, OUTPUT );
  digitalWrite(kitchen_light, LOW);
}

void loop() {
    if (SerialBT.available()) {
    inputString = SerialBT.readStringUntil('\n');  // Read the input string until newline
    inputString.trim();                            // Remove any leading or trailing whitespace

    // Process command
    if (inputString == "livon") {
      digitalWrite(liv_light, HIGH);
    } else if (inputString == "wcon") {
      digitalWrite(wc_light, HIGH);
    } else if (inputString == "fanon") {
      digitalWrite(fan, HIGH);
    } else if (inputString == "kitchenon") {
      digitalWrite(kitchen_light, HIGH);
    } else if (inputString == "livoff") {
      digitalWrite(liv_light, LOW);
    } else if (inputString == "wcoff") {
      digitalWrite(wc_light, LOW);
    } else if (inputString == "fanoff") {
      digitalWrite(fan, LOW);
    } else if (inputString == "kitchenoff") {
      digitalWrite(kitchen_light, LOW);
    } else {
      Serial.print("Unknown command: ");
      Serial.println(inputString);
    }

    // Clear the input string for the next command
    inputString = "";
  }

    // Additional code for your loop, if needed
}
$abcdeabcde151015202530354045505560fghijfghij
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay2:VCC
relay2:GND
relay2:IN
relay2:NC
relay2:COM
relay2:NO