电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
文章
Beetle ESP32 C3 蓝牙数据收发
分 享
扫描二维码分享
Beetle ESP32 C3 蓝牙数据收发
esp32
dfrobot
单片机
xinmeng_wit
关注
发布时间: 2022-09-13
丨
阅读: 4748
### 前言 ESP32 C3最大的特色就是wifi和蓝牙,wifi已经在前面几篇文章中进行了试用,今天来试用蓝牙功能。对于蓝牙,最基础的功能就是数据收发。因此,本篇将实现蓝牙数据的收发。 ### 关键点说明 本篇蓝牙的Arduino程序参考了DFRobot官方文档,在这里只对关键点给出自己的理解并做出说明。 1. 要实现的功能 - 开发板通过蓝牙向外发送“Hello,www.icxbk.com”,发送间隔1s; - 开发板蓝牙接收到的消息通过uart打印出来; 2. 需要包含的头文件 ```c #include
#include
#include
#include
``` 3. 事件回调函数 蓝牙的连接和断开都会产生事件,在事件回调函数里指定需要做的事情。同样,蓝牙的数据接收也会产生对应的接收事件,在接收事件回调函数指定数据的处理方式。 代码在连接和断开回调函数中打印当前的连接状态,同时将状态记录下来。 ```c //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发 class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { //当蓝牙连接时会执行该函数 Serial.println("蓝牙已连接"); deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { //当蓝牙断开连接时会执行该函数 Serial.println("蓝牙已断开"); deviceConnected = false; delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising } }; ``` 在接收回调函数中将接收到的数据通过uart串口打印出来。 ```c //蓝牙接收数据处理。当收到数据时自动触发 class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue if (rxValue.length() > 0) { Serial.println("*********"); Serial.print("Received Value: "); for (int i = 0; i < rxValue.length(); i++){ Serial.print(rxValue[i]); } Serial.println(); Serial.println("*********"); } } }; ``` 4. 主循环中进行数据的发送 ```c void loop() { /@@****************数据发送部分*************/ /@@****************************************/ if (deviceConnected) { //如果有蓝牙连接,就发送数据 pTxCharacteristic->setValue("Hello,www.icxbk.com"); //发送字符串 pTxCharacteristic->notify(); delay(1000); // bluetooth stack will go into congestion, if too many packets are sent } /@@****************************************/ /@@****************************************/ } ``` ### 效果演示 手机使用BLE调试助手与开发板蓝牙连接。 连接上以后,uart会显示已连接:  手机上查看消息接收如下:  手机上发送消息,开发板会将消息通过uart打印出来:   ### 完整源代码 ```c /@@* Video: https://www.youtube.com/watch?v=oCMOYS71NIU Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp Ported to Arduino ESP32 by Evandro Copercini Create a BLE server that, once we receive a connection, will send periodic notifications. The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE" Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY" The design of creating the BLE server is: 1. Create a BLE Server 2. Create a BLE Service 3. Create a BLE Characteristic on the Service 4. Create a BLE Descriptor on the characteristic 5. Start the service. 6. Start advertising. */ /@@* 该示例演示了蓝牙数据透传,烧录代码,打开串口监视器,打开手机的BLE调试助手 * 1.即可看见ESP32-C3发送的数据 * 2.通过BLE调试助手的输入框可向ESP32-C3发送数据 */ #include
#include
#include
#include
BLEServer *pServer = NULL; BLECharacteristic * pTxCharacteristic; bool deviceConnected = false; uint8_t txValue = 0; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发 class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { //当蓝牙连接时会执行该函数 Serial.println("蓝牙已连接"); deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { //当蓝牙断开连接时会执行该函数 Serial.println("蓝牙已断开"); deviceConnected = false; delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising } }; /@@****************数据接收部分*************/ /@@****************************************/ //蓝牙接收数据处理。当收到数据时自动触发 class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue if (rxValue.length() > 0) { Serial.println("*********"); Serial.print("Received Value: "); for (int i = 0; i < rxValue.length(); i++){ Serial.print(rxValue[i]); } Serial.println(); Serial.println("*********"); } } }; /@@***************************************/ /@@****************************************/ void setup() { Serial.begin(115200); BLEBegin(); //初始化蓝牙 } void loop() { /@@****************数据发送部分*************/ /@@****************************************/ if (deviceConnected) { //如果有蓝牙连接,就发送数据 pTxCharacteristic->setValue("Hello,www.icxbk.com"); //发送字符串 pTxCharacteristic->notify(); delay(1000); // bluetooth stack will go into congestion, if too many packets are sent } /@@****************************************/ /@@****************************************/ } void BLEBegin(){ // Create the BLE Device BLEDevice::init(/@@*BLE名称*/"UART Service"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pTxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY ); pTxCharacteristic->addDescriptor(new BLE2902()); BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE ); pRxCharacteristic->setCallbacks(new MyCallbacks()); // Start the service pService->start(); // Start advertising pServer->getAdvertising()->start(); Serial.println("Waiting a client connection to notify..."); } ```
原创作品,未经权利人授权禁止转载。详情见
转载须知
。
举报文章
点赞
(
2
)
xinmeng_wit
关注
评论
(0)
登录后可评论,请
登录
或
注册
相关文章推荐
MK-米客方德推出工业级存储卡
Beetle ESP32 C3 wifi联网获取实时天气信息
开箱测评Beetle ESP32-C3 (RISC-V芯片)模块
正点原子数控电源DP100测评
DP100试用评测-----开箱+初体验
Beetle ESP32 C3环境搭建
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏之二
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境之二
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回
我要举报该内容理由
×
广告及垃圾信息
抄袭或未经授权
其它举报理由
请输入您举报的理由(50字以内)
取消
提交