【问题标题】:ESP32 Arduino httpSecureClient -1 error at core 0 without reason whyESP32 Arduino httpSecureClient -1 核心 0 错误,没有原因
【发布时间】:2021-10-20 08:36:36
【问题描述】:

我在使用 Arduino IDE 中的 ESP 的 httpsecureclient 库时遇到问题。

我尝试将 http 请求发送到 https 域(这不会改变)并且可以正常工作很多次。

就像我做一些 HTTP 调用来获取某些数据让 ESP 做这件事一样。但是当我想让 ESP 使用WiFiClientSecureHTTPClient 将有效负载发布到服务器时,它有时可以正常工作,但突然之间,它停止工作并让我知道众所周知,没什么可说的@ 987654324@响应码...

我用来发送心跳的代码如下;

#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

WiFiClientSecure ApiClient;
HTTPClient ApiHttpClient;


StaticJsonDocument<256> doc;
doc["mac"] = deviceMacAddress;
doc["key"] = DEVICE_SECRET;
doc["type"] = DIGITAL_HQ_SOFTWARE_TYPE;
String heartbeatData;
serializeJson(doc, heartbeatData);
ApiClient.setInsecure(); //skip verification of SSL cert
Serial.println("Sending Heartbeat");
ApiHttpClient.begin(ApiClient, DIGITAL_HQ_HEARTBEAT_ENDPOINT);
ApiHttpClient.addHeader("Content-Type", "application/json");
ApiHttpClient.setUserAgent(DIGITAL_HQ_USER_AGENT);
int responseCode = ApiHttpClient.POST(heartbeatData); // just post, Don't care about the response.
if (responseCode != 200) {
    failedApiCalls ++;
}
Serial.print("ResponseCode from heartbeat: ");
Serial.println(responseCode);
// Free resources
ApiHttpClient.end();

此代码通过以下函数在核心 0 上运行; xTaskCreatePinnedToCore(sendHeartBeat, "Send Heartbeat", 20000, NULL, 25, &amp;heartBeatTask, 0);

我确实在主核心中调用一次心跳,然后它可以工作,但是在第二个核心上,它有时会,但其他时候,它不会。

这没什么太花哨的,我想,我真的无法弄清楚这个......

旁注:

有一个 MQTT 连接运行到核心 1 上的 AWS IoT 中心,我对此没有任何问题。

【问题讨论】:

  • 我认为有些页面没有证书是不允许访问的。尝试设置证书,看看是否有效。这是一个很好的指南:techtutorialsx.com/2017/11/18/esp32-arduino-https-get-request
  • 我不认为这应该是这里的问题。因为我做了“ApiClient.setInsecure();”并且请求有效,但突然之间他们却没有。
  • 好吧,我只是说尝试不会有什么坏处。
  • 用证书试过了,同样的错误......
  • 错误 -1 表示连接被拒绝。所以响应代码确实说了些什么。您可以在浏览器中访问您尝试连接的 URL 并查看服务器是否在线?

标签: c++ arduino esp32


【解决方案1】:

我目前在更新库后遇到了同样的问题,esp32 http 客户端的旧代码停止工作并出现相同的症状。

我可以通过切换为仅使用 HTTPClient 而无需 WiFiClientSecure 来解决此问题。它适用于 https。

#include <HTTPClient.h>
#include <Arduino_JSON.h>
    
void getPricesFromKraken(){
    String url = "https://api.kraken.com/0/public/Ticker?pair=DOGEUSD,XBTUSD";
    
    HTTPClient http;
    JSONVar data;
      
    http.begin(url.c_str());
    int httpResponseCode = http.GET();
    
    if (httpResponseCode > 0) {
        String payload = http.getString();
        data = JSON.parse(payload);
        Serial.println(data);
    }
    else {
        Serial.printf("http response code: %d\n", httpResponseCode);
    }
    http.end();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2017-08-27
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多