c++ - ESP32 意外重启 - 有问题的变量?
问题描述
我的代码中有(至少)一行有问题。我的目的是为 HTTP 请求实例化一些对象。我将它们的指针存储在一个数组中,requestsBatch
因为我将从一个辅助函数中调用它们。存储它们后,我会做一些时间检查,然后将请求发送到服务器。请求成功后,该对象将被删除。
变量似乎address
有问题,但我不明白为什么。
const char* root_ca_sherz = "myCert";
int currentRequestsBatchIndex=0;
class HTTPREQUESTS {
public:
HTTPREQUESTS(String strAddress = "", String strParameters = "", bool bSendImmediately=false, const char* cert=root_ca_sherz) {
address = strAddress;
parameters = strParameters;
certificate = cert;
currentRequestsBatchIndex++;
Serial.println("New object instantiated");
Serial.println(address);
Serial.println(parameters);
}
~HTTPREQUESTS() {
currentRequestsBatchIndex--;
}
bool sendRequest() {
Serial.println("Called from within sendRequest()");
Serial.println(address); // <<<<<< THIS ROW CAUSES THE REBOOT
/*
http.begin(address+"/"+parameters, certificate); //, root_ca_sherz
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
Serial.print("Payload: ");
Serial.println(http.getString());
if (httpCode > 0) { //Check for the returning code
return true;
}
else {
Serial.println("Error on HTTP request");
return false;
}
http.end();
delay(1000);
return false;
*/
return true;
}
private:
const char* certificate="";
String parameters;
String device;
String address;
unsigned long timestamp=0;
int sendAttempts=0;
bool sendImmediately = false;
unsigned long lastSendAttemp=0;
};
HTTPREQUESTS *requestsBatch[5];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(3000);
Serial.println("Ready");
String address = "https://myAddress.com";
String parameters = "?secret=XXXXX&deviceName=deviceName&status=1&value=999&time=123456789&functionName=functionName";
HTTPREQUESTS *req01 = new HTTPREQUESTS(address, parameters);
// Store pointer to array to use it later
requestsBatch[currentRequestsBatchIndex] = req01;
Serial.println("Object stored in array");
Serial.print(F("Send request: "));
if(requestsBatch[0]->sendRequest()) {
//delete requestsBatch[0];
Serial.println("requestsBatch[0] deleted");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
解决方案:更换
currentRequestsBatchIndex++;
经过
if(address != "") {
currentRequestsBatchIndex++;
}
因为 currentRequestsBatchIndex 在创建空*requestsBatch[5]
数组时也在递增。
解决方案
因为我不喜欢 String 类(稍后会导致你的内存问题)我用 char 数组重写,它按预期工作(包括你的“解决方案”,但你检查了串行监视器输出):
const char* root_ca_sherz = "myCert";
char address[128] = {'\0'};
int currentRequestsBatchIndex = 0;
class HTTPREQUESTS {
public:
HTTPREQUESTS(char strAddress[128] = {'\0'}, char strParameters [128] = {'\0'}, bool bSendImmediately = false, const char* cert = root_ca_sherz) {
strcpy (address, strAddress);
strcpy (parameters, strParameters);
certificate = cert;
if (address[0] != '\0') {
currentRequestsBatchIndex++;
}
currentRequestsBatchIndex++;
Serial.println("New object instantiated");
Serial.println(address);
Serial.println(parameters);
}
~HTTPREQUESTS() {
currentRequestsBatchIndex--;
}
bool sendRequest() {
char testAddress [128] = {'\0'};
Serial.println("Called from within sendRequest()");
strcpy (testAddress, address);
Serial.print("Will work: ");
Serial.println(testAddress); // <<<<<< THIS ROW CAUSES THE REBOOT
Serial.print("Will also work: ");
Serial.println(address); // but over written at next creation
/*
http.begin(address+"/"+parameters, certificate); //, root_ca_sherz
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
Serial.print("Payload: ");
Serial.println(http.getString());
if (httpCode > 0) { //Check for the returning code
return true;
}
else {
Serial.println("Error on HTTP request");
return false;
}
http.end();
delay(1000);
return false;
*/
return true;
}
private:
const char* certificate = "";
char parameters[128] = {'\0'};
char device[128] = {'\0'};
// char address[128] = {'\0'};
unsigned long timestamp = 0;
int sendAttempts = 0;
bool sendImmediately = false;
unsigned long lastSendAttemp = 0;
};
HTTPREQUESTS *requestsBatch[5];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(3000);
Serial.println("Ready");
const char address[] = "https://myAddress.com";
const char parameters[] = "?secret=XXXXX&deviceName=deviceName&status=1&value=999&time=123456789&functionName=functionName";
HTTPREQUESTS *req01 = new HTTPREQUESTS(address, parameters);
// Store pointer to array to use it later
requestsBatch[currentRequestsBatchIndex] = req01;
Serial.println("Object stored in array");
Serial.print(F("Send request: "));
if (requestsBatch[0]->sendRequest()) {
//delete requestsBatch[0];
Serial.println("requestsBatch[0] deleted");
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Still running !");
delay(1000);
}
推荐阅读
- git - 有没有什么简单的方法可以让 master 中的每个功能都提交一次,而不是使用 squash 进行 rebase
- c++ - 如何从 __m64 值的 lsb 创建一个 8 位掩码?
- java - 使用 Mulesoft 或 Java 从 json 字符串中提取特定的 json 字段 [] 并存储在 HashMap 中
- java - 为什么 Spring ORM JpaTransactionManager 使用原生实体管理器工厂?
- mysql - 如何在mysql中使用join创建视图
- r - purrr auto.arima xreg 组合
- javascript - 从 Observable 中获取值
目的 - kubernetes - 无法更改 Kubernetes 日志目录
- node.js - 如何在 IBM Watson Conversation 中调用外部 API
- java - 在 glassfish 上以休眠方式缓存