ESP32 Code:
Connect ESP32 to Bluetooth, work and write
and read commands
This code example
shows whatyou can connect an ESP32 microprocessor to a smartphone and then send
commands to control the application and read the jam.
ESP32 WROOM32
nodeMCU, Arduino, DevBoard, Serial Bluetooth
Example : Send
Start via theSerialBluetooth connection to start the application on ESP32
nodeMCU
Then you start
the app on your smartphone: Serial Bluetooth
Terminal and connect the application of the smartphone to ESP32
Then you can
enter a command like "Start" in the input line of Bluetooth Serial
Terminal
Entering commands
After sending the
text: Start
Shows the
received text on the Serial Monitor
of Arduino IDE.
ESP32 Code:
Simple code to test
#include "BluetoothSerial.h"
#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; //Serial Bluetooth send
and receive
String
message = "";
void setup() {
//----< setup() >------
Serial.begin(115200); //*for local
monitor
SerialBT.begin("ESP32_Bluetooth"); //Bluetooth
device name
Serial.println("The device started,
now you can pair it with bluetooth!");
//----</ setup() >------
}
void loop() {
//----< loop() >------
//-< read Bluetooth >-
// get command from smartphone
if (SerialBT.available()) {
char incomingChar
= SerialBT.read();
if
(incomingChar != '\n') {
message += String(incomingChar);
}
else {
message = "";
}
Serial.write(incomingChar);
}
//-</ read Bluetooth >-
//-< do command >-
if (message == "Start") {
Serial.println("Start"); //serial monitor
}
//-</ do command >-
//----</ loop() >------
}
|