微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用 ESP32 的 BLE 功能和 Arduino IDE 为整数赋值的问题

如何解决尝试使用 ESP32 的 BLE 功能和 Arduino IDE 为整数赋值的问题

我是一名学生,目前正在攻读工程学位。我在编码、数据分析等方面有一些很好的基础知识,但我开始不知所措。所以基本上我一直在浏览 Arduino IDE 中包含的示例,名为“BLE_write”,可在 ESP32 BLE Arduino 下的 ESP32 教程中找到。我将包括整个代码。这只是我整个项目的一部分,但它是让它发挥作用的关键一步。因此,本质上,此示例代码从手机上的应用程序接收 uft-8 中的串行蓝牙,并将其打印到 PC 上的串行端口监视器,使用 ESP32 作为服务器。 我需要做什么 只需从蓝牙串口中取出一个 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”中的“onWrite”中的空函数“onWrite”中接收一个字符串,并将该字符串存储在一个地址中。然后在主或循环中访问该字符串。在设置的底部,您可以看到我的一次尝试,但出现错误

"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);
}

解决方法

您可以将值保存在回调之外的全局字符串变量中,并使用 c_str() 从 std::string 中提取字符串。现在你可以在循环中对它执行任何字符串函数了。

String yourValue;

类 MyCallbacks:公共 BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue();

    yourValue = value.c_str();
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。