Components and supplies
1
ESP32 WROOM DA Module
Project description
Code
Bluetooth Control of LED
arduino
See comments in code
1// This example code is in the Public Domain (or CC0 licensed, at your option.) 2// By Evandro Copercini - 2018 3// 4// This example creates a bridge between Serial and Classical Bluetooth (SPP) 5// and also demonstrate that SerialBT have the same functionalities of a normal Serial 6// 7// The code displays the received data and controls the on board LED 8// 9// Connect your device to ESP32test 10// Use Serial Bluetooth Terminal app. 11 12// this library forms part of the ESP32 Boards Manager file 13#include "BluetoothSerial.h" 14 15#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) 16#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it 17#endif 18 19#if !defined(CONFIG_BT_SPP_ENABLED) 20#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip. 21#endif 22 23// set variables and constants 24BluetoothSerial SerialBT; // set the Object SerialBT 25String text = ""; // define the text variable to receive BT input 26int LED_BUILTIN = 2; // the pin number of the onboard LED 27bool LED_STATE = false; // set the LED Status to False 28 29// set up a number array called BT. 30// The assigned values are LED_ON = 0, LED_OFF = 1, TOGGLE = 2, UNDEFINED = 3 31enum BT {LED_ON, LED_OFF, TOGGLE, UNDEFINED }; 32int BT = LED_OFF; // set the enum array to 1 33 34void setup() { 35 Serial.begin(115200); // Serial Monitor 36 SerialBT.begin("ESP32test"); // Bluetooth device name 37 Serial.println("The device started, now you can pair it with bluetooth!"); 38 pinMode(LED_BUILTIN, OUTPUT); // set up the onboard LED pin as output 39} 40 41void loop() { 42 // This line sets the output from the PC to the BT device 43 if (Serial.available()) {SerialBT.write(Serial.read()); } 44 45 // This line sets the input from the device to the PC 46 if (SerialBT.available() > 0) { 47 // stripping transfer characters to get the actual text 48 text = SerialBT.readStringUntil('\n'); // get the BT input upto CRLF 49 text.trim(); // remove whitespace 50 51 // set the value of BT enum array dependent on the input text received 52 if (text == "Led On"){ BT = LED_ON; } // set BT to 0 53 else if (text == "Led Off"){ BT = LED_OFF; }// set BT to 1 54 else if (text == "Toggle"){ BT = TOGGLE; } // set BT to 2 55 else { BT = UNDEFINED; } // input not recognised 56 //Serial.println(text); // show the received text in the monitor 57 58 // this switch case statement uses the value of BT to decide which code to run 59 switch (BT) { 60 case 0 : // LED_ON 61 LED_STATE = true; // set the variable to true 62 Serial.println("LED ON"); // print to the Monitor 63 digitalWrite(LED_BUILTIN, LED_STATE); // turn on the LED 64 break; // break out of the switch case statement 65 66 case 1 : // LED_OFF 67 LED_STATE = false; // set the variable to false 68 Serial.println("LED OFF"); // print to the Monitor 69 digitalWrite(LED_BUILTIN, LED_STATE); // turn off the LED 70 break; // break out of the switch case statement 71 72 case 2 : // LED_TOGGLE 73 LED_STATE = !LED_STATE; // set the variable. Toggle the previous state 74 Serial.println("TOGGLE LED"); // print to the Monitor 75 digitalWrite(LED_BUILTIN, LED_STATE); // toggle the LED 76 break; // break out of the switch case statement 77 78 case 3: // UNDEFINED 79 LED_STATE = false; // set the variable to false 80 Serial.println("UNDEFINED INPUT"); // print to the Monitor 81 digitalWrite(LED_BUILTIN, LED_STATE); // turn off the LED 82 break; // break out of the switch case statement 83 84 default : // if BT is not in the correct range then this code is executed 85 Serial.println("Unrecognised input received"); 86 } 87 } 88 // slight 20ms delay 89 delay(20); 90} 91
Downloadable files
No wiring needed
No wiring needed

Comments
Only logged in users can leave comments