ESP32 因字符串转为 int 而发生恐慌

问题描述 投票:0回答:1

我不是编码专家,我尝试编写一些代码来使用 ESP32 控制蓝牙信号发生器。我设法用一个数字让它发挥作用,现在我尝试从 BTLE 连接一些变量。

这是我的代码:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

String valor;
long previousMillis_BLE1 = 0;
int interval_BLE1 = 1000;

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#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) {
        valor = "";
        for (int i = 0; i < value.length(); i++){
          //Serial.print(value[i]); // Presenta value.
          valor = valor + value[i];
        }

        
       // Serial.print("valor = ");
        Serial.println(valor); // Presenta valor.
      }
    }
};



const byte CPS1 = 3;
const byte CPS2 = 4;

unsigned long TotalTeeth = 10;
unsigned long MissingTeeth = 2;
unsigned long TeethBetweenMissingTeeth = 0;

unsigned long RPM = 800;

//long RPM = valor.toInt();
//unsigned long RPM = valor.toInt();
//int RPM = valor.toInt();
//long int RPM = valor.toInt();


const unsigned long MicrosecondsPerMinute = 1000000ul * 60;


void setup() {

Serial.begin(115200);

  // Create the BLE1 Device
  BLEDevice::init("MyESP32_BLE1");

  // Create the BLE Server
  pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();


  pinMode(CPS1, OUTPUT);
  pinMode(CPS2, OUTPUT);

}


void loop() {

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis_BLE1 > interval_BLE1) {
  float TemperatureBLE1 = random(10,60000)/1000.0; // 3 decimals
  float HumidityBLE1 = random(5,99000)/1000.0;  // 3 decimals

  String temperatureBLE1 = String(TemperatureBLE1,3);
  String humidityBLE1 = String(HumidityBLE1,3);
  String tem_hum_BLE1 = temperatureBLE1 + "," + humidityBLE1;

      std::string value = pCharacteristic->getValue();
      pCharacteristic->setValue(tem_hum_BLE1.c_str()); // Notify.
      pCharacteristic->notify();
      
  previousMillis_BLE1 = currentMillis;     
  interval_BLE1 = random(500,1000);
  }

  unsigned long pulsesPerMinute = RPM * TotalTeeth;
  unsigned long microsecondsPerPulse = MicrosecondsPerMinute / pulsesPerMinute;
  unsigned long microsecondsPerHalfPulse = microsecondsPerPulse / 2;

  for (int i = 0; i < TotalTeeth - MissingTeeth - (TeethBetweenMissingTeeth * (MissingTeeth - 1)); i++)
  {
    Tooth(microsecondsPerHalfPulse);
  }

  for (int i = 0; i < MissingTeeth; i++)
  {
    ToothMissing(microsecondsPerHalfPulse);

    // Between pairs of missing teeth, insert this many teeth
    if (i < MissingTeeth - 1)
    {
      for (int j = 0; j < TeethBetweenMissingTeeth; j++)
      {
        Tooth(microsecondsPerHalfPulse);
      }
    }
  }


}

void Tooth(int duration)
{
  digitalWrite(CPS1, HIGH);
  digitalWrite(CPS2, HIGH);
  delayMicroseconds(duration);
  digitalWrite(CPS1, LOW);
  digitalWrite(CPS2, LOW);
  delayMicroseconds(duration);
}

void ToothMissing(int duration)
{
  // two delays for both halves of a missing pulse
  delayMicroseconds(duration);
  delayMicroseconds(duration);

}

这部分代码是我尝试过的,但不起作用。我将其一一取消注释以进行测试,并且每一行都将永远重新启动 ESP32。

//long RPM = valor.toInt();
//unsigned long RPM = valor.toInt();
//int RPM = valor.toInt();
//long int RPM = valor.toInt();

您知道还有其他方法可以将变量“valor”转换为数字吗?我认为那是一根绳子...

提前谢谢你,丹

//long RPM = valor.toInt();
//unsigned long RPM = valor.toInt();
//int RPM = valor.toInt();
//long int RPM = valor.toInt();

他们中的任何一个遇到 ESP32 都会恐慌并永远重启。

