My first contact to OpenBeken was when I got some unexpected Smart plugs with LN882H.
To make full use of them, the BL0937 had to be included and this forced to enable other drivers, especially NTP.
Sure, yo can't keep track on energy stats without a time, but the idea was born to add a clock without NTP.
There are some cons, especially that NTP will keep the clock accurate, which I learned was not the case with my first approach.
But, there are some pros:
No need for a network: You can set and use time even in the AP mode
No need for NTP protocol
The first approach was very simple and needed only very limited resources.
And finally: you usually don't need exact clock: If the sum of energy values for one day is inaccurate for some seconds, who cares?
O.k., lets finally get to the basic idea:
If I only add one simple variable containing the "time at startup", I can calculate the actual time by adding the value of g_secondsElapsed.
This worked as expected - for some short time - but in my case it was drifting ~ 2 minutes per day
.
So my first improvement was: making g_secondsElapsed more accurate (requiring two further variables):
Every ten seconds I calculate the number of RTOS ticks elapsed and update g_secondsElapsed to this value.
Now time is o.k. for me - it differs about 1 to 2 seconds a day, I can live white this, and maybe a correction can make it even better.
First approach was to implement all this in user_main and new_common - files "known" to all platforms, so I could compile it for all platforms in that stage.
Now to the more complex work: To make full use of the device time, I would like to use the time related functions, which are all "bound to NTP", but not really related to it (e.g. making NTP_GetMonth() a simple GetMonth() or use events without NTP).
My next goal is to split the NTP driver into the actual NTP ("protocol") part and some generic time functions.
To make this work, I will had to introduce "new source files", which need altered Makfiles to work.
And I found some possible "issues" with NTP, which could be addressed at the same time:
If I stop the NTP driver, this will stop the incrementing the time variable in "NTP_OnEverySecond()" - but it will continue to report "synced", even if the time "stops".
As I learned that the OnEverySecond-loops are not very reliable, I would propose to change NTP to the mechanism I used:
When NTP is synced, just save the corresponding startup time - if you add the (optimized) g_secondsElapsed, the time will not drift so much - and even can be used as a base for continuing the clock this way.
So I would like some input on the general idea, maybe it can be implemented in a better way.
And I would really like some input, if this doesn't break anything but works as expected on other platforms.
Actual screenshot of "index" page:
Changes to LN882Hs Makefile are:
The actual progress are here: PR#1152
For OpenBK7231T/N and OpenBL602 the compilation seems o.k., for the others you will need altered Makefiles, too:
To make full use of them, the BL0937 had to be included and this forced to enable other drivers, especially NTP.
Sure, yo can't keep track on energy stats without a time, but the idea was born to add a clock without NTP.
There are some cons, especially that NTP will keep the clock accurate, which I learned was not the case with my first approach.
But, there are some pros:
No need for a network: You can set and use time even in the AP mode
No need for NTP protocol
The first approach was very simple and needed only very limited resources.
And finally: you usually don't need exact clock: If the sum of energy values for one day is inaccurate for some seconds, who cares?
O.k., lets finally get to the basic idea:
If I only add one simple variable containing the "time at startup", I can calculate the actual time by adding the value of g_secondsElapsed.
This worked as expected - for some short time - but in my case it was drifting ~ 2 minutes per day

So my first improvement was: making g_secondsElapsed more accurate (requiring two further variables):
Every ten seconds I calculate the number of RTOS ticks elapsed and update g_secondsElapsed to this value.
Now time is o.k. for me - it differs about 1 to 2 seconds a day, I can live white this, and maybe a correction can make it even better.
First approach was to implement all this in user_main and new_common - files "known" to all platforms, so I could compile it for all platforms in that stage.
Now to the more complex work: To make full use of the device time, I would like to use the time related functions, which are all "bound to NTP", but not really related to it (e.g. making NTP_GetMonth() a simple GetMonth() or use events without NTP).
My next goal is to split the NTP driver into the actual NTP ("protocol") part and some generic time functions.
To make this work, I will had to introduce "new source files", which need altered Makfiles to work.
And I found some possible "issues" with NTP, which could be addressed at the same time:
If I stop the NTP driver, this will stop the incrementing the time variable in "NTP_OnEverySecond()" - but it will continue to report "synced", even if the time "stops".
As I learned that the OnEverySecond-loops are not very reliable, I would propose to change NTP to the mechanism I used:
When NTP is synced, just save the corresponding startup time - if you add the (optimized) g_secondsElapsed, the time will not drift so much - and even can be used as a base for continuing the clock this way.
So I would like some input on the general idea, maybe it can be implemented in a better way.
And I would really like some input, if this doesn't break anything but works as expected on other platforms.
Actual screenshot of "index" page:

