example:
EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
#include "ninux_esp32_ota.h"
...
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
...
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// 1.OTA app partition table has a smaller NVS partition size than the non-OTA
// partition table. This size mismatch may cause NVS initialization to fail.
// 2.NVS partition contains data in new format and cannot be recognized by this version of code.
// If this happens, we erase NVS partition and initialize NVS again.
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
...
tcpip_adapter_init();
initialise_wifi();
...
//HERE a check can run
// esp_ota_mark_app_valid_cancel_rollback();
// or
//esp_ota_mark_app_invalid_rollback_and_reboot();
// for firmware validation
...
ninux_esp32_ota();
...
in initialise_wifi usually there is this line: ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
Generate self-signed certificate and key:
NOTE: Common Name
of server certificate should be host-name of your server.
openssl req -x509 -newkey rsa:2048 -keyout ca_key.pem -out ca_cert.pem -days 365
- openssl configuration may require you to enter a passphrase for the key.
- When prompted for the
Common Name (CN)
, enter the name of the server that the ESP32 will connect to. For this local example, it is probably the IP address. The HTTPS client will make sure that theCN
matches the address given in the HTTPS URL (see Step 3).
Copy the certificate to server_certs
directory inside OTA example directory:
cp ca_cert.pem /path/to/ota/example/server_certs/
Start the HTTPS server:
openssl s_server -WWW -key ca_key.pem -cert ca_cert.pem -port 8070