#include "BluetoothSerial.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;
int received;// o valor recebido será armazenado nesta variável
char receivedChar;// o valor recebido será armazenado como CHAR nesta variável

const char turnON ='a';
const char turnOFF ='b';
const int LEDpin = 2;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32 Richard"); //Bluetooth device name
  Serial.println(" O dispositivo foi iniciado, agora você pode emparelhá-lo com bluetooth!");
  Serial.println(" Para ativar, envie: a");//print on serial monitor  
  Serial.println(" Para desligar, envie: b"); //print on serial monitor 
  pinMode(LEDpin, OUTPUT);
 
}
void loop() {
    receivedChar =(char)SerialBT.read();

  if (Serial.available()) {
    SerialBT.write(Serial.read());
  
  }
  if (SerialBT.available()) {
    
    SerialBT.print("Recebido:");// escreve no aplicativo BT
    SerialBT.println(receivedChar);// escreve no aplicativo BT
    Serial.print ("Recebido:");// escreve no monitor serial
    Serial.println(receivedChar);// escreve no monitor serial
    //SerialBT.println(receivedChar);// escreve no aplicativo BT
    //SerialBT.write(receivedChar); // escreve no monitor serial
    if(receivedChar == turnON)
    {
     SerialBT.println("LED ON:");// escreve no aplicativo BT
     Serial.println("LED ON:");// escreve no aplicativo BT
     digitalWrite(LEDpin, HIGH);// liga o LED
       
    }
    if(receivedChar == turnOFF)
    {
     SerialBT.println("LED OFF:");// escreve no aplicativo BT
     Serial.println("LED OFF:");// escreve no monitor serial
      digitalWrite(LEDpin, LOW);// desliga o LED
    }    
     
  }
  delay(20);
}