From 7ed19501ad64c6bcf4f956b7ec73d25a764b664a Mon Sep 17 00:00:00 2001 From: pkkrusty <79770016+pkkrusty@users.noreply.github.com> Date: Fri, 7 Jan 2022 08:17:03 -0800 Subject: [PATCH 1/3] Add DS3231 bit for turning NTP on/off --- tasmota/support_features.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index 206735a15..e186f9aa3 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -787,8 +787,9 @@ void ResponseAppendFeatures(void) #ifdef USE_SHIFT595 feature8 |= 0x00080000; // xdrv_60_shift595.ino #endif - -// feature8 |= 0x00100000; +#if defined(USE_I2C) && defined(USE_DS3231) + feature8 |= 0x00100000; // xsns_33_ds3231.ino +#endif // feature8 |= 0x00200000; // feature8 |= 0x00400000; // feature8 |= 0x00800000; From 0ef8807fac49493ca1b1eb9b3298fbbfb8e24bed Mon Sep 17 00:00:00 2001 From: pkkrusty <79770016+pkkrusty@users.noreply.github.com> Date: Fri, 7 Jan 2022 08:22:54 -0800 Subject: [PATCH 2/3] Add NTP server capability to DS3231 Copied from GPS driver (xsns_60) --- tasmota/xsns_33_ds3231.ino | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tasmota/xsns_33_ds3231.ino b/tasmota/xsns_33_ds3231.ino index 6082bbe49..987cfd240 100644 --- a/tasmota/xsns_33_ds3231.ino +++ b/tasmota/xsns_33_ds3231.ino @@ -35,6 +35,9 @@ #define XSNS_33 33 #define XI2C_26 26 // See I2CDEVICES.md +#include "NTPServer.h" +#include "NTPPacket.h" + //DS3232 I2C Address #ifndef USE_RTC_ADDR #define USE_RTC_ADDR 0x68 @@ -67,6 +70,23 @@ bool ds3231ReadStatus = false; bool ds3231WriteStatus = false; //flag, we want to read/write to DS3231 only once bool DS3231chipDetected = false; +#define D_CMND_NTP "NTP" + +const char S_JSON_NTP_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_NTP "%s\":%d}"; + +const char kRTCTypes[] PROGMEM = "NTP"; + +#define NTP_MILLIS_OFFSET 50 + +NtpServer timeServer(PortUdp); + +struct NTP_t { + struct { + uint32_t init:1; + uint32_t runningNTP:1; + } mode; +} NTP; + /*----------------------------------------------------------------------* Detect the DS3231 Chip ----------------------------------------------------------------------*/ @@ -156,6 +176,39 @@ void DS3231EverySecond(void) SetDS3231Time (Rtc.utc_time); //update the DS3231 time ds3231WriteStatus = true; } + if (NTP.mode.runningNTP) { + timeServer.processOneRequest(Rtc.utc_time, NTP_MILLIS_OFFSET); + } +} + +/*********************************************************************************************\ + NTP functions + \*********************************************************************************************/ + +void NTPSelectMode(uint16_t mode) +{ + DEBUG_SENSOR_LOG(PSTR("RTC: NTP status %u"),mode); + switch(mode){ + case 0: + NTP.mode.runningNTP = false; + break; + case 1: + if (timeServer.beginListening()) { + NTP.mode.runningNTP = true; + } + break; + + } +} + +bool NTPCmd(void) +{ + bool serviced = true; + if (XdrvMailbox.data_len > 0) { + NTPSelectMode(XdrvMailbox.payload); + Response_P(S_JSON_NTP_COMMAND_NVALUE, XdrvMailbox.command, XdrvMailbox.payload); + } + return serviced; } /*********************************************************************************************\ @@ -173,6 +226,11 @@ bool Xsns33(uint8_t function) } else if (DS3231chipDetected) { switch (function) { + case FUNC_COMMAND_SENSOR: + if (XSNS_33 == XdrvMailbox.index) { + result = NTPCmd(); + } + break; case FUNC_EVERY_SECOND: DS3231EverySecond(); break; From 8dd97f00761fb5905bd61ef3340b395d06bb1fa0 Mon Sep 17 00:00:00 2001 From: pkkrusty <79770016+pkkrusty@users.noreply.github.com> Date: Sat, 8 Jan 2022 05:42:44 -0800 Subject: [PATCH 3/3] Update support_features.ino --- tasmota/support_features.ino | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index e186f9aa3..5b5eaf671 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -787,9 +787,7 @@ void ResponseAppendFeatures(void) #ifdef USE_SHIFT595 feature8 |= 0x00080000; // xdrv_60_shift595.ino #endif -#if defined(USE_I2C) && defined(USE_DS3231) - feature8 |= 0x00100000; // xsns_33_ds3231.ino -#endif +// feature8 |= 0x00100000; // feature8 |= 0x00200000; // feature8 |= 0x00400000; // feature8 |= 0x00800000;