Last active
August 7, 2021 00:56
-
-
Save ddlsmurf/9548d2098d62995a50376d8b5e075c6b to your computer and use it in GitHub Desktop.
ESP32 https OTA #watchy #esp32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wifi_update_client.h> | |
#include <esp_log.h> | |
#include <esp32-hal.h> | |
#include <esp_https_ota.h> | |
static const char *LOG_TAG = "wifi_update_client"; | |
const char *url = "https://raw.githubusercontent.com/sqfmi/Watchy/master/examples/WatchFaces/7_SEG/7_SEG.bin"; | |
// Thanks to Elder Scroll on the Watchy discord: | |
// echo "" | openssl s_client -showcerts -connect raw.githubusercontent.com:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM > raw.githubusercontent.com.pem | |
// The certificate should be in the file raw.githubusercontent.com.pem | |
const char *github_cert = | |
"-----BEGIN CERTIFICATE-----\n" | |
"MIIEsTCCA5mgAwIBAgIQBOHnpNxc8vNtwCtCuF0VnzANBgkqhkiG9w0BAQsFADBs\n" | |
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" | |
"d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n" | |
"ZSBFViBSb290IENBMB4XDTEzMTAyMjEyMDAwMFoXDTI4MTAyMjEyMDAwMFowcDEL\n" | |
"MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n" | |
"LmRpZ2ljZXJ0LmNvbTEvMC0GA1UEAxMmRGlnaUNlcnQgU0hBMiBIaWdoIEFzc3Vy\n" | |
"YW5jZSBTZXJ2ZXIgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2\n" | |
"4C/CJAbIbQRf1+8KZAayfSImZRauQkCbztyfn3YHPsMwVYcZuU+UDlqU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zu\n" | |
"xICaEnL6VpPX/78whQYwvwt/Tv9XBZ0k7YXDK/umdaisLRbvfXknsuvCnQsH6qqF\n" | |
"0wGjIChBWUMo0oHjqvbsezt3tkBigAVBRQHvFwY+3sAzm2fTYS5yh+Rp/BIAV0Ae\n" | |
"cPUeybQ=\n" | |
"-----END CERTIFICATE-----\n"; | |
static const char *event_to_str(esp_http_client_event_id_t id) { | |
switch (id) { | |
case HTTP_EVENT_ERROR: return "HTTP_EVENT_ERROR"; | |
case HTTP_EVENT_ON_CONNECTED: return "HTTP_EVENT_ON_CONNECTED"; | |
case HTTP_EVENT_HEADER_SENT: return "HTTP_EVENT_HEADER_SENT"; | |
case HTTP_EVENT_ON_HEADER: return "HTTP_EVENT_ON_HEADER"; | |
case HTTP_EVENT_ON_DATA: return "HTTP_EVENT_ON_DATA"; | |
case HTTP_EVENT_ON_FINISH: return "HTTP_EVENT_ON_FINISH"; | |
case HTTP_EVENT_DISCONNECTED: return "HTTP_EVENT_DISCONNECTED"; | |
default: return "HTTP_EVENT_unknown"; | |
} | |
} | |
esp_err_t http_event_handler(esp_http_client_event_t *evt) { | |
ESP_LOGI(LOG_TAG, "Download event %s (%d)", event_to_str(evt->event_id), evt->data_len); | |
yield(); | |
return ESP_OK; | |
} | |
const char *wifi_update_client() { | |
ESP_LOGI(LOG_TAG, "Starting image download from %s", url); | |
disableCore0WDT(); | |
esp_http_client_config_t config = { | |
.method = HTTP_METHOD_GET, | |
.url = url, | |
.cert_pem = github_cert, | |
.event_handler = http_event_handler, | |
}; | |
esp_err_t err = esp_https_ota(&config); | |
if (err == ESP_OK) { | |
ESP_LOGI(LOG_TAG, "Download OK - restarting"); | |
esp_restart(); | |
return NULL; | |
} | |
return esp_err_to_name(err); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WIFI_UPDATE_CLIENT_H__ | |
#define WIFI_UPDATE_CLIENT_H__ | |
# ifdef __cplusplus | |
extern "C" { | |
# endif | |
const char *wifi_update_client(); | |
# ifdef __cplusplus | |
} | |
# endif | |
#endif // WIFI_UPDATE_CLIENT_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment