ntp - dl3ebb/OpenIot GitHub Wiki
Setting up your own NTP server can be beneficial since each device will make a request to the NTP server every 15 minutes. Additionally, it helps maintain network isolation as you won't need to open the firewall for external NTP requests.
OpenIot allows you to specify up to three different NTP servers to query. The following example demonstrates the use of an NTP server:
/**
* Demo of the NTP Server usage
*/
#define SWITCH_PIN 25
#define LED_PIN 32
#define WLAN_SSID "your_ssid"
#define WLAN_PASSWD "your_passwd"
#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
#include <Modules/Ntp.h>
#include <Logger/SerialLogger.h>
#include <Modules/Wifi.h>
void setup() {
serialLogger->setBaudrate(115200);
openIot.setLogger(serialLogger);
wifi->setEnablePrimary(true);
wifi->setSsid1(WLAN_SSID);
wifi->setPassword1(WLAN_PASSWD);
ntp->setEnable(true);
/**
* Create the Elements and register them to the connectors
*/
BoolElement *switch1 = new BoolElement("Switch1");
digitalInConnector->registerElement(switch1, SWITCH_PIN, true);
BoolElement *led1 = new BoolElement("Led1");
digitalOutConnector->registerElement(led1, LED_PIN);
eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
led1->setValue(event->newValue);
});
openIot.setup("OpenIotNtp", 1);
}
void loop() {
openIot.loop();
}
The NTP module is simply enabled with:
ntp->setEnable(true);
By default, this will set the NTP server to pool.ntp.org
and configure the timezone to Europe/Berlin (UTC+1, with daylight saving time being 1 hour). However, the NTP module can be customized to fit your specific needs.
After starting the device, the NTP status will be displayed, and after a few seconds, you'll see a message indicating that an NTP update has occurred.
INFO - ---- Ntp settings : ------
INFO - Enabled : true
INFO - Server1 : pool.ntp.org
INFO - Server2 :
INFO - Server3 :
INFO - Offset Gmt (sec) : 3600
INFO - Offset Dst (sec) : 3600
INFO - NTP update : 08.12.2024 15:46:14
| ← Previous Page (REST) | ↑ Tutorial Main Page | Next Page (Web Update) → |