ESP32 Code: ESP32 mit Bluetooth verbinden, arbeiten und Befehle
schreiben und lesen
Dieses Code-Beispiel zeigt, wie man einen ESP32 Microprozessor
mit einem Smartphone verbinden kann und dann Befehle zum Steuern der Anwendung senden
und den Stauts lesen kann.
ESP32
WROOM32 nodeMCU, Arduino, DevBoard, Serial Bluetooth
Beispiel : Send Start
über die SerialBluetooth Verbindung zum Starten der Anwendung auf ESP32 nodeMCU
Dann startet man auf dem Smartphone die App: Serial Bluetooth Terminal und verbindet die Anwendung des Smartphones mit ESP32
Dann kann man in der Eingabezeile von Bluetooth Serial Terminal
einen Befehl wie "Start" eingeben
Eingabe von Befehlen
Nach dem Senden des Textes: Start
Wird auf dem Serial Monitor von Arduino IDE der
empfangene Text gezeigt.
ESP32 Code : Einfacher Code zum Testen
#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() >------
}
|