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
+ }
0 commit comments