[IDE] ESP32-S3(N4) IoT 开发板与手机进行数据通信

[复制链接]
62|0
Lily李 发表于 2025-10-4 19:41 | 显示全部楼层 |阅读模式
ESP32-S3(N4) IoT 开发板是一款基于乐鑫ESP32-S3-WROOM-1-N4模组设计的开发,它支持WIFI和蓝牙5.0双模通讯。
file:///C:/Users/LilyL/AppData/Local/Temp/ksohtml12488/wps1.png因此,它要与手机进行通讯是十分方便的。
蓝牙连接与断开处理的函数为:
  1. class MyServerCallbacks: public BLEServerCallbacks {
  2.     void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
  3.       Serial.println("蓝牙已连接");
  4.     deviceConnected = true;
  5.     };

  6.     void onDisconnect(BLEServer* pServer) {  //当蓝牙断开连接时会执行该函数
  7.       Serial.println("蓝牙已断开");
  8.     deviceConnected = false;
  9.       delay(500); // give the bluetooth stack the chance to get things ready
  10.       pServer->startAdvertising(); // restart advertising

  11.     }
  12. };

蓝牙接收数据的函数为:
  1. class MyCallbacks: public BLECharacteristicCallbacks {
  2.     void onWrite(BLECharacteristic *pCharacteristic) {
  3.       String rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue   
  4.     if (rxValue.length() > 0) {
  5.         Serial.println("*********");
  6.         Serial.print("Received Value: ");
  7.         for (int i = 0; i < rxValue.length(); i++){
  8.           Serial.print(rxValue[i]);
  9.         }
  10.         Serial.println();
  11.         Serial.println("*********");
  12.       }
  13.     }
  14. };

蓝牙初始化的函数为:
  1. void BLEBegin(){
  2.   BLEDevice::init(/*BLE名称*/"UART Service");
  3.   pServer = BLEDevice::createServer();
  4.   pServer->setCallbacks(new MyServerCallbacks());
  5.   BLEService *pService = pServer->createService(SERVICE_UUID);
  6.   pTxCharacteristic = pService->createCharacteristic(
  7.                     CHARACTERISTIC_UUID_TX,
  8.                     BLECharacteristic::PROPERTY_NOTIFY
  9.                   );                    
  10.   pTxCharacteristic->addDescriptor(new BLE2902());
  11.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  12.                       CHARACTERISTIC_UUID_RX,
  13.                       BLECharacteristic::PROPERTY_WRITE
  14.                     );
  15.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  16.   pService->start();
  17.   pServer->getAdvertising()->start();
  18.   Serial.println("Waiting a client connection to notify...");
  19. }

实现手机发送数据的主要程序为:
  1. void setup() {
  2.   Serial.begin(115200);
  3.   BLEBegin();  //初始化蓝牙
  4. }

  5. void loop() {
  6.   if (deviceConnected) {  //如果有蓝牙连接,就发送数据
  7.     pTxCharacteristic->setValue("Hello");  //发送字符串
  8.     pTxCharacteristic->notify();
  9.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  10.     pTxCharacteristic->setValue("DFRobot");  //发送字符串
  11.     pTxCharacteristic->notify();
  12.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  13.   }
  14. }

经程序上传,通过手机上的小程序BLE调试助手,即可向外发送数据,其测试结果如图1至图3所示。
8870668e107b9eabe3.png
1  串口接收手机发送内容

4809268e107cb4285c.png
2 串口监视器接收效果

9076468e107dc85925.png
3 手机退出连接

您需要登录后才可以回帖 登录 | 注册

本版积分规则

12

主题

14

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部