💥💥💞💞欢迎来到本博客❤️❤️💥💥
本人持续分享更多关于电子通信专业内容以及嵌入式和单片机的知识,如果大家喜欢,别忘点个赞加个关注哦,让我们一起共同进步~
自带蓝牙是ESP32一大特点之一,本文就介绍一下如何快速通过Arduino框架使用ESP32的蓝牙功能
BluetoothSerial.h是ESP32 Arduino中的蓝牙串口库,可以通过蓝牙串口与其他设备进行通信。常用的函数如下:
void begin(String localName = "ESP32");
void end();
int available();
int read();
size_t write(uint8_t byte);
size_t print(const String &s);
size_t print(const char str[]);
size_t print(char c);
size_t print(unsigned char c, int base = DEC);
size_t print(int n, int base = DEC);
size_t print(unsigned int n, int base = DEC);
size_t print(double n, int digits = 2);
size_t println(const String &s);
size_t println(const char str[]);
size_t println(char c);
size_t println(unsigned char c, int base = DEC);
size_t println(int n, int base = DEC);
size_t println(unsigned int n, int base = DEC);
size_t println(double n, int digits = 2);
size_t println(void);
这些函数可以帮助开发者实现蓝牙串口通信的相关操作。同时,Arduino开发环境中也提供了丰富的示例代码,可以帮助开发者快速上手使用蓝牙串口功能。
#include
BluetoothSerial SerialBT;//ESP32 Bluetooth initialization and data transmission//Initialize the Bluetooth modulevoid setup(){ Serial.begin(9600); SerialBT.begin("ESP32"); //Set your ESP32 device name hereSerial.println("Bluetooth is ready"); }
//Sending data
void loop(){
SerialBT.print("Hello World!");
//Send data over Bluetooth
Serial.println("Data sent");
delay(1000);
}
#include
#include
BluetoothSerial SerialBT;//ESP32 Bluetooth initialization and data transmission//Initialize the Bluetooth module
String BTData;void setup(){
pinMode(2,OUTPUT); // 对应led的管脚Serial.begin(9600); SerialBT.begin("ESP32"); //Set your ESP32 device name hereSerial.println("Bluetooth is ready"); digitalWrite(2,LOW);}
//Sending data
void CallBTData(String Data );
void loop(){ if (SerialBT.available()) {BTData = SerialBT.readStringUntil('\xff');SerialBT.println(BTData);CallBTData(BTData); // 蓝牙事件的处理//Serial.write(SerialBT.read());}}void CallBTData(String Data ){if(Data.substring(0,3) == "OON"){Serial.println("蓝牙事件:打开LED") ;digitalWrite(2,LOW);}else if(Data.substring(0,3) == "OFF"){Serial.println("蓝牙事件:关闭LED");digitalWrite(2,HIGH);}
}
本人持续分享更多关于电子通信专业内容以及嵌入式和单片机的知识,如果大家喜欢,别忘点个赞加个关注哦,让我们一起共同进步~