logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

Extending the time sources on OpenBeken devices

max4elektroda 1197 8

TL;DR

  • OpenBeken now supports several time sources beyond NTP, including browser time, DS3231 RTC, NEO-6M GPS, and DCF77 radio time.
  • Defining ENABLE_TIME_PMNTP adds a GUI button to set the clock from PC or tablet time, giving a simple one-time sync path.
  • The DS3231 driver starts with startdriver DS3231 , where 1 syncs once on boot and 2 resyncs every minute.
  • The NEO-6M GPS driver decodes NMEA, can set clock and latitude/longitude, and the DCF77 driver decodes minute-long radio time frames with edge selection options.
  • Cheap DS3231 battery boards may be wired for rechargeable cells, so a CR2032 can be unsafe unless the 200 ohm resistor is removed.
ADVERTISEMENT
📢 Listen (AI):
  • GPS module with ceramic antenna and 4-pin female connector
    Recently time code from OBK was changed to allow for more time sources (beside the well known NTP).

    The possibly simplest method is setting the OpenBeken time to the time of the device you are using to access the GUI (this is allready in OBK code, just not enabled):

    Setting device time to browser time

    If you define "ENABLE_TIME_PMNTP" in obk_config.h your GUI will show an additional button "Set clock to PC time", which will set the time to the time of your PC/Tablet...:

    Control panel with buttons Config, Restart, Launch Web Application, About and Set clock to PC time




    This is done once, and the accuracy depends on your device: My LN882H seems quite good, in half a year the drift was only about 5 minutes - if you set it regulary, you can keep it even better.

    But of course there are more things to present:

    Using a DS3231 RTC

    RTC module with DS3231 chip showing pin labels and components

    Also allready present is a driver for the DS3231 RTC - a cheap possibility to add a battery powered RTC chip which can set the time e.g. on startup.
    It's enabled in obk_config.h with "#define ENABLE_DRIVER_DS3231 1"
    Useful in case the device has no access to another time source, e.g. when there's no internet and hence no NTP possible.
    An important hint if you have one of those cheap battery powered devices like this one: The circuit is designed for a rechargeable battery - if used with e common CR2032 this might blow up your cell (or maybe even worse).
    You can easily test if "VCC" is connected to the battery's "+" over a 200 ohm resistor and a diode. If so, I suggest to simply remove the resistor to protect your battery:
    Close-up of a PCB with SCL, SDA and VCC labels, a red diode, and soldered joints visible.
    If your hardware is clear, you start the driver with

    startdriver DS3231 <SCL-pin> <SDA-pin> <optional arg>


    the optional argument will define if and when the openbeken time is set to the RTC time:
    "1" - this will set the device time to RTC time once on startup
    "2" - the device time is regulary (every minute) set to the RTC time, so RTC is "master clock" for the device.

    To set the RTC you can use the two commands
    DS3231_SetTime <hours> <minutes> <seconds> <day> <month> <year> - (e.g. DS3231_SetTime 19 50 01 13 8 25)
    DS3231_SetEpoch <epoch since 1970> - (e.g. DS3231_SetEpoch 3269879540)

    If you are using a Linux PC you might "set" the RTC via
    wget -q "http://<your.device.IP.address>/cmd_tool?cmd=DS3231_SetEpoch+$(date -u +%s)"

    Here's a picture of an ESP32 with DS3231 on pins 25 (SDA) and 26 (SCL).
    ESP32 board with connected sensor module on breadboard using colored wires

    So I am starting the driver with
    startdriver DS3231 26 25 1
    

    to set devices clock to RTC time once on startup of driver.


    If you don't have (or don't want) your device to get the time via internet and NTP I made two pull requests for additional time sources: GPS and DCF77 (a German longwave time signal radio station).

    Using GPS as time source - NEO-6M driver

    This is actually present in PR#1890

    For the devices with a usable UART, you can use my NEO-6M driver. It will decode the NMEA statements from the device and allow you to set the devices time and even latitude/longitude in case you are using "sunrise/sunset" calculations.
    The modules are quite cheap (~2.5 to 3 € on ali) an you need to connect the NEO-6M RX and TX to the devices TX and RX.
    GPS module with ceramic antenna and 4-pin female connector

    Here an example with the module connected to BK7238 module (needs "flag 26" for me, so NEO6M TX is connected to P1 and NEOs RX to P0).
    GPS module connected to ESP8266 microcontroller on a breadboard
    You have the following arguments to
    "startdriver NEO6M"


    "<baudrate>" will use the integer as baud rate - this requires NEO6M was set to this baudrate permanently before. It will not send any command to NEO6M!
    "setclock" will set the obk time to GPS time if NEO6M is "locked"
    "setlatlong" will set the obk latitude/longitude to GPS values if NEO6M is "locked" (and "ENABLE_TIME_SUNSET_SUNRISE" is defined)

    Usually the GPS module will send a lot of NMEA messages every second - this would need a lot of buffer for the "parser".
    So on startup the driver will send the command to disable all messages except RMC (we will only parse "$GPRMC" lines).
    If you have the possibility to set the NEOM6 permanently to only this output, you would only need the RX on the obk side.

    You might read and set the module via "u-Center" software available at "https://www.u-blox.com" - or use the recenty added "savecfg" argument to "startdriver".
    Not tested too much, but works o.k. here ...

    Device status panel OpenBK7231N with GPS data and WiFi signal strength shown

    As my desk is not directly next to a window, reception sometimes is bad. So I bought a pigtail and an antenna for a vehicle with an 2m cable.
    GPS antenna set with black cable, SMA connector, and mounting end fitting
    I didn't measure, but I'd say reception is not as good as if I lay the original antenna there - but it's o.k. to get a GPS signal even 1.5 meters away from the window.

    If you are using a smart switch with live wires, remember that the ground (GND) is usually not isolated from the live wires! So you will need complete insulation to prevent electric shock.

    Using a DCF77 receiver

    This is actually present in PR#1911

    For small money you will get a small board with an ferrite antenna like this one:
    DCF77 time signal receiver module with ferrite rod antenna and PCB

    It will decode a radio signal broadcasting the actual time in "bits" represented by signal changes. Every second there is a 100ms ("0") or 200ms "1" change which is decoded as high/low on the modules output.

    Since it's often used in battery powered devices, you need to enable reception by pulling the "PON" pin to GND.

    The driver will decode the signal by collecting the bits over one minute and then calculate date, time and DST (summer-/winter-time).
    Reception is not too reliable, even with the parity bit it sometimes receives a wrong time.
    You can therefore test, if the next signal is 60 seconds after the previous to be quite sure, reception is fine.

    The decoding is done via interrupts and tested on LN882H, Beken, BL602 and ESP platforms for now.
    Here's some debug output from the reception and decoding of the signal:
    Screenshot showing DCF77 time signal decoding in OpenBeken device logs.

    Starting with
    startdriver DCF77

    optional arguments are
    "fall" per default, a rising edge will mark the "bits", if you need a falling edge (e.g. if using a opto-isolator), use this argument
    "httpstat" add some details from driver on main page
    OpenBK7231N control panel with DCF77 time source and local clock status

    As stated before, smart switches IO pins are usually live, so I also tried if this works trough an opto-isolator: It does (but inverts the signal in my very simple realization):
    ESP32 and DS3231 modules wired on a breadboard with USB power supply

    This shows my test with a BK7231N switch (here powered by a PoE power supply with 48V) and the receiver input on the "switch" (since opto coupler inverts the signal, I used it with "fall" argument). I'm using a common PC-817 and a 220 ohm resistor for 3.3V for DCF77 device.

    Cool? Ranking DIY
    About Author
    max4elektroda
    Level 24  
    Offline 
    max4elektroda wrote 729 posts with rating 179, helped 47 times. Been with us since 2024 year.
  • ADVERTISEMENT
  • #2 21786490
    p.kaczmarek2
    Moderator Smart Home
    Very nice, I didn't even realize there are so many time sources around! Did you use my new HAL interrupts API for DCF77 on LN882H, Beken, BL602 and ESP? How crucial are timings for DCF77, were there any timing-related issues when decoding the signal?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21786722
    insmod
    Level 31  
    HAL IRQ is not used.
    But, do you think you can adapt BK and BL change interrupts to HAL?
    Plus, LN882H, Realteks after B, ECR, and XRs can use both edge irq oob.

    I doubt this driver would be used very often, so i think IOR should be removed.
  • #4 21786758
    p.kaczmarek2
    Moderator Smart Home
    Well, if it's not using IRQ HAL, then @max4elektroda probably should integrate with our HAL first. It will be way cleaner that way.

    I don't worry much about new IOR role. In the past, I didn't want to add new IOR roles because on the pins config page they were sent as separate combobox for each pin, so the page size grew fast, but I think they are now sent once, right? If the pins page sending is solved, then there isn't really much cost to adding IOR role except the single value in enumeration (which I can assume uses byte, so goes up to 255), and a string name entry in table...

    Or is there some hidden IOR cost that I forget about?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #5 21786764
    max4elektroda
    Level 24  
    Yes, this driver was made before and while HAL_IRQ was created, I will try to change it.

    For now added missing "CHANGE" to W600/W800 (tested), LN882H (testing atm, but it'crashing from time to time, it seems IRQ handler needs to be shortened...) and ESP (untested).
    All were only using rise and fall.

    This is the W800 example:

    diff --git a/src/hal/w800/hal_pins_w800.c b/src/hal/w800/hal_pins_w800.c
    index 9810794d..d294e2b6 100644
    --- a/src/hal/w800/hal_pins_w800.c
    +++ b/src/hal/w800/hal_pins_w800.c
    @@ -271,8 +271,10 @@ void HAL_AttachInterrupt(int pinIndex, OBKInterruptType mode, OBKInterruptHandle
            if (mode == INTERRUPT_RISING) {
                    w_mode = WM_GPIO_IRQ_TRIG_RISING_EDGE;
            }
    -       else {
    +       else if (mode == INTERRUPT_FALLING) {
                    w_mode = WM_GPIO_IRQ_TRIG_FALLING_EDGE;
    +       } else {
    +               w_mode = WM_GPIO_IRQ_TRIG_DOUBLE_EDGE;
            }
            tls_gpio_irq_enable(w600Pin, w_mode);
     }
    


    Maybe I simply switch to the "rising/falling" node for all platforms.
    Regarding an own "IOR" you are right, will change this to an argument

    Actual WIP code works on W800 ("INTERRUPT_CHANGE") and BL602 ("INTERRUPT_RISING"/"INTERRUPT_FALLING") and fails on LN882H (tried both, now I need to find out how to shorten IRQ handler)

    insmod wrote:
    But, do you think you can adapt BK and BL change interrupts to HAL?

    Never looked to deep in the interrupt code, maybe after I got this DCF77 code working with the new API.
  • #6 21786839
    p.kaczmarek2
    Moderator Smart Home
    Beken has no native change interrupt I think, so you need to set it up again to opposite edge with each event.
    Helpful post? Buy me a coffee.
  • #7 21786964
    max4elektroda
    Level 24  
    Switched to new IRQ API and added "CHANGE" for the platforms I knew about and can test:
    W800, LN882H and ESP - tested with ESP8266.
    DCF driver works for these (LN882H tested with both "flavors": "CHANGE" and "RISING/FALLING")

    Tested on BL602 as a platform w/o "CHANGE" interrupt.

    Code is really much nicer now.
    There's still some cleanup necessary to remove some only commented out code from the old approach of interrupt handling...

    Moved code not necessarily done in ISR to a new QuickTick function (processing only once a second might fail if we try to process the date while we are in the 100/200ms of one signal).

    Didn't change IOR role but must finish for today.
  • ADVERTISEMENT
  • #8 21787101
    p.kaczmarek2
    Moderator Smart Home
    Nice, HAL updates are necessary, it's good they turned out to be done today already.

    IOR entry is your choice, at the moment I don't think it has much impact on the main build size.
    Helpful post? Buy me a coffee.
  • #9 21787289
    dktr
    Level 26  
    Cool, at one time I tested on an esp8266 + gps neo6p and with the PPS output connected and you can make a pretty reasonable NTP time server out of it.
📢 Listen (AI):

Topic summary

✨ The OpenBeken (OBK) firmware has been updated to support multiple time sources beyond the traditional NTP synchronization. One simple method enabled by defining "ENABLE_TIME_PMNTP" in obk_config.h allows setting the device clock to the time of the browser accessing the GUI via a "Set clock to PC time" button. This method relies on the accuracy of the client device's clock and can maintain reasonable time precision with regular updates, as demonstrated by the LN882H module with minimal drift over six months. Additionally, OBK supports integration with a DS3231 Real-Time Clock (RTC) module, providing a battery-backed hardware time source for improved timekeeping stability independent of network connectivity.

FAQ

TL;DR: Extend OpenBeken time sources with browser time, DS3231 RTC, or NEO‑6M GPS; one device showed 5‑minute drift over 6 months. “Set clock to PC time” adds a one‑click sync option. [Elektroda, max4elektroda, post #21786021] Why it matters: Reliable clocks fix schedules, logs, and sunrise/sunset automations when NTP is unavailable—ideal for offline or firewall‑restricted devices.

Quick Facts

What is OpenBeken time extension about?

OpenBeken (OBK) now supports more time sources beyond NTP. You can sync from your browser, a DS3231 RTC, or a NEO‑6M GPS. The update adds compile flags, drivers, and commands to set device time at startup or periodically. It also supports latitude/longitude updates from GPS to power sunrise/sunset features when enabled. This helps devices without internet keep accurate schedules and logs. “This is done once, and the accuracy depends on your device.” [Elektroda, max4elektroda, post #21786021]

How do I set OpenBeken time to my PC/browser time?

Compile with ENABLE_TIME_PMNTP in obk_config.h. After flashing, open the GUI and use the new button “Set clock to PC time.” The device sets its time to your browser device’s clock. Accuracy depends on your host clock stability. One reported device drifted only about 5 minutes over six months before resync. [Elektroda, max4elektroda, post #21786021]

How do I wire and start a DS3231 RTC with OpenBeken?

Connect DS3231 SCL to your chosen SCL pin and SDA to SDA. Then run: startdriver DS3231 . Use opt=1 to set time once on startup; opt=2 to resync every minute. To program the RTC time, use DS3231_SetTime or DS3231_SetEpoch. [Elektroda, max4elektroda, post #21786021]

Is there a quick Linux command to set the DS3231 to current UTC?

Yes. Example: wget -q "http:///cmd_tool?cmd=DS3231_SetEpoch+$(date -u +%s)". This writes the current UTC epoch into the DS3231 via the device’s HTTP command interface. After that, the device can read back the RTC time at boot or on schedule, depending on your driver option. [Elektroda, max4elektroda, post #21786021]

What is the risk with cheap DS3231 battery boards and how do I fix it?

Some boards attempt to charge the coin cell through a resistor and diode path. A non‑rechargeable CR2032 can overheat or vent in that circuit. Test for a path from VCC to battery “+” via around 200 Ω and a diode. If present, remove the resistor to protect the cell. [Elektroda, max4elektroda, post #21786021]

How do I use a NEO‑6M GPS module as a time source?

Connect NEO‑6M TX to the device RX and RX to TX. Start with: startdriver NEO6M setclock setlatlong. The driver sets time when GPS is locked and can update latitude/longitude for sunrise/sunset if enabled. It disables all but $GPRMC to reduce parsing overhead. [Elektroda, max4elektroda, post #21786021]

Can the GPS driver save configuration on the NEO‑6M?

Yes. Use the “savecfg” argument with startdriver to store settings on the module. That lets you persist output message choices and baud rate, reducing runtime commands. The post notes it was not heavily tested but worked as expected. [Elektroda, max4elektroda, post #21786021]

What’s the cost of adding GPS time to my device?

Approximate NEO‑6M module price is €2.5–€3. This budget option provides UTC and position for automation without internet access. “The modules are quite cheap (~2.5 to 3 € on ali).” That makes GPS a practical alternative where NTP is blocked. [Elektroda, max4elektroda, post #21786021]

How accurate is device time if I only set it from the browser occasionally?

Accuracy depends on your SoC’s clock drift. The author reports an LN882H drifting about 5 minutes over half a year. Regular manual sync keeps error small. For better stability, make DS3231 the master (opt=2) to correct the device each minute. [Elektroda, max4elektroda, post #21786021]

What is DCF77 and will OpenBeken support it?

DCF77 is a German longwave radio time signal. The author submitted pull requests adding DCF77 as an additional time source, alongside GPS. The thread says “…to be continued with DCF77 driver …,” indicating planned coverage in that PR series. [Elektroda, max4elektroda, post #21786021]

Do I need both TX and RX lines for NEO‑6M?

The OBK driver transmits at startup to limit NMEA output to RMC only. That needs TX from the device to the NEO‑6M. If you configure the module permanently for RMC‑only elsewhere, you may wire only the module’s TX to the device’s RX. [Elektroda, max4elektroda, post #21786021]

How do I start the DS3231 on ESP32 pins 26 (SCL) and 25 (SDA)?

Use: startdriver DS3231 26 25 1. That sets the device clock from the RTC once at driver startup. Adjust the third argument to 2 if you want continuous minute‑by‑minute resync instead. [Elektroda, max4elektroda, post #21786021]

How do I wire NEO‑6M to a BK7238 running OpenBeken?

On the shown setup, the author used “flag 26” so NEO‑6M TX goes to P1 and RX to P0. Ensure the UART mapping matches your board configuration before starting the driver. Then run startdriver NEO6M with the desired options. [Elektroda, max4elektroda, post #21786021]

What commands are available to set DS3231 time?

Use DS3231_SetTime (e.g., DS3231_SetTime 19 50 01 13 8 25). Or use DS3231_SetEpoch (e.g., DS3231_SetEpoch 3269879540) for a single‑value update. [Elektroda, max4elektroda, post #21786021]

Quick setup: How do I enable the GUI one‑click time sync?

  1. Open obk_config.h and define ENABLE_TIME_PMNTP.
  2. Build and flash the firmware to your device.
  3. In the GUI, click “Set clock to PC time.” [Elektroda, max4elektroda, post #21786021]

What’s an edge case that can break my setup?

Using a non‑rechargeable CR2032 on a DS3231 board designed to trickle‑charge the cell. The cell can fail. Verify the resistor‑diode path from VCC to the battery “+”. Remove the resistor if present to prevent charging. [Elektroda, max4elektroda, post #21786021]
ADVERTISEMENT