Skip to content

Commit b544f25

Browse files
committed
Add NTP time sync
1 parent 9d2964d commit b544f25

6 files changed

Lines changed: 98 additions & 46 deletions

File tree

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ idf_component_register(
5757
"device_information.c"
5858
"filesystem_utils.c"
5959
"app_management.c"
60+
"ntp.c"
6061
PRIV_REQUIRES
6162
esp_lcd
6263
fatfs
@@ -78,6 +79,7 @@ idf_component_register(
7879
freertos
7980
badgelink
8081
tanmatsu-wifi
82+
lwip
8183
${extra_requires}
8284
INCLUDE_DIRS
8385
"."

main/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dependencies:
44
badgeteam/badge-bsp:
55
version: ">=0.3.2"
66
robotman2412/pax-gfx:
7-
version: ">=2.0.3"
7+
version: ">=2.0.4"
88
badgeteam/appfs:
99
version: ">=1.0.1"
1010
nicolaielectronics/rvswd:

main/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "coprocessor_management.h"
1717
#include "custom_certificates.h"
1818
#include "driver/gpio.h"
19+
#include "esp_err.h"
1920
#include "esp_lcd_panel_ops.h"
2021
#include "esp_lcd_types.h"
2122
#include "esp_log.h"
@@ -27,6 +28,7 @@
2728
#include "hal/lcd_types.h"
2829
#include "icons.h"
2930
#include "menu/home.h"
31+
#include "ntp.h"
3032
#include "nvs_flash.h"
3133
#include "pax_fonts.h"
3234
#include "pax_gfx.h"
@@ -108,6 +110,27 @@ static void wifi_task(void* pvParameters) {
108110
ESP_LOGE(TAG, "WiFi radio not responding, did you flash ESP-HOSTED firmware?");
109111
}
110112
wifi_stack_task_done = true;
113+
114+
if (ntp_get_enabled()) {
115+
if (wifi_connect_try_all() == ESP_OK) {
116+
esp_err_t res = ntp_start_service("pool.ntp.org");
117+
if (res == ESP_OK) {
118+
res = ntp_sync_wait();
119+
if (res != ESP_OK) {
120+
ESP_LOGW(TAG, "NTP time sync failed: %s", esp_err_to_name(res));
121+
} else {
122+
time_t rtc_time = time(NULL);
123+
bsp_rtc_set_time(rtc_time);
124+
ESP_LOGI(TAG, "NTP time sync succesful, RTC updated");
125+
}
126+
} else {
127+
ESP_LOGE(TAG, "Failed to initialize NTP service: %s", esp_err_to_name(res));
128+
}
129+
} else {
130+
ESP_LOGW(TAG, "Could not connect to network for NTP");
131+
}
132+
}
133+
111134
vTaskDelete(NULL);
112135
}
113136

main/menu/settings_clock.c

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gui_style.h"
1111
#include "icons.h"
1212
#include "menu/message_dialog.h"
13+
#include "ntp.h"
1314
#include "nvs.h"
1415
#include "pax_gfx.h"
1516
#include "pax_matrix.h"
@@ -18,7 +19,6 @@
1819
#include "sdkconfig.h"
1920
#include "settings_clock_timezone.h"
2021
#include "timezone.h"
21-
// #include "shapes/pax_misc.h"
2222

2323
static const char* TAG = "clock";
2424

@@ -163,48 +163,6 @@ void adjust_date_time(uint8_t selection, int8_t delta) {
163163
bsp_rtc_set_time(new_time);
164164
}
165165

166-
static bool get_ntp(void) {
167-
nvs_handle_t nvs_handle;
168-
esp_err_t res = nvs_open("system", NVS_READWRITE, &nvs_handle);
169-
if (res != ESP_OK) {
170-
ESP_LOGE(TAG, "Failed to open NVS namespace");
171-
return false;
172-
}
173-
uint8_t enabled = false;
174-
res = nvs_get_u8(nvs_handle, "ntp", &enabled);
175-
if (res != ESP_OK) {
176-
ESP_LOGE(TAG, "Failed to get NVS entry");
177-
nvs_close(nvs_handle);
178-
return false;
179-
}
180-
res = nvs_commit(nvs_handle);
181-
if (res != ESP_OK) {
182-
ESP_LOGE(TAG, "Failed to commit to NVS");
183-
}
184-
nvs_close(nvs_handle);
185-
return (enabled & 1);
186-
}
187-
188-
static void set_ntp(bool enabled) {
189-
nvs_handle_t nvs_handle;
190-
esp_err_t res = nvs_open("system", NVS_READWRITE, &nvs_handle);
191-
if (res != ESP_OK) {
192-
ESP_LOGE(TAG, "Failed to open NVS namespace");
193-
return;
194-
}
195-
res = nvs_set_u8(nvs_handle, "ntp", enabled ? 1 : 0);
196-
if (res != ESP_OK) {
197-
ESP_LOGE(TAG, "Failed to set NVS entry");
198-
nvs_close(nvs_handle);
199-
return;
200-
}
201-
res = nvs_commit(nvs_handle);
202-
if (res != ESP_OK) {
203-
ESP_LOGE(TAG, "Failed to commit to NVS");
204-
}
205-
nvs_close(nvs_handle);
206-
}
207-
208166
static void get_timezone(const timezone_t** zone) {
209167
char timezone_name[32] = {0};
210168
timezone_nvs_get("system", "timezone", timezone_name, sizeof(timezone_name));
@@ -229,7 +187,7 @@ void menu_settings_clock(pax_buf_t* buffer, gui_theme_t* theme) {
229187

230188
get_timezone(&zone);
231189

232-
bool ntp = get_ntp();
190+
bool ntp = ntp_get_enabled();
233191

234192
uint8_t selection = 0;
235193
render(buffer, theme, position, zone, ntp, false, true, selection);
@@ -275,7 +233,7 @@ void menu_settings_clock(pax_buf_t* buffer, gui_theme_t* theme) {
275233
case BSP_INPUT_NAVIGATION_KEY_SELECT:
276234
// Toggle NTP
277235
ntp = !ntp;
278-
set_ntp(ntp);
236+
ntp_set_enabled(ntp);
279237
render(buffer, theme, position, zone, ntp, false, true, selection);
280238
break;
281239
default:

main/ntp.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "ntp.h"
2+
#include "esp_err.h"
3+
#include "esp_log.h"
4+
#include "esp_netif_sntp.h"
5+
#include "nvs.h"
6+
7+
static const char TAG[] = "NTP";
8+
9+
esp_err_t ntp_start_service(const char* server) {
10+
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG(server);
11+
return esp_netif_sntp_init(&config);
12+
}
13+
14+
esp_err_t ntp_sync_wait(void) {
15+
return esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000));
16+
}
17+
18+
// Settings
19+
20+
bool ntp_get_enabled(void) {
21+
nvs_handle_t nvs_handle;
22+
esp_err_t res = nvs_open("system", NVS_READWRITE, &nvs_handle);
23+
if (res != ESP_OK) {
24+
ESP_LOGE(TAG, "Failed to open NVS namespace");
25+
return false;
26+
}
27+
uint8_t enabled = false;
28+
res = nvs_get_u8(nvs_handle, "ntp", &enabled);
29+
if (res != ESP_OK) {
30+
ESP_LOGE(TAG, "Failed to get NVS entry");
31+
nvs_close(nvs_handle);
32+
return false;
33+
}
34+
res = nvs_commit(nvs_handle);
35+
if (res != ESP_OK) {
36+
ESP_LOGE(TAG, "Failed to commit to NVS");
37+
}
38+
nvs_close(nvs_handle);
39+
return (enabled & 1);
40+
}
41+
42+
void ntp_set_enabled(bool enabled) {
43+
nvs_handle_t nvs_handle;
44+
esp_err_t res = nvs_open("system", NVS_READWRITE, &nvs_handle);
45+
if (res != ESP_OK) {
46+
ESP_LOGE(TAG, "Failed to open NVS namespace");
47+
return;
48+
}
49+
res = nvs_set_u8(nvs_handle, "ntp", enabled ? 1 : 0);
50+
if (res != ESP_OK) {
51+
ESP_LOGE(TAG, "Failed to set NVS entry");
52+
nvs_close(nvs_handle);
53+
return;
54+
}
55+
res = nvs_commit(nvs_handle);
56+
if (res != ESP_OK) {
57+
ESP_LOGE(TAG, "Failed to commit to NVS");
58+
}
59+
nvs_close(nvs_handle);
60+
}

main/ntp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include "esp_err.h"
5+
6+
esp_err_t ntp_start_service(const char* server);
7+
esp_err_t ntp_sync_wait(void);
8+
bool ntp_get_enabled(void);
9+
void ntp_set_enabled(bool enabled);

0 commit comments

Comments
 (0)