Purpose:
“ESP32 uses BT SPP Profile to connect with mobile phones or PCs and transmit strings through BT functions to perform specific functional actions for functional strings.”
#include <BluetoothSerial.h>
BluetoothSerial BT; //宣告藍芽物件,名稱為BT
void setup() {
Serial.begin(115200);
BT.begin("BT_Test"); // BTName為藍芽廣播名稱
}
void loop() {
BT.println("Hello World!"); //傳輸到藍芽裝置
//檢查藍芽內是否有資料
while (BT.available()) {
//讀取藍芽資料
String BTdata=BT.readString();
//顯示在序列視窗
Serial.println(BTdata);
BTprocessCommand(BTdata); // do something with the command
}
}
void BTprocessCommand(String data)
{
if (data == "LED_OFF")
{
Serial.println(F("LED_OFF"));
vTaskSuspend(hled);
}
if (data == "LED_ON")
{
Serial.println(F("LED_ON"));
vTaskResume(hled);
}
}
以上程序為ESP32端的code
手機端最簡單方式可以用現成的APP
Arduino Bluetooth Control
沒有留言:
張貼留言