【发布时间】:2021-03-09 22:24:46
【问题描述】:
我是一名学生,目前正在攻读工程学位项目。我在编码、数据分析等方面有一些很好的基础知识,但我开始摸不着头脑了。所以基本上我一直在阅读 Arduino IDE 中包含的名为“BLE_write”的示例,该示例位于 ESP32 BLE Arduino 下的 ESP32 教程中。我将包括整个代码。这只是我整个项目的一部分,但它是让它发挥作用的关键一步。因此,本质上,此示例代码从手机上的应用程序中获取 uft-8 中的串行蓝牙,并将其打印到使用 ESP32 作为服务器的 PC 上的串行端口监视器上。 我需要做什么只是从蓝牙串口中取一个 1-60 的数字并将其分配给一个 int 变量。稍后我将使用此 int 为使用中断的计时器指定闹钟时间。但是现在,我想我只需要知道如何将该值分配给 int 变量。例如,我在应用程序上键入“42”并点击发送,然后代码获取并将 42 分配给一个 int 变量。我看到代码使用了一个类和一些 void 函数,所以我假设我必须更改它以返回一个值,但到目前为止,我所有的尝试都失败了。任何帮助或见解将不胜感激!顶部到 github 的链接什么都不做 :((
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
更新:我发现我需要做的是在定义的类中通过引用传递,以便存储“值”的值的地址。以下是我试图通过引用进行此类传递的尝试。然后我需要在设置或循环中调用该变量。所以是的,总而言之:我需要它在“MyCallBacks”类的 Void 函数“onWrite”中接收一个字符串,并将该字符串存储在一个地址中。然后在 main 或循环中访问该字符串。在设置的底部,您可以看到我的一项尝试,但出现错误:
"BLE_write:67:23: 错误: 'stgTimeSpan' 未在此范围内声明 std::string stg1 = &stgTimeSpan;"
希望此更新有所帮助。在下面找到已编辑的代码。
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
std::string *passString;
std::string stgTimeSpan = '\0';
passString = &value;
stgTimeSpan = *passString;
// if (value.length() > 0) {
// Serial.println("*********");
// Serial.print("New value: ");
// for (int i = 0; i < value.length(); i++)
// Serial.print(value[i]);
//
// Serial.println();
// Serial.println("*********");
// }
}
};
void setup() {
Serial.begin(115200);
Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
std::string stg1 = &stgTimeSpan;
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
【问题讨论】:
-
也许如果您分享了您的一项尝试,我们可能会为您提供帮助。如果我理解正确,您是在问如何将字符串转换为 int?
-
"42" 作为字符串是 ("4" - "0")*10 + ("2"-"0")。
-
您在
MyCallbacks中有打印语句,当您发送值42时它们显示什么? -
@ukBaz 它将 42 打印到串行监视器。查看我所做的更新。
-
@romkey 我更新了它以包含尝试。我想我需要通过引用传递,以便与设置或循环函数共享字符串,然后执行一些操作将其转换为 int 一次。但是现在由于范围错误,我在从设置或循环访问它时遇到了问题。看看我所做的更新。
标签: arduino bluetooth bluetooth-lowenergy esp32 arduino-ide