main/task_wifi.c

branch
stable
changeset 58
7ce410c79048
parent 56
756d1a63d129
child 61
c7b8a9931b59
equal deleted inserted replaced
46:aaa095986ede 58:7ce410c79048
16 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. 16 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter.
17 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. 17 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results.
18 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. 18 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration.
19 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. 19 WIFI_State *wifi_state = NULL; ///< Public state for other tasks.
20 20
21 wifi_scan_config_t scan_config = { ///< WiFi scanner configuration.
22 .ssid = 0,
23 .bssid = 0,
24 .channel = 0,
25 .show_hidden = false
26 };
21 27
22 uint8_t _wifi_ssid[33]; ///< Current SSID 28 uint8_t _wifi_ssid[33]; ///< Current SSID
23 bool _wifi_ScanAPs = false; ///< Scanning 29 bool _wifi_ScanAPs = false; ///< Scanning
24 bool _wifi_ScanDone = false; ///< Scan ready 30 bool _wifi_ScanDone = false; ///< Scan ready
25 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. 31 uint16_t _wifi_Scanned = 0; ///< Total scanned APs.
26 32
27 extern int Main_Screen; ///< Current Screen number. 33 extern int Main_Screen; ///< Current Screen number.
28 extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions. 34 extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions.
29 extern uint32_t TimeSpent; ///< Counter that is increased each second. 35 extern uint32_t TimeSpent; ///< Counter that is increased each second.
36 extern bool System_TimeOk;
37 extern time_t now;
38 extern char strftime_buf[64];
39 extern struct tm timeinfo;
30 40
31 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. 41 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks.
32 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. 42 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP.
33 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. 43 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point.
34 44
56 * @param ctx Context 66 * @param ctx Context
57 * @param event The event 67 * @param event The event
58 * @return Esp error code. 68 * @return Esp error code.
59 */ 69 */
60 esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event); 70 esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event);
71
72 /**
73 * @brief local callback function. Is called when the timesync is valid
74 * from a timeserver. Sets the global boolean System_TimeOk value.
75 * @param tv is the received time. Not used.
76 */
77 void time_sync_notification_cb(struct timeval *tv);
78
79 /**
80 * @brief Array with AP security names
81 */
82 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" };
61 83
62 84
63 /****************************************************************************/ 85 /****************************************************************************/
64 86
65 87
82 */ 104 */
83 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { 105 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) {
84 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); 106 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid);
85 write_config(); 107 write_config();
86 } 108 }
87 ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); 109 // ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record);
88 } 110 }
89 111
90 return ESP_OK; 112 return ESP_OK;
91 } 113 }
92 114
100 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); 122 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
101 123
102 /* 124 /*
103 * Search last connected AP as station. 125 * Search last connected AP as station.
104 */ 126 */
105 if (strlen(config.lastSSID)) { 127 if (strlen(config.lastSSID) && (read_station((uint8_t *)config.lastSSID) >= 0)) {
106 read_station((uint8_t *)config.lastSSID);
107 128
108 /* ssid */ 129 /* ssid */
109 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); 130 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid);
110 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); 131 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz);
111 132
132 _wifi_Scanned = ap_num; 153 _wifi_Scanned = ap_num;
133 _wifi_ScanDone = true; 154 _wifi_ScanDone = true;
134 break; 155 break;
135 156
136 case SYSTEM_EVENT_STA_START: 157 case SYSTEM_EVENT_STA_START:
137 ESP_LOGD(TAG, "Event STA started");
138 // Set the configured hostname for the dhcp client. 158 // Set the configured hostname for the dhcp client.
139 ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname)); 159 ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname));
140 esp_wifi_connect(); 160 esp_wifi_connect();
141 break; 161 break;
142 162
143 // SYSTEM_EVENT_STA_STOP 3 163 // SYSTEM_EVENT_STA_STOP 3
144 case SYSTEM_EVENT_STA_CONNECTED: 164 case SYSTEM_EVENT_STA_CONNECTED: {
145 ESP_LOGI(TAG, "Event STA connected"); 165 const system_event_sta_connected_t *connected = &event->event_info.connected;
166 ESP_LOGI(TAG, "Event STA connected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", channel:%d, authmode:%s",
167 connected->ssid, connected->ssid_len, MAC2STR(connected->bssid), connected->channel, apsec[connected->authmode]);
146 wifi_ap_record_t ap_info; 168 wifi_ap_record_t ap_info;
147 esp_wifi_sta_get_ap_info(&ap_info); 169 esp_wifi_sta_get_ap_info(&ap_info);
148 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 170 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
149 wifi_state->STA_connected = true; 171 wifi_state->STA_connected = true;
150 wifi_state->STA_rssi = ap_info.rssi; 172 wifi_state->STA_rssi = ap_info.rssi;
152 xSemaphoreGive(xSemaphoreWiFi); 174 xSemaphoreGive(xSemaphoreWiFi);
153 } 175 }
154 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 176 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
155 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 177 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
156 break; 178 break;
157 179 }
158 case SYSTEM_EVENT_STA_DISCONNECTED: 180 case SYSTEM_EVENT_STA_DISCONNECTED: {
159 ESP_LOGI(TAG, "Event STA disconnected"); 181 const system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected;
182 wifi_ap_record_t ap;
183
184 ESP_LOGI(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d",
185 disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason);
160 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { 186 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) {
161 wifi_state->STA_connected = false; 187 wifi_state->STA_connected = false;
162 wifi_state->STA_online = false; 188 wifi_state->STA_online = false;
163 wifi_state->STA_rssi = 0; 189 wifi_state->STA_rssi = 0;
164 xSemaphoreGive(xSemaphoreWiFi); 190 xSemaphoreGive(xSemaphoreWiFi);
165 } 191 }
166 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 192 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
167 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 193 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
194 sntp_stop();
195
196 if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && ! _wifi_ScanAPs) {
197 ESP_LOGI(TAG, "Request scan for another AP");
198 _wifi_ScanAPs = true;
199 _wifi_ScanDone = false;
200 ap_num = MAX_AP_NUM;
201 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
202 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Keep looping active.
203 break;
204 }
205 if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && _wifi_ScanAPs && _wifi_ScanDone) {
206 // ESP_LOGI(TAG, "Scan completed, look for another AP, found:%d", _wifi_Scanned);
207 _wifi_ScanAPs = false;
208 _wifi_ScanDone = false;
209 for (int i = 0; i < _wifi_Scanned; i++) {
210 ap = accessp_records[i];
211 if ((read_station(ap.ssid) != -1)) {
212 if (wifiStation.hide) {
213 continue; // Blacklisted.
214 }
215 /* We know this one */
216 wifi_config_t* config = task_wifi_ConfigSTA;
217 memset(config, 0x00, sizeof(wifi_config_t));
218 memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID));
219 memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password));
220 // ESP_LOGI(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password);
221 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
222 break;
223 }
224 }
225 break;
226 }
168 227
169 /* 228 /*
170 * Meanwhile, try to reconnect. 229 * Reconnect previous AP.
171 */ 230 */
172 if (FetchStaConfig()) { 231 if (FetchStaConfig()) {
173 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); 232 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
174 } 233 }
175 break; 234 break;
176 235 }
177 // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6 236 // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6
178 case SYSTEM_EVENT_STA_GOT_IP: 237 case SYSTEM_EVENT_STA_GOT_IP:
179 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); 238 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
180 tcpip_adapter_ip_info_t ip; 239 tcpip_adapter_ip_info_t ip;
181 memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); 240 memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
182 if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0) { 241 if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0) {
183 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { 242 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) {
184 wifi_state->STA_online = true; 243 wifi_state->STA_online = true;
185 snprintf(wifi_state->STA_ip, 15, IPSTR, IP2STR(&ip.ip)); 244 snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&ip.ip));
186 snprintf(wifi_state->STA_nm, 15, IPSTR, IP2STR(&ip.netmask)); 245 snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&ip.netmask));
187 snprintf(wifi_state->STA_gw, 15, IPSTR, IP2STR(&ip.gw)); 246 snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&ip.gw));
188 xSemaphoreGive(xSemaphoreWiFi); 247 xSemaphoreGive(xSemaphoreWiFi);
189 } 248 }
190 } 249 }
250 /*
251 * There doesn't seem to be support for configuring NTP via DHCP so
252 * we need to hardcode the ntp servers. The preffered server can be
253 * set via the setup screen. It should be on your LAN, else leave it
254 * empty. And if you are on a different lan someday, there is no extra
255 * delay because the hostname will not be found.
256 */
257 sntp_stop();
258 if (strlen(config.ntp_server))
259 sntp_setservername(0, config.ntp_server);
260 sntp_setservername(1, (char *)"pool.ntp.org"); // Will get you servers nearby
261 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
262 sntp_set_time_sync_notification_cb(time_sync_notification_cb);
263 sntp_init();
264 #if 0
265 if (strlen(config.ntp_server))
266 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0));
267 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1));
268 #endif
191 break; 269 break;
192 270
193 case SYSTEM_EVENT_STA_LOST_IP: 271 case SYSTEM_EVENT_STA_LOST_IP:
194 ESP_LOGI(TAG, "Lost IP address"); 272 ESP_LOGI(TAG, "Lost IP address");
195 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); 273 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
198 wifi_state->STA_nm[0] = '\0'; 276 wifi_state->STA_nm[0] = '\0';
199 wifi_state->STA_gw[0] = '\0'; 277 wifi_state->STA_gw[0] = '\0';
200 wifi_state->STA_online = false; 278 wifi_state->STA_online = false;
201 xSemaphoreGive(xSemaphoreWiFi); 279 xSemaphoreGive(xSemaphoreWiFi);
202 } 280 }
281 sntp_stop();
203 break; 282 break;
204 283
205 // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9 284 // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9
206 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10 285 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10
207 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11 286 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11
208 // SYSTEM_EVENT_STA_WPS_ER_PIN 12 287 // SYSTEM_EVENT_STA_WPS_ER_PIN 12
209 288
210 case SYSTEM_EVENT_AP_START: 289 case SYSTEM_EVENT_AP_START:
211 ESP_LOGD(TAG, "Event AP started");
212 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); 290 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
213 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 291 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
214 wifi_state->AP_active = true; 292 wifi_state->AP_active = true;
215 wifi_state->AP_clients = 0; 293 wifi_state->AP_clients = 0;
216 xSemaphoreGive(xSemaphoreWiFi); 294 xSemaphoreGive(xSemaphoreWiFi);
217 } 295 }
218 break; 296 break;
219 297
220 case SYSTEM_EVENT_AP_STOP: 298 case SYSTEM_EVENT_AP_STOP:
221 ESP_LOGD(TAG, "Event AP stopped");
222 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); 299 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
223 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 300 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
224 wifi_state->AP_active = false; 301 wifi_state->AP_active = false;
225 wifi_state->AP_clients = 0; 302 wifi_state->AP_clients = 0;
226 xSemaphoreGive(xSemaphoreWiFi); 303 xSemaphoreGive(xSemaphoreWiFi);
227 } 304 }
228 break; 305 break;
229 306
230 case SYSTEM_EVENT_AP_STACONNECTED: 307 case SYSTEM_EVENT_AP_STACONNECTED: {
231 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 308 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
232 wifi_state->AP_clients++; 309 wifi_state->AP_clients++;
233 xSemaphoreGive(xSemaphoreWiFi); 310 xSemaphoreGive(xSemaphoreWiFi);
234 } 311 }
235 ESP_LOGI(TAG, "Event AP new client, %d connections", wifi_state->AP_clients); 312 const system_event_ap_staconnected_t *staconnected = &event->event_info.sta_connected;
236 break; 313 ESP_LOGI(TAG, "Event AP connected, mac:" MACSTR ", aid:%d, conns:%d",
237 314 MAC2STR(staconnected->mac), staconnected->aid, wifi_state->AP_clients);
238 case SYSTEM_EVENT_AP_STADISCONNECTED: 315 break;
316 }
317 case SYSTEM_EVENT_AP_STADISCONNECTED: {
239 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 318 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
240 if (wifi_state->AP_clients > 0) 319 if (wifi_state->AP_clients > 0)
241 wifi_state->AP_clients--; 320 wifi_state->AP_clients--;
242 else 321 else
243 wifi_state->AP_clients = 0; 322 wifi_state->AP_clients = 0;
244 xSemaphoreGive(xSemaphoreWiFi); 323 xSemaphoreGive(xSemaphoreWiFi);
245 } 324 }
246 ESP_LOGI(TAG, "Event AP client disconnect, %d connections", wifi_state->AP_clients); 325 const system_event_ap_stadisconnected_t *stadisconnected = &event->event_info.sta_disconnected;
247 break; 326 ESP_LOGI(TAG, "Event AP disconnected, mac:" MACSTR ", aid:%d, conns:%d",
248 327 MAC2STR(stadisconnected->mac), stadisconnected->aid, wifi_state->AP_clients);
249 // SYSTEM_EVENT_AP_PROBEREQRECVED 17 328 break;
250 // SYSTEM_EVENT_GOT_IP6 18 329 }
251 // SYSTEM_EVENT_ETH_START 19 330 case SYSTEM_EVENT_AP_STAIPASSIGNED:
252 // SYSTEM_EVENT_ETH_STOP 20 331 break;
253 // SYSTEM_EVENT_ETH_CONNECTED 21 332
254 // SYSTEM_EVENT_ETH_DISCONNECTED 22 333 // SYSTEM_EVENT_AP_PROBEREQRECVED 18
255 // SYSTEM_EVENT_ETH_GOT_IP 23 334 // SYSTEM_EVENT_GOT_IP6 19
335 // SYSTEM_EVENT_ETH_START 20
336 // SYSTEM_EVENT_ETH_STOP 21
337 // SYSTEM_EVENT_ETH_CONNECTED 22
338 // SYSTEM_EVENT_ETH_DISCONNECTED 23
339 // SYSTEM_EVENT_ETH_GOT_IP 24
256 340
257 default: 341 default:
258 printf("Unknown event %d\n", event->event_id); 342 printf("Unknown event %d\n", event->event_id);
259 break; 343 break;
260 } 344 }
261 return ESP_OK; 345 return ESP_OK;
346 }
347
348
349
350 void time_sync_notification_cb(struct timeval *tv)
351 {
352 int rc = sntp_get_sync_status();
353
354 if (rc == SNTP_SYNC_STATUS_COMPLETED) {
355 time(&now);
356 localtime_r(&now, &timeinfo);
357 System_TimeOk = true;
358 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
359 ESP_LOGI(TAG, "NTP time is set: %s", strftime_buf);
360 } else {
361 ESP_LOGI(TAG, "NTP unknown time sync event rc=%d", rc);
362 }
262 } 363 }
263 364
264 365
265 366
266 void task_wifi( void * pvParameters ) 367 void task_wifi( void * pvParameters )
297 wifi_state->AP_clients = 0; 398 wifi_state->AP_clients = 0;
298 wifi_state->AP_active = false; 399 wifi_state->AP_active = false;
299 wifi_state->STA_connected = false; 400 wifi_state->STA_connected = false;
300 wifi_state->STA_online = false; 401 wifi_state->STA_online = false;
301 wifi_state->STA_rssi = 0; 402 wifi_state->STA_rssi = 0;
302
303 /* wifi scanner config */
304 wifi_scan_config_t scan_config = {
305 .ssid = 0,
306 .bssid = 0,
307 .channel = 0,
308 .show_hidden = false
309 };
310 403
311 /* 404 /*
312 * start the softAP access point 405 * start the softAP access point
313 * stop DHCP server 406 * stop DHCP server
314 */ 407 */
328 ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); 421 ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
329 ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); 422 ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1");
330 423
331 /* start dhcp client */ 424 /* start dhcp client */
332 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); 425 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
333 ESP_LOGI(TAG, "Start DHCP client for STA interface.");
334 426
335 /* 427 /*
336 * init wifi as station + access point 428 * init wifi as station + access point
337 */ 429 */
338 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); 430 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
358 if (ret != ESP_OK) { 450 if (ret != ESP_OK) {
359 ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); 451 ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret);
360 } 452 }
361 ESP_ERROR_CHECK(esp_wifi_start()); 453 ESP_ERROR_CHECK(esp_wifi_start());
362 454
363 ESP_LOGI(TAG, "SoftAP start ssid: `%s' pwd: `%s' channel: %d, hidden: %s", 455 ESP_LOGI(TAG, "AP start ssid:`%s' pwd:`%s' channel:%d, hidden:%s",
364 ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); 456 ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no");
365 457
366 /* 458 /*
367 * try to get access to previously saved wifi 459 * try to get access to previously saved wifi
368 */ 460 */
386 478
387 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { 479 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
388 /* 480 /*
389 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory 481 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory
390 */ 482 */
391 ESP_LOGI(TAG, "Request disconnect"); 483 ESP_LOGI(TAG, "Request STA disconnect");
392 sntp_stop(); 484 sntp_stop();
393 ESP_ERROR_CHECK(esp_wifi_disconnect()); 485 ESP_ERROR_CHECK(esp_wifi_disconnect());
394 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); 486 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
395 487
396 /* 488 /*
402 config.lastSSID[0] = '\0'; 494 config.lastSSID[0] = '\0';
403 write_config(); 495 write_config();
404 496
405 /* finally: release the scan request bit */ 497 /* finally: release the scan request bit */
406 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 498 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
407 ESP_LOGI(TAG, "Request disconnect is finished.");
408 499
409 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { 500 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
410 501
411 //someone requested a connection! 502 //someone requested a connection!
412 ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); 503 ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid);
419 ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); 510 ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror);
420 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); 511 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
421 } 512 }
422 513
423 /* 514 /*
424 * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted 515 * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted
425 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. 516 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code.
517 * Or, option 3, the 5 seconds timeout is reached. This happens when the AP is not in range.
426 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. 518 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure.
427 */ 519 */
428 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, portMAX_DELAY ); 520 // ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED);
429 521 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS);
430 if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { 522 // ESP_LOGI(TAG, "2 waitbits %08x", uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED));
523
524 if (uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)) {
431 /* 525 /*
432 * only save the config if the connection was successful! 526 * only save the config if the connection was successful!
433 */ 527 */
434 if(uxBits & TASK_WIFI_HAS_IP) { 528 if (uxBits & TASK_WIFI_STA_CONNECTED) {
435 /* save wifi config */ 529 /* save wifi config */
436 SaveStaConfig(); 530 SaveStaConfig();
437 sntp_setservername(0, "nl.pool.ntp.org");
438 sntp_init();
439 ESP_LOGI(TAG, "Initialized SNTP %s", sntp_getservername(0));
440 } else { 531 } else {
441 ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. 532 ESP_LOGI(TAG, "Connection failed");
442 /* failed attempt to connect regardles of the reason */ 533 /* failed attempt to connect regardles of the reason */
443 534
444 /* otherwise: reset the config */ 535 /* otherwise: reset the config */
445 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); 536 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
446 } 537 }
538 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
447 } else { 539 } else {
448 /* hit portMAX_DELAY limit ? */ 540 /* hit 10 seconds timeout */
449 abort(); 541 ESP_LOGI(TAG, "Connection timeout");
542 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
543 vTaskDelay(100 / portTICK_PERIOD_MS);
544 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
450 } 545 }
451
452 /* finally: release the connection request bit */
453 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
454 546
455 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { 547 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) {
456 548
457 /* safe guard against overflow */ 549 /* safe guard against overflow */
458 ap_num = MAX_AP_NUM; 550 ap_num = MAX_AP_NUM;
460 552
461 /* finally: release the scan request bit */ 553 /* finally: release the scan request bit */
462 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); 554 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN);
463 } 555 }
464 556
465 /*
466 * Here we should check for reconnect actions.
467 */
468
469 } /* for(;;) */ 557 } /* for(;;) */
470 vTaskDelay( (TickType_t)10); 558 vTaskDelay( (TickType_t)10);
471 } 559 }
472 560
473
474 /**
475 * @brief Array with AP security names
476 */
477 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" };
478 561
479 562
480 /** 563 /**
481 * @brief Show an AP station as a button. The buttons are already defined. 564 * @brief Show an AP station as a button. The buttons are already defined.
482 * @param idx The index position on the display, 1 to 7. 565 * @param idx The index position on the display, 1 to 7.
521 604
522 605
523 606
524 bool WiFi_Init(void) 607 bool WiFi_Init(void)
525 { 608 {
526 char pwd[65], pmpt[32]; 609 char pwd[65], pmpt[96];
527 610
528 switch (Main_Screen) { 611 switch (Main_Screen) {
529 case MAIN_TOOLS_SETUP_WIFI: 612 case MAIN_TOOLS_SETUP_WIFI:
530 TopMessage("WiFi"); 613 TopMessage((char *)"WiFi");
531 TFT_setFont(DEJAVU24_FONT, NULL); 614 TFT_setFont(DEJAVU24_FONT, NULL);
532 _fg = TFT_WHITE; 615 _fg = TFT_WHITE;
533 TFT_print("Momentje ..", CENTER, CENTER); 616 TFT_print((char *)"Momentje ..", CENTER, CENTER);
534 _wifi_ScanAPs = true; 617 _wifi_ScanAPs = true;
535 _wifi_ScanDone = false; 618 _wifi_ScanDone = false;
536 Buttons_Add(260, 200, 60, 40, "Ok", 0); 619 Buttons_Add(260, 200, 60, 40, (char *)"Ok", 0);
537 Buttons[0].dark = true; 620 Buttons[0].dark = true;
538 Buttons_Show(); 621 Buttons_Show();
539 // Now add the buttons we draw manually. 622 // Now add the buttons we draw manually.
540 Buttons_Add( 0, 30, 250, 30, "", 1); 623 Buttons_Add( 0, 30, 250, 30, (char *)"", 1);
541 Buttons_Add( 0, 60, 250, 30, "", 2); 624 Buttons_Add( 0, 60, 250, 30, (char *)"", 2);
542 Buttons_Add( 0, 90, 250, 30, "", 3); 625 Buttons_Add( 0, 90, 250, 30, (char *)"", 3);
543 Buttons_Add( 0,120, 250, 30, "", 4); 626 Buttons_Add( 0,120, 250, 30, (char *)"", 4);
544 Buttons_Add( 0,150, 250, 30, "", 5); 627 Buttons_Add( 0,150, 250, 30, (char *)"", 5);
545 Buttons_Add( 0,180, 250, 30, "", 6); 628 Buttons_Add( 0,180, 250, 30, (char *)"", 6);
546 Buttons_Add( 0,210, 250, 30, "", 7); 629 Buttons_Add( 0,210, 250, 30, (char *)"", 7);
547 break; 630 break;
548 631
549 case MAIN_TOOLS_SETUP_WIFI_CUR: 632 case MAIN_TOOLS_SETUP_WIFI_CUR:
550 TopMessage("WiFi verbinding"); 633 TopMessage((char *)"WiFi verbinding");
551 // Get extra information. 634 // Get extra information.
552 wifi_ap_record_t ap_info; 635 wifi_ap_record_t ap_info;
553 esp_wifi_sta_get_ap_info(&ap_info); 636 esp_wifi_sta_get_ap_info(&ap_info);
554 637
555 wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; 638 wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */;
563 strcpy(ip, ip4addr_ntoa(&ip_info.ip)); 646 strcpy(ip, ip4addr_ntoa(&ip_info.ip));
564 strcpy(netmask, ip4addr_ntoa(&ip_info.netmask)); 647 strcpy(netmask, ip4addr_ntoa(&ip_info.netmask));
565 strcpy(gw, ip4addr_ntoa(&ip_info.gw)); 648 strcpy(gw, ip4addr_ntoa(&ip_info.gw));
566 TFT_setFont(DEFAULT_FONT, NULL); 649 TFT_setFont(DEFAULT_FONT, NULL);
567 _fg = TFT_WHITE; 650 _fg = TFT_WHITE;
568 TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 40); 651 TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 40);
569 TFT_print("Kanaal", 155 - TFT_getStringWidth("Kanaal"), 60); 652 TFT_print((char *)"Kanaal", 155 - TFT_getStringWidth((char *)"Kanaal"), 60);
570 TFT_print("Rssi", 155 - TFT_getStringWidth("Rssi"), 80); 653 TFT_print((char *)"Rssi", 155 - TFT_getStringWidth((char *)"Rssi"), 80);
571 TFT_print("Mode", 155 - TFT_getStringWidth("Mode"), 100); 654 TFT_print((char *)"Mode", 155 - TFT_getStringWidth((char *)"Mode"), 100);
572 TFT_print("IP adres", 155 - TFT_getStringWidth("IP adres"), 120); 655 TFT_print((char *)"IP adres", 155 - TFT_getStringWidth((char *)"IP adres"), 120);
573 TFT_print("Netmask", 155 - TFT_getStringWidth("Netmask"), 140); 656 TFT_print((char *)"Netmask", 155 - TFT_getStringWidth((char *)"Netmask"), 140);
574 TFT_print("Gateway", 155 - TFT_getStringWidth("Gateway"), 160); 657 TFT_print((char *)"Gateway", 155 - TFT_getStringWidth((char *)"Gateway"), 160);
575 _fg = TFT_YELLOW; 658 _fg = TFT_YELLOW;
576 TFT_print((char*)wconfig->sta.ssid, 165, 40); 659 TFT_print((char*)wconfig->sta.ssid, 165, 40);
577 sprintf(pmpt, "%d", ap_info.primary); 660 sprintf(pmpt, "%d", ap_info.primary);
578 TFT_print(pmpt, 165, 60); 661 TFT_print(pmpt, 165, 60);
579 sprintf(pmpt, "%d", ap_info.rssi); 662 sprintf(pmpt, "%d", ap_info.rssi);
582 TFT_print(pmpt, 165, 100); 665 TFT_print(pmpt, 165, 100);
583 TFT_print((char*)ip, 165, 120); 666 TFT_print((char*)ip, 165, 120);
584 TFT_print((char*)netmask, 165, 140); 667 TFT_print((char*)netmask, 165, 140);
585 TFT_print((char*)gw, 165, 160); 668 TFT_print((char*)gw, 165, 160);
586 } 669 }
587 Buttons_Add(130, 200, 60, 40, "Ok", 0); 670 Buttons_Add(130, 200, 60, 40, (char *)"Ok", 0);
588 Buttons[0].dark = true; 671 Buttons[0].dark = true;
589 Buttons_Show(); 672 Buttons_Show();
590 break; 673 break;
591 674
592 case MAIN_TOOLS_SETUP_WIFI_CON: 675 case MAIN_TOOLS_SETUP_WIFI_CON:
593 TopMessage("WiFi verbinden"); 676 TopMessage((char *)"WiFi verbinden");
594 TFT_setFont(DEJAVU18_FONT, NULL); 677 TFT_setFont(DEJAVU18_FONT, NULL);
595 _fg = TFT_WHITE; 678 _fg = TFT_WHITE;
596 TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 70); 679 TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 70);
597 _fg = TFT_YELLOW; 680 _fg = TFT_YELLOW;
598 TFT_print((char*)_wifi_ssid, 165, 70); 681 TFT_print((char*)_wifi_ssid, 165, 70);
599 Buttons_Add( 0, 200, 100, 40, "Annuleer", 0); 682 Buttons_Add( 0, 200, 100, 40, (char *)"Annuleer", 0);
600 Buttons_Add(110, 200, 100, 40, "Vergeet", 1); 683 Buttons_Add(110, 200, 100, 40, (char *)"Vergeet", 1);
601 Buttons_Add(220, 200, 100, 40, "Verbind", 2); 684 Buttons_Add(220, 200, 100, 40, (char *)"Verbind", 2);
602 Buttons_Show(); 685 Buttons_Show();
603 Buttons[0].dark = true; 686 Buttons[0].dark = true;
604 break; 687 break;
605 688
606 case MAIN_TOOLS_SETUP_WIFI_NEW: 689 case MAIN_TOOLS_SETUP_WIFI_NEW:
607 TopMessage("WiFi nieuw"); 690 TopMessage((char *)"WiFi nieuw");
608 snprintf(pmpt, 32, "Password for %s", _wifi_ssid); 691 snprintf(pmpt, 95, "Password for %s", _wifi_ssid);
609 pwd[0] = '\0'; 692 pwd[0] = '\0';
610 EditTextMin(pmpt, pwd, 64, 8); 693 EditTextMin(pmpt, pwd, 64, 8);
611 /* 694 /*
612 * Disconnect first 695 * Disconnect first
613 */ 696 */
614 _bg = TFT_BLACK; 697 _bg = TFT_BLACK;
615 TFT_fillScreen(_bg); 698 TFT_fillScreen(_bg);
616 TFT_setFont(DEJAVU24_FONT, NULL); 699 TFT_setFont(DEJAVU24_FONT, NULL);
617 _fg = TFT_WHITE; 700 _fg = TFT_WHITE;
618 TFT_print("Momentje ..", CENTER, CENTER); 701 TFT_print((char *)"Momentje ..", CENTER, CENTER);
619 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 702 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
620 vTaskDelay(100 / portTICK_PERIOD_MS); 703 vTaskDelay(100 / portTICK_PERIOD_MS);
621 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); 704 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
622 705
623 /* 706 /*
735 Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; 818 Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW;
736 } 819 }
737 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 820 ESP_ERROR_CHECK(esp_wifi_scan_stop());
738 } else if (Choice == 0) { 821 } else if (Choice == 0) {
739 Main_Screen = MAIN_TOOLS_SETUP; 822 Main_Screen = MAIN_TOOLS_SETUP;
823 _wifi_ScanAPs = false;
824 _wifi_ScanDone = false;
740 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 825 ESP_ERROR_CHECK(esp_wifi_scan_stop());
741 } 826 } else if (TimeSpent > 10) {
742 if (TimeSpent > 10) {
743 _wifi_ScanAPs = true; 827 _wifi_ScanAPs = true;
744 } 828 }
745 break; 829 break;
746 830
747 case MAIN_TOOLS_SETUP_WIFI_CUR: 831 case MAIN_TOOLS_SETUP_WIFI_CUR:
765 case 2: // Connect 849 case 2: // Connect
766 _bg = TFT_BLACK; 850 _bg = TFT_BLACK;
767 TFT_fillScreen(_bg); 851 TFT_fillScreen(_bg);
768 TFT_setFont(DEJAVU24_FONT, NULL); 852 TFT_setFont(DEJAVU24_FONT, NULL);
769 _fg = TFT_WHITE; 853 _fg = TFT_WHITE;
770 TFT_print("Momentje ..", CENTER, CENTER); 854 TFT_print((char *)"Momentje ..", CENTER, CENTER);
771 /* 855 /*
772 * Disconnect old connections and wait until it's gone. 856 * Disconnect old connections and wait until it's gone.
773 */ 857 */
774 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 858 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
775 vTaskDelay(100 / portTICK_PERIOD_MS); 859 vTaskDelay(100 / portTICK_PERIOD_MS);

mercurial