// Include the Bluetooth Serial Library
#include "BluetoothSerial.h"
// Check if Bluetooth is properly enabled
#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);
// Define the name that will appear on your Phone's Bluetooth list
SerialBT.begin("ESP32_Lab_Group_X");
Serial.println("The device is started, now you can pair it with bluetooth!");
pinMode(2, OUTPUT); // LED connected to GPIO 2
}
void loop() {
// Check if data is coming from the Smartphone
if (SerialBT.available()) {
char incomingChar = SerialBT.read();
Serial.print("Received: ");
Serial.println(incomingChar);
// Bluetooth Logic
if (incomingChar == '1') {
digitalWrite(2, HIGH);
SerialBT.println("Success: LED turned ON");
}
else if (incomingChar == '0') {
digitalWrite(2, LOW);
SerialBT.println("Success: LED turned OFF");
}
}
delay(20);
}