78浏览
查看: 78|回复: 0

[ESP8266/ESP32] FireBeetle 2 ESP32 C6 蓝牙连接点亮米老鼠灯带

[复制链接]
收到进阶版帮助帖的启发,写完网页控灯帖子(https://mc.dfrobot.com.cn/thread-318980-1-1.html)后,我又想到了用蓝牙的方式来控灯。
先来看下效果(分别是蓝牙发送和接收消息)。




整个测试的过程记录如下:
1、从帮助帖里面找到样例代码,修改提交,提示错误。
样例代码如下:
  1. /*
  2.     Video: https://www.youtube.com/watch?v=oCMOYS71NIU
  3.     Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
  4.     Ported to Arduino ESP32 by Evandro Copercini
  5.    Create a BLE server that, once we receive a connection, will send periodic notifications.
  6.    The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  7.    Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
  8.    Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with  "NOTIFY"
  9.    The design of creating the BLE server is:
  10.    1. Create a BLE Server
  11.    2. Create a BLE Service
  12.    3. Create a BLE Characteristic on the Service
  13.    4. Create a BLE Descriptor on the characteristic
  14.    5. Start the service.
  15.    6. Start advertising.
  16. */
  17. /* 该示例演示了蓝牙数据透传,烧录代码,打开串口监视器,打开手机的BLE调试助手
  18. * 1.即可看见ESP32发送的数据--见APP使用图
  19. * 2.通过BLE调试助手的输入框可向ESP32发送数据--见APP使用图
  20. * 该示例由BLE_uart示例更改而来
  21. */
  22. #include <BLEDevice.h>
  23. #include <BLEServer.h>
  24. #include <BLEUtils.h>
  25. #include <BLE2902.h>
  26. BLEServer *pServer = NULL;
  27. BLECharacteristic * pTxCharacteristic;
  28. bool deviceConnected = false;
  29. uint8_t txValue = 0;
  30. // See the following for generating UUIDs:
  31. // https://www.uuidgenerator.net/
  32. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  33. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  34. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  35. //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发
  36. class MyServerCallbacks: public BLEServerCallbacks {
  37.     void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
  38.       Serial.println("蓝牙已连接");
  39.       deviceConnected = true;
  40.     };
  41.     void onDisconnect(BLEServer* pServer) {  //当蓝牙断开连接时会执行该函数
  42.       Serial.println("蓝牙已断开");
  43.       deviceConnected = false;
  44.       delay(500); // give the bluetooth stack the chance to get things ready
  45.       pServer->startAdvertising(); // restart advertising
  46.     }
  47. };
  48. /****************数据接收部分*************/
  49. /****************************************/
  50. //蓝牙接收数据处理。当收到数据时自动触发
  51. class MyCallbacks: public BLECharacteristicCallbacks {
  52.     void onWrite(BLECharacteristic *pCharacteristic) {
  53.       std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
  54.       //if(rxValue == "ON"){Serial.println("开灯");}   //判断接收的字符是否为"ON"
  55.       if (rxValue.length() > 0) {
  56.         Serial.println("*********");
  57.         Serial.print("Received Value: ");
  58.         for (int i = 0; i < rxValue.length(); i++){
  59.           Serial.print(rxValue[i]);
  60.         }
  61.         Serial.println();
  62.         Serial.println("*********");
  63.       }
  64.     }
  65. };
  66. /***************************************/
  67. /****************************************/
  68. void setup() {
  69.   Serial.begin(115200);
  70.   BLEBegin();  //初始化蓝牙
  71. }
  72. void loop() {
  73. /****************数据发送部分*************/
  74. /****************************************/
  75.   if (deviceConnected) {  //如果有蓝牙连接,就发送数据
  76.     pTxCharacteristic->setValue("Hello");  //发送字符串
  77.     pTxCharacteristic->notify();
  78.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  79.     pTxCharacteristic->setValue("DFRobot");  //发送字符串
  80.     pTxCharacteristic->notify();
  81.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  82.   }
  83. /****************************************/
  84. /****************************************/
  85. }
  86. void BLEBegin(){
  87.   // Create the BLE Device
  88.   BLEDevice::init(/*BLE名称*/"UART Service");
  89.   // Create the BLE Server
  90.   pServer = BLEDevice::createServer();
  91.   pServer->setCallbacks(new MyServerCallbacks());
  92.   // Create the BLE Service
  93.   BLEService *pService = pServer->createService(SERVICE_UUID);
  94.   // Create a BLE Characteristic
  95.   pTxCharacteristic = pService->createCharacteristic(
  96.                     CHARACTERISTIC_UUID_TX,
  97.                     BLECharacteristic::PROPERTY_NOTIFY
  98.                   );
  99.   pTxCharacteristic->addDescriptor(new BLE2902());
  100.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  101.                       CHARACTERISTIC_UUID_RX,
  102.                       BLECharacteristic::PROPERTY_WRITE
  103.                     );
  104.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  105.   // Start the service
  106.   pService->start();
  107.   // Start advertising
  108.   pServer->getAdvertising()->start();
  109.   Serial.println("Waiting a client connection to notify...");
  110. }