string integer esp32 arduino-esp32 panic
1个回答
0
投票

问题是

valor.toInt()
返回 0。这会导致
pulsesPerMinute
中的
unsigned long pulsesPerMinute = RPM * TotalTeeth;
也等于 0。然后,在接下来的行
unsigned long microsecondsPerPulse = MicrosecondsPerMinute / pulsesPerMinute;
中,由于
pulsesPerMinute
为零,因此被零除。为了解决这个问题,你应该将行
long RPM = valor.toInt();
放在
onWrite
方法中,因为现在,RPM 是在程序开始时计算的,而此时 valor 尚未初始化,导致
valor.toInt()
返回 0。除此之外,建议在计算
pulsesPerMinute
之前检查 RPM 的值。

修复这些问题的示例代码(我尚未测试此代码):

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

String valor;
long previousMillis_BLE1 = 0;
int interval_BLE1 = 1000;

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

const byte CPS1 = 3;
const byte CPS2 = 4;

unsigned long TotalTeeth = 10;
unsigned long MissingTeeth = 2;
unsigned long TeethBetweenMissingTeeth = 0;

unsigned long RPM = 800;



const unsigned long MicrosecondsPerMinute = 1000000ul * 60;


#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) {
        valor = "";
        for (int i = 0; i < value.length(); i++){
          //Serial.print(value[i]); // Presenta value.
          valor = valor + value[i];
        }

        
       // Serial.print("valor = ");
        Serial.println(valor); // Presenta valor.
      }
      long RPM = valor.toInt();
    }
};

void setup() {

Serial.begin(115200);

  // Create the BLE1 Device
  BLEDevice::init("MyESP32_BLE1");

  // Create the BLE Server
  pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();


  pinMode(CPS1, OUTPUT);
  pinMode(CPS2, OUTPUT);

}


void loop() {

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis_BLE1 > interval_BLE1) {
  float TemperatureBLE1 = random(10,60000)/1000.0; // 3 decimals
  float HumidityBLE1 = random(5,99000)/1000.0;  // 3 decimals

  String temperatureBLE1 = String(TemperatureBLE1,3);
  String humidityBLE1 = String(HumidityBLE1,3);
  String tem_hum_BLE1 = temperatureBLE1 + "," + humidityBLE1;

      std::string value = pCharacteristic->getValue();
      pCharacteristic->setValue(tem_hum_BLE1.c_str()); // Notify.
      pCharacteristic->notify();
      
  previousMillis_BLE1 = currentMillis;     
  interval_BLE1 = random(500,1000);
  }

  if (RPM == 0) {
    // handle error, for example:
    Serial.println("RPM is 0, returning from loop...");
    return;

  }
  unsigned long pulsesPerMinute = RPM * TotalTeeth;
  unsigned long microsecondsPerPulse = MicrosecondsPerMinute / pulsesPerMinute;
  unsigned long microsecondsPerHalfPulse = microsecondsPerPulse / 2;

  for (int i = 0; i < TotalTeeth - MissingTeeth - (TeethBetweenMissingTeeth * (MissingTeeth - 1)); i++)
  {
    Tooth(microsecondsPerHalfPulse);
  }

  for (int i = 0; i < MissingTeeth; i++)
  {
    ToothMissing(microsecondsPerHalfPulse);

    // Between pairs of missing teeth, insert this many teeth
    if (i < MissingTeeth - 1)
    {
      for (int j = 0; j < TeethBetweenMissingTeeth; j++)
      {
        Tooth(microsecondsPerHalfPulse);
      }
    }
  }


}

void Tooth(int duration)
{
  digitalWrite(CPS1, HIGH);
  digitalWrite(CPS2, HIGH);
  delayMicroseconds(duration);
  digitalWrite(CPS1, LOW);
  digitalWrite(CPS2, LOW);
  delayMicroseconds(duration);
}

void ToothMissing(int duration)
{
  // two delays for both halves of a missing pulse
  delayMicroseconds(duration);
  delayMicroseconds(duration);

}
© www.soinside.com 2019 - 2024. All rights reserved.