# HG changeset patch # User Michiel Broek # Date 1558172091 -7200 # Node ID 4ec04c6f15512f679e70645f1b4ac41e5f6a7b0f # Parent edbd879492047c3d6304ea776944f993cc2f46b0 SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org. diff -r edbd87949204 -r 4ec04c6f1551 main/config.h --- a/main/config.h Fri May 17 15:43:31 2019 +0200 +++ b/main/config.h Sat May 18 11:34:51 2019 +0200 @@ -171,7 +171,7 @@ bool AskIodine; ///< Ask to confirm the iodine test. uint8_t IodineTime; ///< Continue after this time if not confirmed. int EquipmentRec; ///< Equipment record number. - char obsolete1[32]; ///< Obsolete. + char ntp_server[32]; ///< Preffered NTP server. char hostname[32]; ///< Our hostname. char ap_ssid[32]; ///< AP SSID. char ap_pwd[64]; ///< AP password. diff -r edbd87949204 -r 4ec04c6f1551 main/setup.c --- a/main/setup.c Fri May 17 15:43:31 2019 +0200 +++ b/main/setup.c Sat May 18 11:34:51 2019 +0200 @@ -55,6 +55,7 @@ ShowText(2, 140, "AP pwd", config.ap_pwd); ShowInteger(2, 156, "AP kanaal", NULL, config.ap_channel); ShowBool(2, 172, "AP SSID verborgen", config.ap_ssid_hidden); + ShowText(2, 188, "NTP server", config.ntp_server); Buttons_Clear(); Buttons_Add( 0, 210, 45, 30, "Ok" , 0); Buttons_Add(276, 210, 45, 30, "Ed" , 1); @@ -153,6 +154,7 @@ bool hidden = config.ap_ssid_hidden; EditBool("AP SSID verborgen", &hidden); config.ap_ssid_hidden = (uint8_t)hidden; + EditText("Voorkeur NTP server", config.ntp_server, 31); crc2 = crc32_le(0, dst, sizeof(config)); if ((crc1 != crc2) && Confirm("Gewijzigd, opslaan?", "Ja", "Nee")) { diff -r edbd87949204 -r 4ec04c6f1551 main/task_tft.c --- a/main/task_tft.c Fri May 17 15:43:31 2019 +0200 +++ b/main/task_tft.c Sat May 18 11:34:51 2019 +0200 @@ -510,17 +510,12 @@ } /* - * Timekeeping. In the WiFi task sntp is started if there - * is a valid internet connection. + * Timekeeping. + * In the WiFi task sntp is started and sets System_TimeOk if the + * clock is synced once. */ time(&now); localtime_r(&now, &timeinfo); - // Is time set? If not, tm_year will be (1970 - 1900). - if ((timeinfo.tm_year > (2016 - 1900)) && (! System_TimeOk)) { - System_TimeOk = true; - strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); - ESP_LOGI(TAG, "System time is set: %s", strftime_buf); - } if (Old_Screen != Main_Screen) { diff -r edbd87949204 -r 4ec04c6f1551 main/task_wifi.c --- 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; diff -r edbd87949204 -r 4ec04c6f1551 sdkconfig --- a/sdkconfig Fri May 17 15:43:31 2019 +0200 +++ b/sdkconfig Sat May 18 11:34:51 2019 +0200 @@ -346,7 +346,7 @@ CONFIG_LWIP_SO_REUSE=y CONFIG_LWIP_SO_REUSE_RXTOALL=y # CONFIG_LWIP_SO_RCVBUF is not set -CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1 +CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=3 # CONFIG_LWIP_IP_FRAG is not set # CONFIG_LWIP_IP_REASSEMBLY is not set # CONFIG_LWIP_STATS is not set @@ -357,7 +357,7 @@ CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y # CONFIG_LWIP_DHCP_RESTORE_LAST_IP is not set CONFIG_LWIP_DHCPS_LEASE_UNIT=60 -CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 +CONFIG_LWIP_DHCPS_MAX_STATION_NUM=4 # CONFIG_LWIP_AUTOIP is not set CONFIG_LWIP_NETIF_LOOPBACK=y CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8