main/task_wifi.c

changeset 49
4ec04c6f1551
parent 34
5c92103c5e72
child 51
0624a9a3ce75
equal deleted inserted replaced
48:edbd87949204 49:4ec04c6f1551
25 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. 25 uint16_t _wifi_Scanned = 0; ///< Total scanned APs.
26 26
27 extern int Main_Screen; ///< Current Screen number. 27 extern int Main_Screen; ///< Current Screen number.
28 extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions. 28 extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions.
29 extern uint32_t TimeSpent; ///< Counter that is increased each second. 29 extern uint32_t TimeSpent; ///< Counter that is increased each second.
30 extern bool System_TimeOk;
31 extern time_t now;
32 extern char strftime_buf[64];
33 extern struct tm timeinfo;
30 34
31 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. 35 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. 36 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. 37 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point.
34 38
56 * @param ctx Context 60 * @param ctx Context
57 * @param event The event 61 * @param event The event
58 * @return Esp error code. 62 * @return Esp error code.
59 */ 63 */
60 esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event); 64 esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event);
65
66 /**
67 * @brief local callback function. Is called when the timesync is valid
68 * from a timeserver. Sets the global boolean System_TimeOk value.
69 * @param tv is the received time. Not used.
70 */
71 void time_sync_notification_cb(struct timeval *tv);
61 72
62 73
63 /****************************************************************************/ 74 /****************************************************************************/
64 75
65 76
163 wifi_state->STA_rssi = 0; 174 wifi_state->STA_rssi = 0;
164 xSemaphoreGive(xSemaphoreWiFi); 175 xSemaphoreGive(xSemaphoreWiFi);
165 } 176 }
166 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 177 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
167 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 178 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
179 sntp_stop();
168 180
169 /* 181 /*
170 * Meanwhile, try to reconnect. 182 * Meanwhile, try to reconnect.
171 */ 183 */
172 if (FetchStaConfig()) { 184 if (FetchStaConfig()) {
186 snprintf(wifi_state->STA_nm, 15, IPSTR, IP2STR(&ip.netmask)); 198 snprintf(wifi_state->STA_nm, 15, IPSTR, IP2STR(&ip.netmask));
187 snprintf(wifi_state->STA_gw, 15, IPSTR, IP2STR(&ip.gw)); 199 snprintf(wifi_state->STA_gw, 15, IPSTR, IP2STR(&ip.gw));
188 xSemaphoreGive(xSemaphoreWiFi); 200 xSemaphoreGive(xSemaphoreWiFi);
189 } 201 }
190 } 202 }
203 /*
204 * There doesn't seem to be support for configuring NTP via DHCP so
205 * we need to hardcode the ntp servers.
206 */
207 sntp_stop();
208 if (strlen(config.ntp_server))
209 sntp_setservername(0, config.ntp_server);
210 sntp_setservername(1, "nl.pool.ntp.org");
211 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
212 sntp_set_time_sync_notification_cb(time_sync_notification_cb);
213 sntp_init();
214 if (strlen(config.ntp_server))
215 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0));
216 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1));
191 break; 217 break;
192 218
193 case SYSTEM_EVENT_STA_LOST_IP: 219 case SYSTEM_EVENT_STA_LOST_IP:
194 ESP_LOGI(TAG, "Lost IP address"); 220 ESP_LOGI(TAG, "Lost IP address");
195 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); 221 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
198 wifi_state->STA_nm[0] = '\0'; 224 wifi_state->STA_nm[0] = '\0';
199 wifi_state->STA_gw[0] = '\0'; 225 wifi_state->STA_gw[0] = '\0';
200 wifi_state->STA_online = false; 226 wifi_state->STA_online = false;
201 xSemaphoreGive(xSemaphoreWiFi); 227 xSemaphoreGive(xSemaphoreWiFi);
202 } 228 }
229 sntp_stop();
203 break; 230 break;
204 231
205 // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9 232 // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9
206 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10 233 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10
207 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11 234 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11
257 default: 284 default:
258 printf("Unknown event %d\n", event->event_id); 285 printf("Unknown event %d\n", event->event_id);
259 break; 286 break;
260 } 287 }
261 return ESP_OK; 288 return ESP_OK;
289 }
290
291
292
293 void time_sync_notification_cb(struct timeval *tv)
294 {
295 int rc = sntp_get_sync_status();
296
297 if (rc == SNTP_SYNC_STATUS_COMPLETED) {
298 time(&now);
299 localtime_r(&now, &timeinfo);
300 System_TimeOk = true;
301 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
302 ESP_LOGI(TAG, "System time is set: %s", strftime_buf);
303 } else {
304 ESP_LOGI(TAG, "Notification of unknown time synchronization event rc=%d", rc);
305 }
262 } 306 }
263 307
264 308
265 309
266 void task_wifi( void * pvParameters ) 310 void task_wifi( void * pvParameters )
386 430
387 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { 431 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
388 /* 432 /*
389 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory 433 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory
390 */ 434 */
391 ESP_LOGI(TAG, "Request disconnect"); 435 ESP_LOGI(TAG, "Request STA disconnect");
392 sntp_stop(); 436 sntp_stop();
393 ESP_ERROR_CHECK(esp_wifi_disconnect()); 437 ESP_ERROR_CHECK(esp_wifi_disconnect());
394 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); 438 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
395 439
396 /* 440 /*
432 * only save the config if the connection was successful! 476 * only save the config if the connection was successful!
433 */ 477 */
434 if(uxBits & TASK_WIFI_HAS_IP) { 478 if(uxBits & TASK_WIFI_HAS_IP) {
435 /* save wifi config */ 479 /* save wifi config */
436 SaveStaConfig(); 480 SaveStaConfig();
437 sntp_setservername(0, "nl.pool.ntp.org");
438 sntp_init();
439 ESP_LOGI(TAG, "Initialized SNTP %s", sntp_getservername(0));
440 } else { 481 } else {
441 ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. 482 ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks.
442 /* failed attempt to connect regardles of the reason */ 483 /* failed attempt to connect regardles of the reason */
443 484
444 /* otherwise: reset the config */ 485 /* otherwise: reset the config */
736 } 777 }
737 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 778 ESP_ERROR_CHECK(esp_wifi_scan_stop());
738 } else if (Choice == 0) { 779 } else if (Choice == 0) {
739 Main_Screen = MAIN_TOOLS_SETUP; 780 Main_Screen = MAIN_TOOLS_SETUP;
740 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 781 ESP_ERROR_CHECK(esp_wifi_scan_stop());
741 } 782 } else if (TimeSpent > 10) {
742 if (TimeSpent > 10) {
743 _wifi_ScanAPs = true; 783 _wifi_ScanAPs = true;
744 } 784 }
745 break; 785 break;
746 786
747 case MAIN_TOOLS_SETUP_WIFI_CUR: 787 case MAIN_TOOLS_SETUP_WIFI_CUR:

mercurial