2813浏览
查看: 2813|回复: 1

[ESP8266/ESP32] 【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离

[复制链接]
本帖最后由 云天 于 2022-6-2 14:29 编辑

【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图7
Beetle ESP32-C3是一款基于ESP32-C3 RISC-V 32位单核处理器芯片的主控板,专为物联网 (IoT) 设备而设计。
本贴将测试Beetle ESP32-C3的蓝牙Bluetooth 5传输距离实际传输距离。
【蓝牙服务】
根据例程,找到蓝牙的服务UUID,和特征UUID(发送和接收)
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"

【APP inventor2】

编写手机APP
1、界面设计
【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图1
2、获取蓝牙地址
【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图2
3、连接蓝牙并监听数据
【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图3


4、接收数据

【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图4

5、发送指令,控制ESP32 C3板载灯

【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图5

【ESP32 C3编程】
使用Arduino IDE编写程序,配置过程参考官方教程。
以下程序,通过修改例程。每隔一秒向APP发送递增数字。
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5. BLEServer *pServer = NULL;
  6. BLECharacteristic * pTxCharacteristic;
  7. bool deviceConnected = false;
  8. int led = 10;
  9. // See the following for generating UUIDs:
  10. // [url]https://www.uuidgenerator.net/[/url]
  11. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  12. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  13. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  14. //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发
  15. class MyServerCallbacks: public BLEServerCallbacks {
  16.     void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
  17.       Serial.println("蓝牙已连接");
  18.       deviceConnected = true;
  19.     };
  20.     void onDisconnect(BLEServer* pServer) {  //当蓝牙断开连接时会执行该函数
  21.       Serial.println("蓝牙已断开");
  22.       deviceConnected = false;
  23.       delay(500); // give the bluetooth stack the chance to get things ready
  24.       pServer->startAdvertising(); // restart advertising
  25.     }
  26. };
  27. /****************数据接收部分*************/
  28. /****************************************/
  29. //蓝牙接收数据处理。当收到数据时自动触发
  30. class MyCallbacks: public BLECharacteristicCallbacks {
  31.     void onWrite(BLECharacteristic *pCharacteristic) {
  32.       std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
  33.       if(rxValue == "ON\r"){digitalWrite(led,HIGH);Serial.println("开灯");}   //判断接收的字符是否为"ON"
  34.       if(rxValue == "OFF\r"){digitalWrite(led,LOW);Serial.println("关灯");}   //判断接收的字符是否为"OFFN"
  35.       if (rxValue.length() ==3) {
  36.         digitalWrite(led,HIGH);Serial.println("开灯");
  37.         }
  38.       if (rxValue.length() ==4) {
  39.         digitalWrite(led,LOW);Serial.println("关灯");
  40.         }
  41.     }      
  42. };
  43. /***************************************/
  44. /****************************************/
  45. uint16_t txValue = 0;
  46. void setup() {
  47.   Serial.begin(115200);
  48.   BLEBegin();  //初始化蓝牙
  49.   pinMode(led,OUTPUT);
  50. }
  51. void loop() {
  52. /****************数据发送部分*************/
  53. /****************************************/
  54.   if (deviceConnected) {  //如果有蓝牙连接,就发送数据
  55.    
  56.     pTxCharacteristic->setValue(txValue);  //发送字符串
  57.     pTxCharacteristic->notify();
  58.     delay(1000); // bluetooth stack will go into congestion, if too many packets are sent
  59.     txValue++;
  60.    
  61.   }
  62. /****************************************/
  63. /****************************************/
  64. }
  65. void BLEBegin(){
  66.   // Create the BLE Device
  67.   BLEDevice::init(/*BLE名称*/"UART Service");
  68.   // Create the BLE Server
  69.   pServer = BLEDevice::createServer();
  70.   pServer->setCallbacks(new MyServerCallbacks());
  71.   // Create the BLE Service
  72.   BLEService *pService = pServer->createService(SERVICE_UUID);
  73.   // Create a BLE Characteristic
  74.   pTxCharacteristic = pService->createCharacteristic(
  75.                     CHARACTERISTIC_UUID_TX,
  76.                     BLECharacteristic::PROPERTY_NOTIFY
  77.                   );
  78.   pTxCharacteristic->addDescriptor(new BLE2902());
  79.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  80.                       CHARACTERISTIC_UUID_RX,
  81.                       BLECharacteristic::PROPERTY_WRITE
  82.                     );
  83.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  84.   // Start the service
  85.   pService->start();
  86.   // Start advertising
  87.   pServer->getAdvertising()->start();
  88.   Serial.println("Waiting a client connection to notify...");
  89. }
