Skip to content

Commit a23d355

Browse files
committed
https_ota example added
1 parent 9241cf2 commit a23d355

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed

30_https_ota/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := https_ota
7+
8+
include $(IDF_PATH)/make/project.mk
9+

30_https_ota/main/component.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/certs.pem

30_https_ota/main/main.c

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include <string.h>
2+
3+
#include "freertos/FreeRTOS.h"
4+
#include "freertos/task.h"
5+
#include "cJSON.h"
6+
#include "driver/gpio.h"
7+
#include "esp_system.h"
8+
#include "esp_log.h"
9+
#include "esp_http_client.h"
10+
#include "esp_https_ota.h"
11+
12+
#include "wifi_functions.h"
13+
14+
#define FIRMWARE_VERSION 0.1
15+
#define UPDATE_JSON_URL "https://esp32tutorial.netsons.org/https_ota/firmware.json"
16+
#define BLINK_GPIO GPIO_NUM_26
17+
18+
// server certificates
19+
extern const char server_cert_pem_start[] asm("_binary_certs_pem_start");
20+
extern const char server_cert_pem_end[] asm("_binary_certs_pem_end");
21+
22+
// receive buffer
23+
char rcv_buffer[200];
24+
25+
// esp_http_client event handler
26+
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
27+
28+
switch(evt->event_id) {
29+
case HTTP_EVENT_ERROR:
30+
break;
31+
case HTTP_EVENT_ON_CONNECTED:
32+
break;
33+
case HTTP_EVENT_HEADER_SENT:
34+
break;
35+
case HTTP_EVENT_ON_HEADER:
36+
break;
37+
case HTTP_EVENT_ON_DATA:
38+
if (!esp_http_client_is_chunked_response(evt->client)) {
39+
strncpy(rcv_buffer, (char*)evt->data, evt->data_len);
40+
}
41+
break;
42+
case HTTP_EVENT_ON_FINISH:
43+
break;
44+
case HTTP_EVENT_DISCONNECTED:
45+
break;
46+
}
47+
return ESP_OK;
48+
}
49+
50+
// Blink task
51+
void blink_task(void *pvParameter) {
52+
53+
gpio_pad_select_gpio(BLINK_GPIO);
54+
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
55+
while(1) {
56+
gpio_set_level(BLINK_GPIO, 0);
57+
vTaskDelay(1000 / portTICK_PERIOD_MS);
58+
gpio_set_level(BLINK_GPIO, 1);
59+
vTaskDelay(1000 / portTICK_PERIOD_MS);
60+
}
61+
}
62+
63+
64+
// Check update task
65+
// downloads every 30sec the json file with the latest firmware
66+
void check_update_task(void *pvParameter) {
67+
68+
while(1) {
69+
70+
printf("Looking for a new firmware...\n");
71+
72+
// configure the esp_http_client
73+
esp_http_client_config_t config = {
74+
.url = UPDATE_JSON_URL,
75+
.event_handler = _http_event_handler,
76+
};
77+
esp_http_client_handle_t client = esp_http_client_init(&config);
78+
79+
// downloading the json file
80+
esp_err_t err = esp_http_client_perform(client);
81+
if(err == ESP_OK) {
82+
83+
// parse the json file
84+
cJSON *json = cJSON_Parse(rcv_buffer);
85+
if(json == NULL) printf("downloaded file is not a valid json, aborting...\n");
86+
else {
87+
cJSON *version = cJSON_GetObjectItemCaseSensitive(json, "version");
88+
cJSON *file = cJSON_GetObjectItemCaseSensitive(json, "file");
89+
90+
// check the version
91+
if(!cJSON_IsNumber(version)) printf("unable to read new version, aborting...\n");
92+
else {
93+
94+
double new_version = version->valuedouble;
95+
if(new_version > FIRMWARE_VERSION) {
96+
97+
printf("current firmware version (%.1f) is lower than the available one (%.1f), upgrading...\n", FIRMWARE_VERSION, new_version);
98+
if(cJSON_IsString(file) && (file->valuestring != NULL)) {
99+
printf("downloading and installing new firmware (%s)...\n", file->valuestring);
100+
101+
esp_http_client_config_t ota_client_config = {
102+
.url = file->valuestring,
103+
.cert_pem = server_cert_pem_start,
104+
};
105+
esp_err_t ret = esp_https_ota(&ota_client_config);
106+
if (ret == ESP_OK) {
107+
printf("OTA OK, restarting...\n");
108+
esp_restart();
109+
} else {
110+
printf("OTA failed...\n");
111+
}
112+
}
113+
else printf("unable to read the new file name, aborting...\n");
114+
}
115+
else printf("current firmware version (%.1f) is greater or equal to the available one (%.1f), nothing to do...\n", FIRMWARE_VERSION, new_version);
116+
}
117+
}
118+
}
119+
else printf("unable to download the json file, aborting...\n");
120+
121+
// cleanup
122+
esp_http_client_cleanup(client);
123+
124+
printf("\n");
125+
vTaskDelay(30000 / portTICK_PERIOD_MS);
126+
}
127+
}
128+
129+
void app_main() {
130+
131+
printf("HTTPS OTA, firmware %.1f\n\n", FIRMWARE_VERSION);
132+
133+
// start the blink task
134+
xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
135+
136+
// connect to the wifi network
137+
wifi_initialise();
138+
wifi_wait_connected();
139+
printf("Connected to wifi network\n");
140+
141+
// start the check update task
142+
xTaskCreate(&check_update_task, "check_update_task", 8192, NULL, 5, NULL);
143+
}

