2023年5月7日 星期日

ESP32 Bluetooth SPP Profile Application

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.”

Fundamental:
SPP (Serial Port Profile) is a Classic Bluetooth profile, SPP defines the requirements for Bluetooth devices necessary for setting up emulated serial cable connections using RFCOMM between two peer devices. The requirements are expressed in terms of services provided to applications, and by defining the features and procedures that are required for interoperability between Bluetooth devices.

不論是PC端或是手機端, 只要跟 ESP32的藍芽連接, 就會有一個SPP建構的無線的serial Port.
我們就可以利用serial port的軟體或是APP來跟 ESP32通訊傳輸指令去控制. 
最簡單就是直接控制ESP32板子的LED燈

#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

  }

  delay(500);

}

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



ESP32與手機APP相互溝通接收到的指令
從手機APP傳送控制字元到 ESP32接收作相對應的處理(控制IO做家電控制)
手機APP Demo:
NB端的 Demo:

Youtube





沒有留言:

張貼留言