#include <Arduino.h>
#include <BluetoothSerial.h>
//#include <WiFi.h>
//#include "esp_coexist.h"
BluetoothSerial ESP_BT; // Bluetooth Serial object
const int RedLED = 2; // Red LED connected to GPIO 2
//const int YellowLED = 18;
//const int GreenLED = 19;
void setup() {
Serial.begin(115200); // Start serial communication for debugging
delay(1000); // Allow time for Serial to initialize
ESP_BT.begin("ESP32 WOKWI LED Control"); // Start Bluetooth with device name
pinMode(RedLED, OUTPUT); // Set Red LED pin as output
//pinMode(YellowLED, OUTPUT);
//pinMode(GreenLED, OUTPUT);
//Serial.println("Setting Bluetooth priority over WiFi...");
// Set Bluetooth priority
/* esp_err_t err = esp_coex_status_bit_set(ESP_COEX_ST_TYPE_BT, 1); // Replace with actual flag
if (err == ESP_OK) {
Serial.println("Bluetooth priority set successfully.");
} else {
Serial.printf("Failed to set Bluetooth priority: %d\n", err);
}
*/
//Serial.println("Bluetooth Initialized. Pair with 'ESP32 LED Control' and send 'Red/Yellow/Green LED ON' or 'Red/Yellow/Green LED OFF'.");
}
void loop() {
if (ESP_BT.available()) { // Check if data is available
String command = ESP_BT.readString(); // Read the command sent via Bluetooth
command.trim(); // Remove any extra spaces or newline characters
Serial.println(command);
if (command == "RedON") {
digitalWrite(RedLED, HIGH); // Turn Red LED on
// Serial.println("Red LED ON");
// ESP_BT.println("Red LED ON");
}
if (command == "RedOFF") {
digitalWrite(RedLED, LOW); // Turn Red LED off
// Serial.println("Red LED OFF");
// ESP_BT.println("Red LED OFF");
}
/*
if (command == "YellowON") {
digitalWrite(YellowLED, HIGH);
//Serial.println("Yellow LED ON");
//ESP_BT.println("Yellow LED ON");
}
if (command == "YellowOFF") {
digitalWrite(YellowLED, LOW);
//Serial.println("YELLOW LED OFF");
//ESP_BT.println("Yellow LED OFF");
}
/* if (command == "GreenON") {
digitalWrite(GreenLED, HIGH);
Serial.println("Green LED ON");
ESP_BT.println("Green LED ON");
}
if (command == "GreenOFF") {
digitalWrite(GreenLED, LOW);
Serial.println("Green LED OFF");
ESP_BT.println("Green LED OFF");
} */
ESP_BT.println("Invalid Command. Use 'Red/Yellow/GreenON' or 'Red/Yellow/GreenOFF'.");
}
delay(20);
}