Changes to LN882Hs Makefile are:
--- sdk/OpenLN882H/project/OpenBeken/CMakeLists.txt 2024-03-26 16:04:01.812615610 +0100
+++ sdk/OpenLN882H/project/OpenBeken/CMakeLists.txt 2024-04-02 15:35:22.872182469 +0200
@@ -56,8 +56,10 @@
# app/src/driver/drv_max72xx_internal.c
# app/src/driver/drv_max72xx_single.c
# app/src/driver/drv_mcp9808.c
-# app/src/driver/drv_ntp_events.c
- app/src/driver/drv_ntp.c
+# app/src/driver/drv_ntp_events.c # replaced by drv_clock_events.c
+ app/src/driver/drv_clock_events.c
+ app/src/driver/drv_ntp.c # general time functions moved to drv_deviceclock.c
+ app/src/driver/drv_deviceclock.c
# app/src/driver/drv_pt6523.c
app/src/driver/drv_pwm_groups.c
app/src/driver/drv_pwmToggler.c
The actual progress are here: PR#1152
For OpenBK7231T/N and OpenBL602 the compilation seems o.k., for the others you will need altered Makefiles, too:
--- sdk/OpenW800/app/Makefile 2024-03-26 16:04:03.812580727 +0100
+++ sdk/OpenW800/app/Makefile 2024-04-02 21:24:35.486435795 +0200
@@ -30,6 +30,7 @@
CSRCS += $(_SHARED_APP)/hal/w800/hal_pins_w800.c
CSRCS += $(_SHARED_APP)/hal/w800/hal_wifi_w800.c
CSRCS += $(_SHARED_APP)/httpserver/hass.c
+CSRCS += $(_SHARED_APP)/driver/drv_deviceclock.c
CSRCS += $(_SHARED_APP)/httpserver/http_fns.c
CSRCS += $(_SHARED_APP)/httpserver/http_tcp_server.c
CSRCS += $(_SHARED_APP)/httpserver/http_basic_auth.c
--- sdk/OpenW600/app/Makefile 2024-03-26 16:04:02.376605773 +0100
+++ sdk/OpenW600/app/Makefile 2024-04-02 21:01:54.011157424 +0200
@@ -47,6 +47,7 @@
CSRCS += $(_SHARED_APP)/driver/drv_httpButtons.c
CSRCS += $(_SHARED_APP)/driver/drv_main.c
CSRCS += $(_SHARED_APP)/driver/drv_ntp.c
+CSRCS += $(_SHARED_APP)/driver/drv_deviceclock.c
CSRCS += $(_SHARED_APP)/driver/drv_tasmotaDeviceGroups.c
CSRCS += $(_SHARED_APP)/driver/drv_test_drivers.c
CSRCS += $(_SHARED_APP)/driver/drv_bridge_driver.c
--- sdk/OpenXR809/project/oxr_sharedApp/gcc/Makefile 2024-03-26 16:04:04.420570122 +0100
+++ sdk/OpenXR809/project/oxr_sharedApp/gcc/Makefile 2024-04-02 20:52:18.203372715 +0200
@@ -67,6 +67,7 @@
SRCS += ../shared/src/driver/drv_ntp
SRCS += ../shared/src/driver/drv_tuyaMCU
SRCS += ../shared/src/driver/drv_uart
+SRCS += ../shared/src/driver/drv_deviceclock
SRCS += ../shared/src/i2c/drv_i2c_main
SRCS += ../shared/src/i2c/drv_i2c_mcp23017