- ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好者 ❤️
- ❤️ 本篇创建记录 2025-03-02 ❤️
- ❤️ 本篇更新记录 2025-03-02 ❤️
- 🎉 欢迎关注 🔎点赞 👍收藏 ⭐️留言📝
- 🙏 此博客均由博主单独编写,不存在任何商业团队运营,如发现错误,请留言轰炸哦!及时修正!感谢支持!
快速导读
1. 前言
为了更加方便调试开发物联网应用,博主开发了一个DPJCN物联网工具集小程序,目标是整合一些常用的开发工具到微信小程序中(毕竟现在电脑端也支持打开小程序了,那就等于这些开发工具都可以电脑端使用了,相比以往更加方便),同时也是计划不断添加新的可用性项目操作界面。
目前小程序已经通过备案认证,微信上可直接搜索“
DPJCN物联网工具集
”使用。
使用中存在任何问题,可以在评论区说明,博主都会修复。
2. 蓝牙调试工具使用说明
这里使用esp32来作为ble客户端,通过小程序来对它的服务特征进行通信。
2.1 下载一下代码到ESP32
// 导入必要的库
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <ArduinoJson.h> // 引入JSON处理库
// 设备启动打印信息
const char projectInfo[] PROGMEM = R"rawliteral(
/* *****************************************************************
* 测试ble调试功能
*
* 创建日期 :2025.02.21
* 最后更改日期:2024.02.21
*
*
* *****************************************************************/
)rawliteral";
/******************* 常量声明 **********************/
// 定义收发服务的UUID(唯一标识)
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
// 定义唯一特征 用来接收蓝牙app发送过来的数据
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
// 定义唯一特征 用来发送给蓝牙app
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
// 说明一下,这两个ID主要用来做同步
#define INTERVAL 10000 // 读取时间间隔,默认10s
#define BUFFER_SIZE 20 // 缓存区大小
/******************* 常量声明 **********************/
/******************* 函数声明 **********************/
void initBLE(void); // 初始化BLE
void send(void); // 发送数据
/******************* 函数声明 **********************/
/******************* 变量定义 **********************/
BLECharacteristic *pCharacteristicWrite;
BLECharacteristic *pCharacteristicNotify;
BLEServer *pServer;
char command[BUFFER_SIZE]; // 存储上传Json数据
bool deviceConnected = false;
bool oldDeviceConnected = false;
String data = "";
unsigned long previousMillis = 0; // 记录上次读取的时间戳
bool hasInit = false;
String macAddress = "";
/******************* 变量定义 **********************/
// 定义一个Server回调
class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
Serial.println("------>有一个设备连入: ");
deviceConnected = true;
previousMillis = 0;
}
void onDisconnect(BLEServer *pServer)
{
deviceConnected = false;
}
void onMtuChanged(BLEServer *pServer, esp_ble_gatts_cb_param_t *param)
{
Serial.println("onMtuChanged>> MTU: ");
Serial.println(param->mtu.mtu);
Serial.println("onMtuChanged()");
}
};
// 定义一个特征回调
class MyWriteCallbacks : public BLECharacteristicCallbacks
{
void onRead(BLECharacteristic *pCharacteristic)
{
// 处理读取请求
}
void onWrite(BLECharacteristic *pCharacteristic)
{
// 处理写入请求
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0)
{
Serial.print("------>收到数据: ");
Serial.println(rxValue.c_str());
data = rxValue.c_str();
Serial.println();
}
}
};
void setup()
{
delay(2000); // 延时2秒,用于等待系统上电稳定
Serial.begin(115200); // 初始化串口,波特率 115200
Serial.println(projectInfo);
initBLE(); // 初始化BLE
}
void loop()
{
// 断开连接
if (!deviceConnected && oldDeviceConnected)
{
// 给蓝牙准备数据时间
delay(500);
BLEAdvertisementData oAdvData = BLEAdvertisementData();
// 设置广播数据
oAdvData.setManufacturerData(macAddress.c_str());
pServer->getAdvertising()->setAdvertisementData(oAdvData);
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("等待一个客户端连接通知...");
hasInit = false;
}
// 正在连接时
if (deviceConnected && !oldDeviceConnected)
{
// 记录正在连接
oldDeviceConnected = deviceConnected;
}
if (deviceConnected)
{
if (!hasInit)
{
delay(300); // 延时300ms
hasInit = true;
}
unsigned long currentMillis = millis(); // 获取当前时间戳
if (currentMillis - previousMillis >= 5000) // 每隔一段时间读取一下传感器数据 interval为时间间隔
{
previousMillis = currentMillis; // 记录当前时间戳
sendData();
delay(300); // 延时300ms
}
}
delay(2000);
}
/**
* 初始化BLE
*/
void initBLE(void)
{
// 初始化一个ble设备
BLEDevice::init("bleDebug");
macAddress = BLEDevice::getAddress().toString().c_str();
Serial.print("MAC:");
Serial.println(macAddress);
// 为蓝牙设备创建一个服务器
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// 基于SERVICE_UUID来创建一个服务
BLEService *pService = pServer->createService(SERVICE_UUID);
// 创建一个ble特征,用于收到对方数据
pCharacteristicWrite = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
pCharacteristicWrite->setCallbacks(new MyWriteCallbacks());
pCharacteristicNotify = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristicNotify->addDescriptor(new BLE2902());
// 启动服务器
pService->start();
// 启动广播
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
BLEAdvertisementData oAdvData = BLEAdvertisementData();
// 设置广播数据
oAdvData.setManufacturerData(macAddress.c_str());
pServer->getAdvertising()->setAdvertisementData(oAdvData);
pServer->getAdvertising()->start();
Serial.println("等待一个客户端连接通知...");
Serial.print("MTU:");
Serial.println(BLEDevice::getMTU());
}
/**
* 发送数据
*/
void sendData(void)
{
memset(command, 0, BUFFER_SIZE);
sprintf(command, "{\"data\":%.2f}", 30); // 构成Json数据
Serial.printf("param:%s\n", command); // 串口输出最终发送的数据
Serial.println("sendData~"); // 串口打印信息表示执行了此函数
pCharacteristicNotify->setValue(command); // 收到数据后返回数据
pCharacteristicNotify->notify(); // 通知BLE
}
在这里,我们创建了一个名字为bleDebug的ble设备,并且
// 定义收发服务的UUID(唯一标识)
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
// 定义唯一特征 用来接收蓝牙app发送过来的数据
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
// 定义唯一特征 用来发送给蓝牙app
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
在这里我们会创建一个id为6E400001-B5A3-F393-E0A9-E50E24DCCA9E
的服务。同时,创建了一个id为6E400002-B5A3-F393-E0A9-E50E24DCCA9E
的写特征,用来接收小程序发送过来的数据。创建了一个id为6E400003-B5A3-F393-E0A9-E50E24DCCA9E
的通知特征,用来给小程序发送数据。
下载成功之后,在串口会显示以下信息。
2.2 打开小程序,选择蓝牙调试
2.3 选择对应的蓝牙设备并连接
连接上设备后,设备会打印以下串口调试信息。
代码中会自动定时往小程序发送json数据。
2.4 选择对应的写服务和通知服务,测试读取数据
在调试信息区,可以看到:
2.5 测试小程序单次发送信息到设备
分别发送了
123
456
789
查看一下设备的打印信息。
2.6 测试小程序循环发送信息到设备
设置了间隔2s依次发送
123
456
789