1
|
/*
|
2
|
* Academic License - for use in teaching, academic research, and meeting
|
3
|
* course requirements at degree granting institutions only. Not for
|
4
|
* government, commercial, or other organizational use.
|
5
|
*
|
6
|
* File: waijung2_hwdrvlib.c
|
7
|
*
|
8
|
* Code generated with Waijung 2 ESP32 Target Blockset,
|
9
|
* for Simulink model 'esp32_ota_sta_demo1'.
|
10
|
*
|
11
|
* Model version : 10.0
|
12
|
* Simulink Coder version : 24.2 (R2024b) 21-Jun-2024
|
13
|
* C/C++ source code generated on : Tue Jun 17 17:38:50 2025
|
14
|
*
|
15
|
* Target selection: esp32.tlc
|
16
|
* Embedded hardware selection: Cadence Design Systems (Tensilica)->Espressif Xtensa single-/dual-core 32-bit LX6 microprocessor
|
17
|
* Code generation objectives: Unspecified
|
18
|
* Validation result: Not run
|
19
|
*/
|
20
|
|
21
|
/** Model's header file **/
|
22
|
#include "esp32_ota_sta_demo1.h"
|
23
|
|
24
|
int32_t waijung2_sprintf_s(char *buffer, uint16_t buffer_len, const char
|
25
|
* format, ...)
|
26
|
{
|
27
|
int res;
|
28
|
va_list args;
|
29
|
va_start (args, format);
|
30
|
res = vsnprintf (buffer, (int)buffer_len, format, args);
|
31
|
va_end (args);
|
32
|
return res;
|
33
|
}
|
34
|
|
35
|
static const char *TAG = "WJG2_OTA_FIRMWARE_UPGRADE_BLOCK";
|
36
|
esp_err_t _http_OTA_event_handler(esp_http_client_event_t *evt)
|
37
|
{
|
38
|
switch (evt->event_id) {
|
39
|
case HTTP_EVENT_ERROR:
|
40
|
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
41
|
break;
|
42
|
|
43
|
case HTTP_EVENT_ON_CONNECTED:
|
44
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
45
|
break;
|
46
|
|
47
|
case HTTP_EVENT_HEADER_SENT:
|
48
|
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
|
49
|
break;
|
50
|
|
51
|
case HTTP_EVENT_ON_HEADER:
|
52
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key,
|
53
|
evt->header_value);
|
54
|
break;
|
55
|
|
56
|
case HTTP_EVENT_ON_DATA:
|
57
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
58
|
break;
|
59
|
|
60
|
case HTTP_EVENT_ON_FINISH:
|
61
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
62
|
break;
|
63
|
|
64
|
case HTTP_EVENT_DISCONNECTED:
|
65
|
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
|
66
|
break;
|
67
|
|
68
|
case HTTP_EVENT_REDIRECT:
|
69
|
ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
|
70
|
break;
|
71
|
}
|
72
|
|
73
|
return ESP_OK;
|
74
|
}
|
75
|
|
76
|
void ota_firmware_upgrade(char * ota_url )
|
77
|
{
|
78
|
ESP_LOGI(TAG, "Starting OTA");
|
79
|
esp_http_client_config_t config = {
|
80
|
.url = ota_url,
|
81
|
|
82
|
//.cert_pem = google_cert,
|
83
|
.crt_bundle_attach = esp_crt_bundle_attach,
|
84
|
.event_handler = _http_OTA_event_handler,
|
85
|
.keep_alive_enable = true,
|
86
|
};
|
87
|
|
88
|
esp_https_ota_config_t ota_config = {
|
89
|
.http_config = &config,
|
90
|
};
|
91
|
|
92
|
ESP_LOGI(TAG, "Upgrading firmware ...");
|
93
|
esp_err_t ret = esp_https_ota(&ota_config);
|
94
|
if (ret == ESP_OK) {
|
95
|
ESP_LOGI(TAG, "Successfully upgrade the firmware");
|
96
|
ESP_LOGI(TAG, "ESP32 module is rebooting...");
|
97
|
esp_restart();
|
98
|
} else {
|
99
|
ESP_LOGE(TAG, "Firmware upgrading failed");
|
100
|
}
|
101
|
}
|
102
|
|
103
|
void GPIO_enable_DigitalOutput()
|
104
|
{
|
105
|
gpio_pad_select_gpio(2);
|
106
|
gpio_set_direction(2, GPIO_MODE_OUTPUT);
|
107
|
gpio_set_drive_capability(GPIO_NUM_2, GPIO_DRIVE_CAP_2);
|
108
|
}
|
109
|
|
110
|
int clients_num= 0;
|
111
|
int WIFI_CONNECTW = 0;
|
112
|
const int WIFI_CONNECTED_BIT = BIT0; /* connected bit for wifi station mode */
|
113
|
int WIFI_INIT = 0; /* 0 -> fresh init, 1 -> AP mode, 2 -> STA mode */
|
114
|
static const char* WIFITAG = "WJG2_WIFI_SETUP_BLOCK";
|
115
|
static EventGroupHandle_t wifi_event_group;// Event group for wifi connection
|
116
|
wifi_sta_list_t clients;
|
117
|
esp_netif_t *esp_netif;
|
118
|
char GW[20] ;
|
119
|
int clients_rssi = 0;
|
120
|
wifi_ap_record_t ap0;
|
121
|
esp_netif_ip_info_t info;
|
122
|
uint apinfo = 0;
|
123
|
static void event_handler(void* arg, esp_event_base_t event_base, int32_t
|
124
|
event_id, void* event_data) // Wifi event handler
|
125
|
{
|
126
|
if (event_base == WIFI_EVENT) {
|
127
|
switch (event_id)
|
128
|
{
|
129
|
case WIFI_EVENT_AP_STACONNECTED:
|
130
|
if (esp_wifi_ap_get_sta_list(&clients) == ESP_OK) {
|
131
|
clients_num = clients.num;
|
132
|
clients_rssi = clients.sta[0].rssi;
|
133
|
}
|
134
|
break;
|
135
|
|
136
|
case WIFI_EVENT_AP_STADISCONNECTED:
|
137
|
if (esp_wifi_ap_get_sta_list(&clients) == ESP_OK) {
|
138
|
clients_num = clients.num;
|
139
|
}
|
140
|
break;
|
141
|
|
142
|
case WIFI_EVENT_STA_START:
|
143
|
ESP_LOGI(WIFITAG,"- Wifi STA start event\n\n");
|
144
|
esp_wifi_connect();
|
145
|
break;
|
146
|
|
147
|
case WIFI_EVENT_STA_DISCONNECTED:
|
148
|
//xEventGroupClearBits(event_group, WIFI_CONNECTED_BIT);
|
149
|
//printf("Disconnected from Router retry connection\r\n");
|
150
|
ESP_LOGI(WIFITAG,"Disconnected from Router retry connection\r\n");
|
151
|
WIFI_CONNECTW = 0;
|
152
|
esp_wifi_connect();
|
153
|
break;
|
154
|
|
155
|
default:
|
156
|
break;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
if (event_base == IP_EVENT ) {
|
161
|
switch (event_id)
|
162
|
{
|
163
|
case IP_EVENT_STA_GOT_IP:
|
164
|
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
165
|
ESP_LOGI(WIFITAG,"Connected to Router and obtained IP\r\n");
|
166
|
ESP_ERROR_CHECK(esp_netif_get_ip_info(esp_netif, &info));
|
167
|
ip4addr_ntoa_r((ip4_addr_t*)&info.gw,GW,sizeof(GW));
|
168
|
ESP_LOGI(WIFITAG,"GW: %s \n", GW);
|
169
|
esp_wifi_sta_get_ap_info(&ap0);
|
170
|
apinfo = ap0.rssi;
|
171
|
ESP_LOGI(WIFITAG,"Connected AP RSSI = %d\n", apinfo);
|
172
|
WIFI_CONNECTW = 1;
|
173
|
break;
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
|
178
|
void wifi_initialise()
|
179
|
{
|
180
|
esp_err_t ret = nvs_flash_init();
|
181
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
182
|
{
|
183
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
184
|
ret = nvs_flash_init();
|
185
|
}
|
186
|
|
187
|
// connect to the wifi network
|
188
|
wifi_event_group = xEventGroupCreate();
|
189
|
ESP_ERROR_CHECK(esp_netif_init());
|
190
|
}
|
191
|
|
192
|
void wifi_wait_connected()
|
193
|
{
|
194
|
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, false, true, 12000/
|
195
|
portTICK_PERIOD_MS);
|
196
|
// Add 12 seconds search for wifi ...... originally was portMAX_DELAY to wait infinetely
|
197
|
}
|
198
|
|
199
|
void WIFI_start_Station_Mode_WIFISetup(char * ssid, char * pass)
|
200
|
{
|
201
|
if (WIFI_INIT == 0) {
|
202
|
clients_num = -1;
|
203
|
wifi_initialise(); //nvs and event group init
|
204
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
205
|
esp_netif = esp_netif_create_default_wifi_sta();
|
206
|
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
207
|
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
|
208
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
209
|
ESP_EVENT_ANY_ID,
|
210
|
&event_handler,
|
211
|
NULL,
|
212
|
NULL));
|
213
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
214
|
IP_EVENT_STA_GOT_IP,
|
215
|
&event_handler,
|
216
|
NULL,
|
217
|
NULL));
|
218
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
219
|
} else { // else already intialized, stop wifi to reconfigure
|
220
|
ESP_LOGI(WIFITAG,"WIFI is stopping to reinitialise to WIFI STA\r\n");
|
221
|
ESP_ERROR_CHECK(esp_wifi_stop());
|
222
|
clients_num = -1;
|
223
|
esp_netif = esp_netif_create_default_wifi_sta();
|
224
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
225
|
IP_EVENT_STA_GOT_IP,
|
226
|
&event_handler,
|
227
|
NULL,
|
228
|
NULL));
|
229
|
}
|
230
|
|
231
|
wifi_config_t wifi_config = {
|
232
|
.sta = {
|
233
|
//.ssid ="" ,
|
234
|
|
235
|
//.password = "",
|
236
|
},
|
237
|
};
|
238
|
|
239
|
strcpy((char *)wifi_config.sta.ssid, (const char *)ssid);
|
240
|
strcpy((char *)wifi_config.sta.password, (const char *)pass);
|
241
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
242
|
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
243
|
ESP_ERROR_CHECK(esp_wifi_start());
|
244
|
esp_netif_set_hostname(esp_netif, "esp32");
|
245
|
WIFI_INIT = 1;
|
246
|
}
|
247
|
|
248
|
int prev_trig_in_stat_WIFISetup = 0;
|
249
|
|
250
|
/* [EOF] */
|