复制代码

查看在“\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.3\libraries\BLE\src”,“BLECharacteristic.h“文件。setValue()函数所接收的数据类型。确定上面发送的数据类型。

  1. class BLECharacteristic {
  2. public:
  3.         BLECharacteristic(const char* uuid, uint32_t properties = 0);
  4.         BLECharacteristic(BLEUUID uuid, uint32_t properties = 0);
  5.         virtual ~BLECharacteristic();
  6.         void           addDescriptor(BLEDescriptor* pDescriptor);
  7.         BLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
  8.         BLEDescriptor* getDescriptorByUUID(BLEUUID descriptorUUID);
  9.         BLEUUID        getUUID();
  10.         std::string    getValue();
  11.         uint8_t*       getData();
  12.         size_t         getLength();
  13.         void indicate();
  14.         void notify(bool is_notification = true);
  15.         void setBroadcastProperty(bool value);
  16.         void setCallbacks(BLECharacteristicCallbacks* pCallbacks);
  17.         void setIndicateProperty(bool value);
  18.         void setNotifyProperty(bool value);
  19.         void setReadProperty(bool value);
  20. <font color="#ff0000">        void setValue(uint8_t* data, size_t size);
  21.         void setValue(std::string value);
  22.         void setValue(uint16_t& data16);
  23.         void setValue(uint32_t& data32);
  24.         void setValue(int& data32);
  25.         void setValue(float& data32);
  26.         void setValue(double& data64); </font>
  27.         void setWriteProperty(bool value);
  28.         void setWriteNoResponseProperty(bool value);
  29.         std::string toString();
  30.         uint16_t getHandle();
复制代码
【实际测试】
通过接收信息数据的稳定性来,判断传输距离。因地面一块瓷砖为60CM,在74块砖前较为稳定,之后会出现跳跃。确定传输距离为44.4米。

【Beetle ESP32-C3试用(一)】测试蓝牙Bluetooth 5传输距离图6

【测试视频】




【扩展知识】

1、远距离传输
蓝牙5可以做远距离传输,远距离传输将会无人机,遥控赛车,竞技手柄,工业自动化需要200-500范围内的数据传输提供强有力的技术支持,目前可以支持蓝牙5远距离的芯片有Nordic 52840,TI CC2640R2F, Silconlab EFR32 Blue Gecko,但是可惜的是目前没有一部手机支持远距离

2、数据类型为什么喜欢叫它 int8_t, uint8_t;int16_t, uint16_t;int32_t, uint32_t?
1,数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。
2,8,16,32又是什么意思?
这些数字都是位数,即,使用多少位来存储数据,一个字节8位,其中因为数字有正负之粉,对应有符号(signed,最高位为0,表示正数)和无符号(unsigned,最高位为1,表示负数),所以表示的范围不一样,但是因为位数一致,故表示的个数是一样的。
比如使用16位,也就是两个字节来存储
可以是 typedef signed short int int16_t;//给有符号短整型short int,取别名int16_t
也可以是 typedef unsigned short int uint16_t;//给无符号短整型short int,取别名为uint16_t
int16_t 表示数据范围则是0 ~65535。
uint16_t 表示数据范围为-32768~32767
表示的个数是一样的:65536=2的16次方


整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型的话就需要在类型前加上unsigned。无符号版本和有符号版本的区别就是无符号类型能保存2倍于有符号类型的正整数数据,比如16位系统中一个int能存储的数据的范围为-32768~32767,而unsigned能存储的数据范围则是0 ~65535。由于在计算机中,整数是以补码形式存放的。根据最高位的不同,如果是1,有符号数的话就是负数;如果是无符号数,则都解释为正数。同时在相同位数的情况下,所能表达的整数范围变大。

挖一下这句话
在默认情况下声明的整型变量都是有符号的类型(char有点特别),有什么特别呢

如何确认char到底是signed char还是unsigned char

ANSI C 提供了3种字符类型,分别是char、signed char、unsigned char 、char相当于signed char或者unsigned char,但是这取决于编译器!
这三种字符类型都是按照1个字节存储的,可以保存256个不同的值。
signed char取值范围是 -128 到 127
unsigned char 取值范围是 0 到 255

有符号和无符号有默认的,可以省略不写,默认情况下声明的整型变量都是有符号的类型









小明同学  初级技匠

发表于 2022-6-6 22:54:01

请问app inventor 2的网址是多少?
我找到官方的但进不去
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail