Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.

Sat, 18 May 2019 22:58:28 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 18 May 2019 22:58:28 +0200
changeset 51
0624a9a3ce75
parent 50
e62125be165c
child 52
4b5b28b0ad42

Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.

main/task_wifi.c file | annotate | diff | comparison | revisions
--- a/main/task_wifi.c	Sat May 18 11:35:28 2019 +0200
+++ b/main/task_wifi.c	Sat May 18 22:58:28 2019 +0200
@@ -113,8 +113,7 @@
     /*
      * Search last connected AP as station.
      */
-    if (strlen(config.lastSSID)) {
-	read_station((uint8_t *)config.lastSSID);
+    if (strlen(config.lastSSID) && (read_station((uint8_t *)config.lastSSID) >= 0)) {
 
 	/* ssid */
 	size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid);
@@ -202,12 +201,15 @@
 		}
 		/*
 		 * There doesn't seem to be support for configuring NTP via DHCP so
-		 * we need to hardcode the ntp servers.
+		 * we need to hardcode the ntp servers. The preffered server can be
+		 * set via the setup screen. It should be on your LAN, else leave it
+		 * empty. And if you are on a different lan someday, there is no extra
+		 * delay because the hostname will not be found. 
 		 */
 		sntp_stop();
 		if (strlen(config.ntp_server))
 		    sntp_setservername(0, config.ntp_server);
-		sntp_setservername(1, "nl.pool.ntp.org");
+		sntp_setservername(1, "pool.ntp.org"); // Will get you servers nearby
                 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
                 sntp_set_time_sync_notification_cb(time_sync_notification_cb);
                 sntp_init();
@@ -423,10 +425,12 @@
 
     for(;;) {
 
+//	ESP_LOGI(TAG, "1 wait for %08x", TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT);
 	/* actions that can trigger: request a connection, a scan, or a disconnection */
 	uxBits = xEventGroupWaitBits(xEventGroupWifi, 
 		                     TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT,
 		                     pdFALSE, pdFALSE, portMAX_DELAY );
+//	ESP_LOGI(TAG, "1 waitbits %08x", uxBits);
 
 	if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
 	    /*
@@ -469,7 +473,9 @@
 	     * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code.
 	     * Note that the reason code is not exploited. For all intent and purposes a failure is a failure.
 	     */
-	    uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, portMAX_DELAY );
+//	    ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED);
+	    uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 10000 / portTICK_PERIOD_MS);
+//	    ESP_LOGI(TAG, "2 waitbits %08x", uxBits);
 
 	    if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) {
 		/* 
@@ -485,16 +491,18 @@
 		    /* otherwise: reset the config */
 		    memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
 		}
+		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
 	    } else {
-		/* hit portMAX_DELAY limit ? */
-		abort();
+		/* hit 10 seconds timeout */
+		ESP_LOGI(TAG, "Connection timeout");
+		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
+                vTaskDelay(100 / portTICK_PERIOD_MS);
+		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
 	    }
 
-	    /* finally: release the connection request bit */
-	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
-
 	} else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) {
 
+	    ESP_LOGI(TAG, "Request WiFi scan");
 	    /* safe guard against overflow */
 	    ap_num = MAX_AP_NUM;
 	    ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
@@ -506,6 +514,7 @@
 	/*
 	 * Here we should check for reconnect actions.
 	 */
+//	ESP_LOGI(TAG, "check reconnect");
 
     } /* for(;;) */
     vTaskDelay( (TickType_t)10);

mercurial