ESP32 OTA HTTPS 简单测试

ESP32 OTA HTTPS simple test

我真的不知道我做错了什么..代码很简单但是我无法与服务器建立连接..

代码

 #include "WiFi.h"
#include "HttpsOTAUpdate.h"

static const char *ssid     = "DSRSB2-1701 2.4Ghz"; 
static const char *password = "DSR112233"; 

static const char *url = "https://sandboxarduino.000webhostapp.com/Intern/tagroba.ino.esp32.bin"; 

static const char *server_certificate = "-----BEGINCERTIFICATE-----MIIGQzCCBSugAwIBAgIQAa0laHpzho2fa0BMYg1P8DANBgkqhkiG9w0BAQsFADBeMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMR0wGwYDVQQDExRSYXBpZFNTTCBSU0EgQ0EgMjAxODAeFw0xOTA2MTEwMDAwMDBaFw0yMTA3MTAxMjAwMDBaMB4xHDAaBgNVBAMMEyouMDAwd2ViaG9zdGFwcC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDS1VeFEunq8KmmqZOmfEwMMQ8XyBon6dY3JpkhrFPvB18pcFBFMDsQomb0+2Sw7YULRdOUyOdg+t6fyLYF26cPPD2Gz5pbHL54z4pH/l3OKWz3Zeredv1UrBmzomcV7xwuX0Fr4IM5X37/ncXAXkROTUX2L7Osk1Im8YrzugkloJp6fUSRN3CGi7tT0eieLj1/8Ypf3lbeC7n+eNlYtLkF3xgebw2vymLCDq0apsUH3/AfedPk2HrQQjahqyDKmVkTJrfiQGupwsPyDgioWkplWzTSYWfPvd0bnUD6OMv0AWtNyuPfWIphcv4e1/f7yae2HRt3yGohbtGRQlLRmguvAgMBAAGjggM7MIIDNzAfBgNVHSMEGDAWgBRTyhdZ/GvAAyEvGq7kqqgcglbadTAdBgNVHQ4EFgQULGuBFhMxJAICmcEsNltAm7ZUUPkwMQYDVR0RBCowKIITKi4wMDB3ZWJob3N0YXBwLmNvbYIRMDAwd2ViaG9zdGFwcC5jb20wDgYDVR0PA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OQY6gcFoSg9kAqFGogjzTIrTwLfmmjTaJNMRVivA2FFMValstTGPqPC+BccFOUD+wCHLU1q33d/LfFFei+1gOQT1cV04+KkPbsvONlFd3wcsONdfR2v96kWnxugX4HQsOvh318nbZ8FKMG4VT5icoIygc//arJu4GUrYcQiLbL9K3a9Q3YqD8+4ip1UC/krpghWQKcrNW8VHnUUVLd7mxjrflKrC+80DMQ5o=-----ENDCERTIFICATE-----";

static HttpsOTAStatus_t otastatus;

void HttpEvent(HttpEvent_t *event)
{
    switch(event->event_id) {
        case HTTP_EVENT_ERROR:
            Serial.println("Http Event Error");
            break;
        case HTTP_EVENT_ON_CONNECTED:
            Serial.println("Http Event On Connected");
            break;
        case HTTP_EVENT_HEADER_SENT:
            Serial.println("Http Event Header Sent");
            break;
        case HTTP_EVENT_ON_HEADER:
            Serial.printf("Http Event On Header, key=%s, value=%s\n", event->header_key, event->header_value);
            break;
        case HTTP_EVENT_ON_DATA:
            break;
        case HTTP_EVENT_ON_FINISH:
            Serial.println("Http Event On Finish");
            break;
        case HTTP_EVENT_DISCONNECTED:
            Serial.println("Http Event Disconnected");
            break;
    }
}

void setup(){

    Serial.begin(115200);
    Serial.print("Attempting to connect to SSID: ");
    WiFi.begin(ssid, password);

    // attempt to connect to Wifi network:
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(1000);
    }

    Serial.print("Connected to ");
    Serial.println(ssid);
    
    HttpsOTA.onHttpEvent(HttpEvent);
    Serial.println("Starting OTA");
    HttpsOTA.begin(url, server_certificate); 

    Serial.println("Please Wait it takes some time ...");
}

void loop(){
    otastatus = HttpsOTA.status();
    if(otastatus == HTTPS_OTA_SUCCESS) { 
        Serial.println("Firmware written successfully. ESP32 is going to restart");
        ESP.restart();
    } else if(otastatus == HTTPS_OTA_FAIL) { 
        Serial.println("Firmware Upgrade Fail");
    }
    delay(1000);
}

这是Arduino IED串口监视器的输出 [串口监视器1

非常感谢你的帮助

您的证书必须像存储在 CRT 文件中一样出示。不要删除空格,不要删除换行符。

static const char *server_certificate = R"(
-----BEGIN CERTIFICATE-----
MIIGQzCCBSugAwIBAgIQAa0laHpzho2fa0BMYg1P8DANBgkqhkiG9w0BAQsFADBe
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMR0wGwYDVQQDExRSYXBpZFNTTCBSU0EgQ0EgMjAxODAe
Fw0xOTA2MTEwMDAwMDBaFw0yMTA3MTAxMjAwMDBaMB4xHDAaBgNVBAMMEyouMDAw
d2ViaG9zdGFwcC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDS
1VeFEunq8KmmqZOmfEwMMQ8XyBon6dY3JpkhrFPvB18pcFBFMDsQomb0+2Sw7YUL
RdOUyOdg+t6fyLYF26cPPD2Gz5pbHL54z4pH/l3OKWz3Zeredv1UrBmzomcV7xwu
X0Fr4IM5X37/ncXAXkROTUX2L7Osk1Im8YrzugkloJp6fUSRN3CGi7tT0eieLj1/
8Ypf3lbeC7n+eNlYtLkF3xgebw2vymLCDq0apsUH3/AfedPk2HrQQjahqyDKmVkT
JrfiQGupwsPyDgioWkplWzTSYWfPvd0bnUD6OMv0AWtNyuPfWIphcv4e1/f7yae2
HRt3yGohbtGRQlLRmguvAgMBAAGjggM7MIIDNzAfBgNVHSMEGDAWgBRTyhdZ/GvA
AyEvGq7kqqgcglbadTAdBgNVHQ4EFgQULGuBFhMxJAICmcEsNltAm7ZUUPkwMQYD
VR0RBCowKIITKi4wMDB3ZWJob3N0YXBwLmNvbYIRMDAwd2ViaG9zdGFwcC5jb20w
DgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjA+
BgNVHR8ENzA1MDOgMaAvhi1odHRwOi8vY2RwLnJhcGlkc3NsLmNvbS9SYXBpZFNT
TFJTQUNBMjAxOC5jcmwwTAYDVR0gBEUwQzA3BglghkgBh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