微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ESP32 OTA 通过 ETH

如何解决ESP32 OTA 通过 ETH

我有一个正在运行的系统,它是一个带有 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 域。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。