ESP32+Arduino+HttpsClient

ESP32+Arduino+HttpsClientで状況がよくわからずハマった事象を書いておきます。

WiFiClientSecure *client = new WiFiClientSecure;
if(client) {
    client -> setCACert(rootCACertificate);
    {
        // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is 
        HTTPClient https;
        Serial.print("[HTTPS] begin...\n");
        if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) {  // HTTPS
            Serial.print("[HTTPS] GET...\n");
            // start connection and send HTTP header
            int httpCode = https.GET();
            // httpCode will be negative on error
            if (httpCode > 0) {
                // HTTP header has been send and Server response header has been handled
                Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
                // file found at server
                if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
                    String payload = https.getString();
                    Serial.println(payload);
                }
            } 
            else {
                Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
            }

            https.end();
        } 
        else {
            Serial.printf("[HTTPS] Unable to connect\n");
        }
    }
    delete client;
} 
else {
    Serial.println("Unable to create client");
}

元コードはこちら

こんなコードでhttps通信を行おうとしましたが、その時にエラーとして
「[HTTPS] GET… failed, error: connection refused」
が出ました。
root Certificateに問題があるのかと思いましたが、他のスケッチでは動いていたので原因となるのはおかしいと思い、Core debug levelをveboseで実行してみると、原因がわかりました。
エラーメッセージは「connection refused」ですが、ヒープメモリ不足でこのエラーになっていました。

接続先やroot Certificateでも差があると思いますが、https通信を行う直前で

ESP.getFreeHeap()

で、
約60,000ではエラー
約90,000では正常
でした。

HttpsClientはヒープメモリを十分に確保して使いましょう。

カテゴリー: Arduino関連, ESP32 パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください