// Load libraries #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 = 4; // 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() { // 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 =="led_on"){ // led on digitalWrite(ledPin, HIGH); } // Check received message and control output accordingly else if (message =="led_off"){ // led off digitalWrite(ledPin, LOW); } else if (message =="led_blink"){ // led blinking 50 times then led on int i=0; while(i<50){ digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(200); i+=1; } digitalWrite(ledPin, HIGH); } delay(20); }