通过 ETH 的 ESP32 OTA

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

我有一个正在运行和工作的系统,它是一个带有 LAN8720 的 ESP32,可以通过互联网进行通信。

现在只需平面 HTTP 请求(通过

WiFiClientSecure
客户端)就可以像魅力一样工作。但我还需要(通过 https)更新设备。

现在我有这个代码:

#include <Arduino.h>
#include <Update.h>
#include <HTTPUpdate.h>

WiFiClientSecure otaClient;

Serial.println("Preparing to update");

// Do OTA update
otaClient.setInsecure(); //skip verification of SSL cert

if (!otaClient.connect(DIGITAL_HQ_BASE_URL, 443)) {
    Serial.println("Could not connect.");
}

otaClient.print("GET "); // watch the space!
otaClient.print(DIGITAL_HQ_BINARY_ENDPOINT); // API endpoint
otaClient.println(" HTTP/1.1"); // watch the space!
otaClient.print("Host: ");
otaClient.println(DIGITAL_HQ_BASE_URL);
otaClient.println("Connection: keep-alive"); // Don't close, since we need to perform OTA
otaClient.print("User-Agent: ");
otaClient.println(DIGITAL_HQ_USER_AGENT);
otaClient.println("Cache-Control: no-cache");
otaClient.println();

unsigned long timeout = millis();
while (otaClient.available() == 0) {
    if (millis() - timeout > 5000) {
        Serial.println("Client Timeout !");
        otaClient.stop();
        return;
    }
}

while (otaClient.available()) {
    // read line till /n
    String line = otaClient.readStringUntil('\n');
    // remove space, to check if the line is end of headers
    line.trim();

    // if the the line is empty,
    // this is end of headers
    // break the while and feed the
    // remaining `client` to the
    // Update.writeStream();
    if (!line.length()) {
        //headers ended
        break; // and get the OTA started
    }

    // Check if the HTTP Response is 200
    // else break and Exit Update
    if (line.startsWith("HTTP/1.1")) {
        if (line.indexOf("200") < 0) {
            Serial.println("Got a non 200 status code from server. Exiting OTA Update.");
            break;
        }
    }

    // extract headers here
    // Start with content length
    if (line.startsWith("Content-Length: ")) {
        contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
        Serial.println("Got " + String(contentLength) + " bytes from server");
    }

    // Next, the content type
    if (line.startsWith("Content-Type: ")) {
        String contentType = getHeaderValue(line, "Content-Type: ");
        Serial.println("Got " + contentType + " payload.");
        if (contentType == "application/octet-stream") {
            isValidContentType = true;
        }
    }
}

Serial.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));

if (contentLength && isValidContentType) {
    // Check if there is enough to OTA Update
    bool canBegin = Update.begin(contentLength);

    // If yes, begin
    if (canBegin) {
        Serial.println("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!");
        // No activity would appear on the Serial monitor
        // So be patient. This may take 2 - 5mins to complete
        size_t written = Update.writeStream(otaClient);

        if (written == contentLength) {
            Serial.println("Written : " + String(written) + " successfully");
        } else {
            Serial.println("Written only : " + ConvertFormatBytes(written) + "/" + ConvertFormatBytes(contentLength));
            // retry??
            // execOTA();
        }

        if (Update.end()) {
            Serial.println("OTA done!");
            if (Update.isFinished()) {
                Serial.println("Update successfully completed. Rebooting.");
                ESP.restart();
            } else {
                Serial.println("Update not finished? Something went wrong!");
            }
        } else {
            Serial.println("Error Occurred. Error #: " + String(Update.getError()));
        }
    } else {
        // not enough space to begin OTA
        // Understand the partitions and
        // space availability
        Serial.println("Not enough space to begin OTA");
        otaClient.stop();
    }
} else {
    Serial.println("There was no content in the response");
    otaClient.stop();
}

运行时没有错误,但在

Preparing to update
控制台消息上被冻结。有人知道我在这里做错了什么吗?

该文件需要来自 https 域。

https esp32 ota
1个回答
0
投票

我建议您使用 OTAdrive for SSL。检查此示例以了解 通过 HTTPS 的 OTA

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