简体   繁体   中英

ESP32 Arduino httpSecureClient -1 error at core 0 without reason why

I'm having an issue with the httpsecureclient library for the ESP in the Arduino IDE.

I try to send http requests to a https domain (that doesn't change) and works alot of times just fine.

Like I do some HTTP calls to obtain certain data to let the ESP do it's thing. But when I want to let the ESP post a payload to a server, using the WiFiClientSecure and HTTPClient , it sometimes works without issues, but all of a sudden, it stops working and throws me the well known, nothing saying -1 response code...

The code I ues to send a heartbeat is the following;

#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();

this code runs on core 0, via the following function; xTaskCreatePinnedToCore(sendHeartBeat, "Send Heartbeat", 20000, NULL, 25, &heartBeatTask, 0);

I do call the heartbeat once in the main core, then it works, but then on the second core, it sometimes does, but other times, it doesnt.

There is nothing too fancy about this, I think and I really can't seem to figure this one out...

Side notes:

There is an MQTT connection running to the AWS IoT hub, on core 1, where I don't have any issues with.

I currently run into same troubles after updating the libraries, old code for esp32 http clients stopped to work with the same symptoms.

I could solve this by switching to simply use HTTPClient only, without WiFiClientSecure. And it works with 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();
}

I'm wondering if you did find a solution for your issue. I currently run into same troubles after updating the libraries, old code for esp32 http clients stopped to work with the same symptoms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM