main/task_wifi.c

changeset 49
4ec04c6f1551
parent 34
5c92103c5e72
child 51
0624a9a3ce75
--- a/main/task_wifi.c	Fri May 17 15:43:31 2019 +0200
+++ b/main/task_wifi.c	Sat May 18 11:34:51 2019 +0200
@@ -27,6 +27,10 @@
 extern int			Main_Screen;			///< Current Screen number.
 extern sButton			Buttons[MAXBUTTONS];		///< Buttons definitions.
 extern uint32_t			TimeSpent;			///< Counter that is increased each second.
+extern bool			System_TimeOk;
+extern time_t			now;
+extern char			strftime_buf[64];
+extern struct tm		timeinfo;
 
 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0;			///< When set, means a client requested to scan wireless networks.
 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1;		///< When set, means a client requested to disconnect from currently connected AP.
@@ -59,6 +63,13 @@
  */
 esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event);
 
+/**
+ * @brief local callback function. Is called when the timesync is valid
+ *        from a timeserver. Sets the global boolean System_TimeOk value. 
+ * @param tv is the received time. Not used.
+ */
+void time_sync_notification_cb(struct timeval *tv);
+
 
 /****************************************************************************/
 
@@ -165,6 +176,7 @@
 		}
 		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
 		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
+		sntp_stop();
 
 		/*
 		 * Meanwhile, try to reconnect.
@@ -188,6 +200,20 @@
 		    	xSemaphoreGive(xSemaphoreWiFi);
 		    }
 		}
+		/*
+		 * There doesn't seem to be support for configuring NTP via DHCP so
+		 * we need to hardcode the ntp servers.
+		 */
+		sntp_stop();
+		if (strlen(config.ntp_server))
+		    sntp_setservername(0, config.ntp_server);
+		sntp_setservername(1, "nl.pool.ntp.org");
+                sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
+                sntp_set_time_sync_notification_cb(time_sync_notification_cb);
+                sntp_init();
+		if (strlen(config.ntp_server))
+                    ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0));
+		ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1));
 		break;
 
 	case SYSTEM_EVENT_STA_LOST_IP:
@@ -200,6 +226,7 @@
 		    wifi_state->STA_online = false;
 		    xSemaphoreGive(xSemaphoreWiFi);
 		}
+		sntp_stop();
 		break;
 
 	//   SYSTEM_EVENT_STA_WPS_ER_SUCCESS	9
@@ -263,6 +290,23 @@
 
 
 
+void time_sync_notification_cb(struct timeval *tv)
+{
+    int rc = sntp_get_sync_status();
+
+    if (rc == SNTP_SYNC_STATUS_COMPLETED) {
+	time(&now);
+        localtime_r(&now, &timeinfo);
+	System_TimeOk = true;
+        strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
+        ESP_LOGI(TAG, "System time is set: %s", strftime_buf);
+    } else {
+    	ESP_LOGI(TAG, "Notification of unknown time synchronization event rc=%d", rc);
+    }
+}
+
+
+
 void task_wifi( void * pvParameters )
 {
     esp_err_t		ret;
@@ -388,7 +432,7 @@
 	    /*
 	     * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory
 	     */
-	    ESP_LOGI(TAG, "Request disconnect");
+	    ESP_LOGI(TAG, "Request STA disconnect");
 	    sntp_stop();
 	    ESP_ERROR_CHECK(esp_wifi_disconnect());
 	    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
@@ -434,9 +478,6 @@
 		if(uxBits & TASK_WIFI_HAS_IP) {
 		    /* save wifi config */
 		    SaveStaConfig();
-		    sntp_setservername(0, "nl.pool.ntp.org");
-		    sntp_init();
-		    ESP_LOGI(TAG, "Initialized SNTP %s", sntp_getservername(0));
 		} else {
 		    ESP_LOGI(TAG, "Connection failed");	// TODO: Scan other SSID's for known networks.
 		    /* failed attempt to connect regardles of the reason */
@@ -738,8 +779,7 @@
 			} else if (Choice == 0) {
 			    Main_Screen = MAIN_TOOLS_SETUP;
 			    ESP_ERROR_CHECK(esp_wifi_scan_stop());
-			}
-			if (TimeSpent > 10) {
+			} else if (TimeSpent > 10) {
 			     _wifi_ScanAPs = true;
 			}
 			break;

mercurial