Skip to content

Commit 8dedf3a

Browse files
Merge pull request #92 from oldboys92/ui_ntp_sync
Add NTP server and timezone configuration for time synchronization
2 parents 49d4e63 + f3f1965 commit 8dedf3a

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/config/Configuration.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#include "Configuration.h"
22

33
unsigned long startMillis = 0;
4-
unsigned long startMillis_sunspec = 0;
54
unsigned long currentMillis;
5+
// for time synchronization
6+
time_t now;
7+
tm timeinfo;
68

79
// ============================================================================
810
// CONFIGURATION VARIABLES (stored in Preferences)
911
// ============================================================================
1012

1113
// Data source and server settings
1214
char input_type[40];
15+
char ntp_server[40] = "de.pool.ntp.org";
16+
char timezone[64] = "CET-1CEST,M3.5.0/2,M10.5.0/3"; // Central European Time
1317
char mqtt_server[160];
1418
char mqtt_port[6] = "1883";
1519
char mqtt_topic[90] = "tele/meter/SENSOR";
@@ -140,6 +144,8 @@ void WifiManagerSetup() {
140144
preferences.begin("e2s_config", false);
141145
strcpy(input_type, preferences.getString("input_type", input_type).c_str());
142146
strcpy(mqtt_server, preferences.getString("mqtt_server", mqtt_server).c_str());
147+
strcpy(ntp_server, preferences.getString("ntp_server", ntp_server).c_str());
148+
strcpy(timezone, preferences.getString("timezone", timezone).c_str());
143149
strcpy(query_period, preferences.getString("query_period", query_period).c_str());
144150
strcpy(led_gpio, preferences.getString("led_gpio", led_gpio).c_str());
145151
strcpy(led_gpio_i, preferences.getString("led_gpio_i", led_gpio_i).c_str());
@@ -164,6 +170,8 @@ void WifiManagerSetup() {
164170
WiFiManagerParameter custom_input_type("type", "<b>Data source</b><br><code>MQTT</code> for MQTT<br><code>HTTP</code> for generic HTTP<br><code>SMA</code> for SMA EM/HM multicast<br><code>SHRDZM</code> for SHRDZM UDP data<br><code>SUNSPEC</code> for Modbus TCP SUNSPEC data", input_type, 40);
165171
WiFiManagerParameter custom_mqtt_server("server", "<b>Server</b><br>MQTT Server IP, query url for generic HTTP or Modbus TCP server IP for SUNSPEC", mqtt_server, 160);
166172
WiFiManagerParameter custom_mqtt_port("port", "<b>Port</b><br> for MQTT or Modbus TCP (SUNSPEC)", mqtt_port, 6);
173+
WiFiManagerParameter param_ntp_server("ntp_server", "NTP server <span title=\"for time synchronization\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", ntp_server, 40);
174+
WiFiManagerParameter param_timezone("timezone", "Timezone <span title=\"e.g. UTC0, UTC+1, UTC-3, UTC+1CET-1CEST,M3.5.0/02:00:00,M10.5.0/03:00:00\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", timezone, 64);
167175
WiFiManagerParameter custom_query_period("query_period", "<b>Query period</b><br>for generic HTTP and SUNSPEC, in milliseconds", query_period, 10);
168176
WiFiManagerParameter custom_led_gpio("led_gpio", "<b>GPIO</b><br>of internal LED", led_gpio, 3);
169177
WiFiManagerParameter custom_led_gpio_i("led_gpio_i", "<b>GPIO is inverted</b><br><code>true</code> or <code>false</code>", led_gpio_i, 6);
@@ -197,6 +205,8 @@ void WifiManagerSetup() {
197205
wifiManager.addParameter(&custom_section1);
198206
wifiManager.addParameter(&custom_input_type);
199207
wifiManager.addParameter(&custom_mqtt_server);
208+
wifiManager.addParameter(&param_ntp_server);
209+
wifiManager.addParameter(&param_timezone);
200210
wifiManager.addParameter(&custom_query_period);
201211
wifiManager.addParameter(&custom_led_gpio);
202212
wifiManager.addParameter(&custom_led_gpio_i);
@@ -233,6 +243,8 @@ void WifiManagerSetup() {
233243
strcpy(input_type, custom_input_type.getValue());
234244
strcpy(mqtt_server, custom_mqtt_server.getValue());
235245
strcpy(mqtt_port, custom_mqtt_port.getValue());
246+
strcpy(ntp_server, param_ntp_server.getValue());
247+
strcpy(timezone, param_timezone.getValue());
236248
strcpy(query_period, custom_query_period.getValue());
237249
strcpy(led_gpio, custom_led_gpio.getValue());
238250
strcpy(led_gpio_i, custom_led_gpio_i.getValue());
@@ -256,6 +268,8 @@ void WifiManagerSetup() {
256268
DEBUG_SERIAL.println("\tinput_type : " + String(input_type));
257269
DEBUG_SERIAL.println("\tmqtt_server : " + String(mqtt_server));
258270
DEBUG_SERIAL.println("\tmqtt_port : " + String(mqtt_port));
271+
DEBUG_SERIAL.println("\tntp_server: " + String(ntp_server));
272+
DEBUG_SERIAL.println("\ttimezone: " + String(timezone));
259273
DEBUG_SERIAL.println("\tquery_period : " + String(query_period));
260274
DEBUG_SERIAL.println("\tled_gpio : " + String(led_gpio));
261275
DEBUG_SERIAL.println("\tled_gpio_i : " + String(led_gpio_i));
@@ -310,6 +324,8 @@ void WifiManagerSetup() {
310324
preferences.putString("input_type", input_type);
311325
preferences.putString("mqtt_server", mqtt_server);
312326
preferences.putString("mqtt_port", mqtt_port);
327+
preferences.putString("ntp_server", ntp_server);
328+
preferences.putString("timezone", timezone);
313329
preferences.putString("query_period", query_period);
314330
preferences.putString("led_gpio", led_gpio);
315331
preferences.putString("led_gpio_i", led_gpio_i);

src/config/Configuration.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@
3838
#define DEBUG_SERIAL if(DEBUG)Serial
3939

4040
extern unsigned long startMillis;
41-
extern unsigned long startMillis_sunspec;
4241
extern unsigned long currentMillis;
42+
// for time synchronization
43+
extern time_t now;
44+
extern tm timeinfo;
4345

4446
// ============================================================================
4547
// CONFIGURATION VARIABLES (stored in Preferences)
4648
// ============================================================================
4749

4850
// Data source and server settings
4951
extern char input_type[40];
52+
extern char ntp_server[40];
53+
extern char timezone[64];
5054
extern char mqtt_server[160];
5155
extern char mqtt_port[6];
5256
extern char mqtt_topic[90];

src/main.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ void setup(void) {
1919
DEBUG_SERIAL.begin(115200);
2020
WifiManagerSetup();
2121

22+
// Initialize time via NTP
23+
#ifdef ESP32
24+
configTime(0, 0, ntp_server);
25+
setenv("TZ", timezone, 1);
26+
tzset();
27+
#else
28+
// ESP8266
29+
configTime(timezone, ntp_server);
30+
#endif
31+
while (!getLocalTime(&timeinfo))
32+
{
33+
DEBUG_SERIAL.println("Waiting for NTP time...");
34+
delay(500);
35+
}
36+
DEBUG_SERIAL.print("Current time: ");
37+
char time_buffer[20];
38+
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
39+
DEBUG_SERIAL.println(time_buffer);
40+
2241
if (String(led_gpio).toInt() > 0) {
2342
led = String(led_gpio).toInt();
2443
}
@@ -179,15 +198,17 @@ void setup(void) {
179198
// Set Up HTTP query
180199
if (dataHTTP) {
181200
period = atol(query_period);
182-
startMillis = millis();
183201
http.useHTTP10(true);
184202
}
185203

186204
// Set up mDNS responder
187205
setupMdns();
206+
207+
startMillis = millis();
188208
}
189209

190210
void loop() {
211+
currentMillis = millis();
191212
#ifndef ESP32
192213
MDNS.update();
193214
#endif
@@ -214,15 +235,13 @@ void loop() {
214235
parseSHRDZM();
215236
}
216237
if (dataSUNSPEC) {
217-
currentMillis = millis();
218-
if (currentMillis - startMillis_sunspec >= period) {
219-
parseSUNSPEC();
220-
startMillis_sunspec = currentMillis;
238+
if (currentMillis - startMillis >= period) {
239+
parseSUNSPEC();
240+
startMillis = currentMillis;
221241
}
222242

223243
}
224244
if (dataHTTP) {
225-
currentMillis = millis();
226245
if (currentMillis - startMillis >= period) {
227246
queryHTTP();
228247
startMillis = currentMillis;

0 commit comments

Comments
 (0)