复制代码
报错如下:
D:\desktop\C6测评报告\LANYA-BLUETOOTH1\LANYA-BLUETOOTH1.ino: In member function 'virtual void MyCallbacks::onWrite(BLECharacteristic*)':
D:\desktop\C6测评报告\LANYA-BLUETOOTH1\LANYA-BLUETOOTH1.ino:66:54: error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested
exit status 1
Compilation error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested

2、于是修改样例代码,将std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
改成 String rxValue = pCharacteristic->getValue();

调试成功。

3、修改样例代码,增加米奇小灯的控灯程序,如下:
  1. /*
  2.     Video: https://www.youtube.com/watch?v=oCMOYS71NIU
  3.     Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
  4.     Ported to Arduino ESP32 by Evandro Copercini
  5.    Create a BLE server that, once we receive a connection, will send periodic notifications.
  6.    The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  7.    Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
  8.    Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with  "NOTIFY"
  9.    The design of creating the BLE server is:
  10.    1. Create a BLE Server
  11.    2. Create a BLE Service
  12.    3. Create a BLE Characteristic on the Service
  13.    4. Create a BLE Descriptor on the characteristic
  14.    5. Start the service.
  15.    6. Start advertising.
  16. */
  17. /* 该示例演示了蓝牙数据透传,烧录代码,打开串口监视器,打开手机的BLE调试助手
  18. * 1.即可看见ESP32发送的数据--见APP使用图
  19. * 2.通过BLE调试助手的输入框可向ESP32发送数据--见APP使用图
  20. * 该示例由BLE_uart示例更改而来
  21. */
  22. #include <BLEDevice.h>
  23. #include <BLEServer.h>
  24. #include <BLEUtils.h>
  25. #include <BLE2902.h>
  26. #include <Adafruit_NeoPixel.h>
  27. #ifdef __AVR__
  28. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  29. #endif
  30. // Which pin on the Arduino is connected to the NeoPixels?
  31. #define PIN        6 // On Trinket or Gemma, suggest changing this to 1
  32. // How many NeoPixels are attached to the Arduino?
  33. #define NUMPIXELS 15 // Popular NeoPixel ring size
  34. // When setting up the NeoPixel library, we tell it how many pixels,
  35. // and which pin to use to send signals. Note that for older NeoPixel
  36. // strips you might need to change the third parameter -- see the
  37. // strandtest example for more information on possible values.
  38. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  39. BLEServer *pServer = NULL;
  40. BLECharacteristic * pTxCharacteristic;
  41. bool deviceConnected = false;
  42. uint8_t txValue = 0;
  43. int led = 15;
  44. // See the following for generating UUIDs:
  45. // https://www.uuidgenerator.net/
  46. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  47. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  48. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  49. //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发
  50. class MyServerCallbacks: public BLEServerCallbacks {
  51.     void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
  52.       Serial.println("蓝牙已连接");
  53.       deviceConnected = true;
  54.     };
  55.     void onDisconnect(BLEServer* pServer) {  //当蓝牙断开连接时会执行该函数
  56.       Serial.println("蓝牙已断开");
  57.       deviceConnected = false;
  58.       delay(500); // give the bluetooth stack the chance to get things ready
  59.       pServer->startAdvertising(); // restart advertising
  60.     }
  61. };
  62. /****************数据接收部分*************/
  63. /****************************************/
  64. //蓝牙接收数据处理。当收到数据时自动触发
  65. class MyCallbacks: public BLECharacteristicCallbacks {
  66.     void onWrite(BLECharacteristic *pCharacteristic) {
  67.       String rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
  68.       if(rxValue == "ON"){
  69.         Serial.println("OPEN");
  70.         digitalWrite(led,HIGH);
  71.         delay(0);
  72.         for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
  73.             // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
  74.             // Here we're using a moderately bright green color:
  75.             pixels.setPixelColor(i, pixels.Color(0, 150, 0));
  76.             pixels.show();   // Send the updated pixel colors to the hardware.
  77.             delay(0); // Pause before next pass through loop
  78.           }
  79.         }   //判断接收的字符是否为"ON"
  80.       if(rxValue == "OFF"){
  81.         Serial.println("CLOSE");
  82.         digitalWrite(led,LOW);
  83.         pixels.clear();                //得到/OFF时关闭灯
  84.         for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
  85.           // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
  86.           // Here we're using a moderately bright green color:
  87.           pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  88.           pixels.show();   // Send the updated pixel colors to the hardware.
  89.           delay(0); // Pause before next pass through loop
  90.           }
  91.         }   //判断接收的字符是否为"OFF"      
  92.     }
  93. };
  94. /***************************************/
  95. /****************************************/
  96. void setup() {
  97.   Serial.begin(115200);
  98.   BLEBegin();  //初始化蓝牙
  99.   pinMode(led,OUTPUT);
  100.   #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  101.   clock_prescale_set(clock_div_1);
  102.   #endif
  103.   pixels.begin();
  104. }
  105. void loop() {
  106. /****************数据发送部分*************/
  107. /****************************************/
  108.   if (deviceConnected) {  //如果有蓝牙连接,就发送数据
  109.     pTxCharacteristic->setValue("Hello ");  //发送字符串
  110.     pTxCharacteristic->notify();
  111.     delay(1000); // bluetooth stack will go into congestion, if too many packets are sent
  112.     pTxCharacteristic->setValue("Nanjing ");  //发送字符串
  113.     pTxCharacteristic->notify();
  114.     delay(1000); // bluetooth stack will go into congestion, if too many packets are sent
  115.   }
  116. /****************************************/
  117. /****************************************/
  118. }
  119. void BLEBegin(){
  120.   // Create the BLE Device
  121.   BLEDevice::init(/*BLE名称*/"UART Service");
  122.   // Create the BLE Server
  123.   pServer = BLEDevice::createServer();
  124.   pServer->setCallbacks(new MyServerCallbacks());
  125.   // Create the BLE Service
  126.   BLEService *pService = pServer->createService(SERVICE_UUID);
  127.   // Create a BLE Characteristic
  128.   pTxCharacteristic = pService->createCharacteristic(
  129.                     CHARACTERISTIC_UUID_TX,
  130.                     BLECharacteristic::PROPERTY_NOTIFY
  131.                   );
  132.   pTxCharacteristic->addDescriptor(new BLE2902());
  133.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  134.                       CHARACTERISTIC_UUID_RX,
  135.                       BLECharacteristic::PROPERTY_WRITE
  136.                     );
  137.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  138.   // Start the service
  139.   pService->start();
  140.   // Start advertising
  141.   pServer->getAdvertising()->start();
  142.   Serial.println("Waiting a client connection to notify...");
  143. }
复制代码

4、在手机上下载“BLE调试助手”的app,连接蓝牙,查看收发消息情况。
FireBeetle 2 ESP32 C6 蓝牙连接点亮米老鼠灯带图1FireBeetle 2 ESP32 C6 蓝牙连接点亮米老鼠灯带图2



测试成功。

测试后记:
1、本来还想验证一下太阳能板的使用,气象站、温湿度以及espnow等功能,但是时间有限;
2、其实投入的时间还是不少的,但是无奈测试过程中经常报错,遇到困难有时候无从下手,甚至一度搁置。幸亏在群里有众多高手,在群里咨询,加上看帖子之后,终于慢慢找到了头绪;

3、在参考测试了巴法云、EMQX等平台之后,感觉自己的知识结构又在不断完善,很有收获。

最后,再一次感谢论坛给我这次评测的机会。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail