#include "BluetoothSerial.h"
// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Bluetooth Serial object
BluetoothSerial SerialBT;
// GPIO where LED is connected to
const int ledPin = 13;
// Handle received and sent messages
String message = "";
char incomingChar;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32");
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// put your main code here, to run repeatedly:
// Read received messages (LED control command)
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}
// Check received message and control output accordingly
if (message =="1"){
const char* myStr = "1"; // the string(RESPONSE) to be sent to cell phone via bluetooth
//converts the string to uint8_t type
uint8_t ledState = static_cast<uint8_t>(myStr[0]);
digitalWrite(ledPin, HIGH);
SerialBT.write(ledState); //send messages to my phone. 1
}
else if (message =="0"){
const char* myStr = "0"; // the string(RESPONSE) to be sent to cell phone via bluetooth
//converts the string to uint8_t type
uint8_t ledState = static_cast<uint8_t>(myStr[0]);
digitalWrite(ledPin, LOW);
SerialBT.write(ledState); // Sends message to my phone. 0
}
delay(20);
}