AIvoices/firmware-cpp/test/ota_test.cpp
2025-04-08 14:05:27 +01:00

69 lines
2.4 KiB
C++

// This sketch provide the functionality of OTA Firmware Upgrade
#include "WiFi.h"
#include "HttpsOTAUpdate.h"
// This sketch shows how to implement HTTPS firmware update Over The Air.
// Please provide your WiFi credentials, https URL to the firmware image and the server certificate.
static const char *ssid = "EE-PPA1GZ"; // your network SSID (name of wifi network)
static const char *password = "9JkyRJHXTDTKb3"; // your network password
#define TOUCH_MODE
#ifdef TOUCH_MODE
static const char *url = "https://elato.s3.us-east-1.amazonaws.com/firmware-touch.bin"; //state url of your firmware image
#else
static const char *url = "https://elato.s3.us-east-1.amazonaws.com/firmware-button.bin"; //state url of your firmware image
#endif
static const char *server_certificate = R"EOF(
-----BEGIN CERTIFICATE-----
<YOUR HOST CERTIFICATE HERE>
-----END CERTIFICATE-----
)EOF";
static HttpsOTAStatus_t otastatus;
void HttpEventCb(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(HttpEventCb);
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. To reboot device, call API ESP.restart() or PUSH restart button on device");
ESP.restart();
} else if (otastatus == HTTPS_OTA_FAIL) {
Serial.println("Firmware Upgrade Fail");
}
delay(1000);
}