30_https_ota/main/wifi_functions.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/event_groups.h"
3+
#include "nvs_flash.h"
4+
#include "esp_event_loop.h"
5+
#include "esp_log.h"
6+
#include "esp_wifi.h"
7+
8+
#include "wifi_functions.h"
9+
10+
11+
// Event group for wifi connection
12+
static EventGroupHandle_t wifi_event_group;
13+
const int CONNECTED_BIT = BIT0;
14+
15+
// Wifi event handler
16+
static esp_err_t event_handler(void *ctx, system_event_t *event)
17+
{
18+
switch(event->event_id) {
19+
20+
case SYSTEM_EVENT_STA_START:
21+
esp_wifi_connect();
22+
break;
23+
24+
case SYSTEM_EVENT_STA_GOT_IP:
25+
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
26+
break;
27+
28+
case SYSTEM_EVENT_STA_DISCONNECTED:
29+
esp_wifi_connect();
30+
break;
31+
32+
default:
33+
break;
34+
}
35+
36+
return ESP_OK;
37+
}
38+
39+
void wifi_initialise(void) {
40+
41+
// initialize NVS, required for wifi
42+
ESP_ERROR_CHECK(nvs_flash_init());
43+
44+
// connect to the wifi network
45+
wifi_event_group = xEventGroupCreate();
46+
tcpip_adapter_init();
47+
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
48+
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
49+
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
50+
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
51+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
52+
wifi_config_t wifi_config = {
53+
.sta = {
54+
.ssid = WIFI_SSID,
55+
.password = WIFI_PASS,
56+
},
57+
};
58+
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
59+
ESP_ERROR_CHECK(esp_wifi_start());
60+
}
61+
62+
void wifi_wait_connected()
63+
{
64+
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
65+
}

30_https_ota/main/wifi_functions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _WIFI_FUNCTIONS_H_
2+
#define _WIFI_FUNCTIONS_H_
3+
4+
#define WIFI_SSID ""
5+
#define WIFI_PASS ""
6+
7+
void wifi_initialise(void);
8+
void wifi_wait_connected();
9+
10+
#endif

30_https_ota/server_certs/certs.pem

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
3+
MSQwIgYDVQQKExtEaWdpdG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