Skip to content

Commit

Permalink
https_ota example added
Browse files Browse the repository at this point in the history
  • Loading branch information
lucadentella committed Oct 27, 2018
1 parent 9241cf2 commit a23d355
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 30_https_ota/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := https_ota

include $(IDF_PATH)/make/project.mk

1 change: 1 addition & 0 deletions 30_https_ota/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/certs.pem
143 changes: 143 additions & 0 deletions 30_https_ota/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "cJSON.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"

#include "wifi_functions.h"

#define FIRMWARE_VERSION 0.1
#define UPDATE_JSON_URL "https://esp32tutorial.netsons.org/https_ota/firmware.json"
#define BLINK_GPIO GPIO_NUM_26

// server certificates
extern const char server_cert_pem_start[] asm("_binary_certs_pem_start");
extern const char server_cert_pem_end[] asm("_binary_certs_pem_end");

// receive buffer
char rcv_buffer[200];

// esp_http_client event handler
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {

switch(evt->event_id) {
case HTTP_EVENT_ERROR:
break;
case HTTP_EVENT_ON_CONNECTED:
break;
case HTTP_EVENT_HEADER_SENT:
break;
case HTTP_EVENT_ON_HEADER:
break;
case HTTP_EVENT_ON_DATA:
if (!esp_http_client_is_chunked_response(evt->client)) {
strncpy(rcv_buffer, (char*)evt->data, evt->data_len);
}
break;
case HTTP_EVENT_ON_FINISH:
break;
case HTTP_EVENT_DISCONNECTED:
break;
}
return ESP_OK;
}

// Blink task
void blink_task(void *pvParameter) {

gpio_pad_select_gpio(BLINK_GPIO);
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
while(1) {
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(BLINK_GPIO, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}


// Check update task
// downloads every 30sec the json file with the latest firmware
void check_update_task(void *pvParameter) {

while(1) {

printf("Looking for a new firmware...\n");

// configure the esp_http_client
esp_http_client_config_t config = {
.url = UPDATE_JSON_URL,
.event_handler = _http_event_handler,
};
esp_http_client_handle_t client = esp_http_client_init(&config);

// downloading the json file
esp_err_t err = esp_http_client_perform(client);
if(err == ESP_OK) {

// parse the json file
cJSON *json = cJSON_Parse(rcv_buffer);
if(json == NULL) printf("downloaded file is not a valid json, aborting...\n");
else {
cJSON *version = cJSON_GetObjectItemCaseSensitive(json, "version");
cJSON *file = cJSON_GetObjectItemCaseSensitive(json, "file");

// check the version
if(!cJSON_IsNumber(version)) printf("unable to read new version, aborting...\n");
else {

double new_version = version->valuedouble;
if(new_version > FIRMWARE_VERSION) {

printf("current firmware version (%.1f) is lower than the available one (%.1f), upgrading...\n", FIRMWARE_VERSION, new_version);
if(cJSON_IsString(file) && (file->valuestring != NULL)) {
printf("downloading and installing new firmware (%s)...\n", file->valuestring);

esp_http_client_config_t ota_client_config = {
.url = file->valuestring,
.cert_pem = server_cert_pem_start,
};
esp_err_t ret = esp_https_ota(&ota_client_config);
if (ret == ESP_OK) {
printf("OTA OK, restarting...\n");
esp_restart();
} else {
printf("OTA failed...\n");
}
}
else printf("unable to read the new file name, aborting...\n");
}
else printf("current firmware version (%.1f) is greater or equal to the available one (%.1f), nothing to do...\n", FIRMWARE_VERSION, new_version);
}
}
}
else printf("unable to download the json file, aborting...\n");

// cleanup
esp_http_client_cleanup(client);

printf("\n");
vTaskDelay(30000 / portTICK_PERIOD_MS);
}
}

void app_main() {

printf("HTTPS OTA, firmware %.1f\n\n", FIRMWARE_VERSION);

// start the blink task
xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);

// connect to the wifi network
wifi_initialise();
wifi_wait_connected();
printf("Connected to wifi network\n");

// start the check update task
xTaskCreate(&check_update_task, "check_update_task", 8192, NULL, 5, NULL);
}
65 changes: 65 additions & 0 deletions 30_https_ota/main/wifi_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "nvs_flash.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_wifi.h"

#include "wifi_functions.h"


// Event group for wifi connection
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;

// Wifi event handler
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:
esp_wifi_connect();
break;

default:
break;
}

return ESP_OK;
}

void wifi_initialise(void) {

// initialize NVS, required for wifi
ESP_ERROR_CHECK(nvs_flash_init());

// connect to the wifi network
wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}

void wifi_wait_connected()
{
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
}
10 changes: 10 additions & 0 deletions 30_https_ota/main/wifi_functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _WIFI_FUNCTIONS_H_
#define _WIFI_FUNCTIONS_H_

#define WIFI_SSID ""
#define WIFI_PASS ""

void wifi_initialise(void);
void wifi_wait_connected();

#endif
27 changes: 27 additions & 0 deletions 30_https_ota/server_certs/certs.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE-----
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow
SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT
GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF
q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8
SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0
Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA
a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj
/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T
AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG
CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv
bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k
c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw
VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC
ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz
MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu
Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
-----END CERTIFICATE-----

0 comments on commit a23d355

Please sign in to comment.