- ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好者 ❤️
- ❤️ 本篇创建记录 2025-03-12 ❤️
- ❤️ 本篇更新记录 2025-03-12 ❤️
- 🎉 欢迎关注 🔎点赞 👍收藏 ⭐️留言📝
- 🙏 此博客均由博主单独编写,不存在任何商业团队运营,如发现错误,请留言轰炸哦!及时修正!感谢支持!
1. 前言
为了更加方便调试开发物联网应用,博主开发了一个DPJCN物联网工具集小程序,目标是整合一些常用的开发工具到微信小程序中(毕竟现在电脑端也支持打开小程序了,那就等于这些开发工具都可以电脑端使用了,相比以往更加方便),同时也是计划不断添加新的可用性项目操作界面。
目前小程序已经通过备案认证,微信上可直接搜索“
DPJCN物联网工具集
”使用。
使用中存在任何问题,可以在评论区说明,博主都会修复。
2. 蓝牙小车工具使用说明
这里使用esp32来作为ble客户端,通过小程序来对它的服务特征进行通信。
硬件材料
- esp32(这里只是用它来模拟小车接收数据)
这个板子是博主自制的一个小pcb。
2.1 下载一下代码到ESP32
- bleCar.ino
// 导入必要的库
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <ArduinoJson.h> // 引入JSON处理库
// 设备启动打印信息
const char projectInfo[] PROGMEM = R"rawliteral(
/* *****************************************************************
* 基于ESP32C3 SuperMini的蓝牙小车控制系统
*
* 创建日期 :2025.03.12
* 最后更改日期:2025.03.12
*
*
* *****************************************************************/
)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;
}
}
delay(1000);
}
/**
* 初始化BLE
*/
void initBLE(void)
{
// 初始化一个ble设备
BLEDevice::init("蓝牙小车A");
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());
}
这里我们用到了二维码扫码功能,内容就是mac地址。所以先生成一个二维码待会使用。
2.2 打开小程序,选择蓝牙小车
2.3 扫码二维码控制小车
也能看到对应的调试信息。
0 - 停止
1 - 上
2 - 左
3 - 右
4 -下