logo elektroda
logo elektroda
X
logo elektroda

[BK7231N] I'm thinking about adding sunrise and sunset to NTP events, anyone interested?

rickbronson 6258 60
Best answers

How can sunrise and sunset be added to NTP-based events in OpenBK7231N, and what is the right way to set it up?

Yes — sunrise and sunset can be added to NTP events, and the feature was eventually merged after testing [#20869378][#20906485] Use a single `ntp_setLatlong` command instead of separate latitude/longitude commands, and keep the code behind a `#define` in `obk_config.h`; if you want to avoid breaking config downgrades, do not add new config fields and use globals/statics set from `autoexec.bat` instead [#20819240][#20843876][#20846866] In `autoexec.bat`, start NTP, set the timezone and coordinates, then `waitFor NTPState 1` before creating `addClockEvent sunrise ...` and `addClockEvent sunset ...`, otherwise the events may be created before the timezone change has taken effect [#20823027][#20828244] The implementation was reported to be accurate to about ±1–2 minutes versus published sunrise/sunset times, which was considered acceptable [#20823018][#20869623] If you want a sunset delay like Tasmota’s civil sunset, the simplest workaround suggested was a one-shot `addRepeatingEvent` delay of 30 minutes after the sunset event, but it will not survive a reboot during that delay [#20879370]
Generated by the language model.
ADVERTISEMENT
  • #61 21753729
    divadiow
    Level 38  
    Posts: 4859
    Help: 424
    Rate: 860
    I don't know if this is correct, but the chatgpt-codex-connector bot just sent me this suggestion after I updated my branch

    In src/driver/drv_ntp_events.c:
    Code: C / C++
    Log in, to see the code


    DST fix drives sunrise events out of range
    When fix_DSTforEvents shifts all future sun events by hours, it simply does e->hour += hours (line 179). If the DST change subtracts an hour (e.g. summer→winter) any sunrise close to midnight immediately becomes negative (−1) and NTP_RunEventsForSecond will never match it against ltm->tm_hour, so that event is lost until the schedule is recreated. The inverse happens for hours ≥24. Wrap the result back into 0 23 (and adjust the day when crossing midnight) so that sunrise/sunset handlers still run after a DST switch.
  • ADVERTISEMENT

Topic summary

✨ A feature to add sunrise and sunset events to NTP-based scheduling on BK7231N devices was proposed and developed, with code inspired by Tasmota and integrated into the OpenBK7231T_App firmware. The implementation includes commands to set latitude and longitude in a single command to save flash memory, and sunrise/sunset events can be added as clock events triggering device actions such as powering relays on or off. Testing showed the sunrise/sunset calculations are accurate within ±2 minutes globally. Challenges addressed include handling NTP synchronization timing, timezone offsets, and ensuring correct event triggering after device reboot or power outages. The code footprint increased by about 11 KB, mainly due to trigonometric calculations. The feature was merged into the main repository with ongoing improvements such as self-tests and documentation updates. Users shared example autoexec.bat scripts demonstrating how to configure the feature, including setting timezone, coordinates, and clock events for controlling lights. Discussions also covered handling manual overrides and state persistence across reboots using LFS commands. The feature is fully functional and available in the latest OpenBK7231T_App firmware, with additional pull requests extending functionality to expose next sunrise/sunset times as constants.
Generated by the language model.

FAQ

TL;DR: Field tests show the OpenBK sunrise/sunset scheduler stays within ±2 minutes of official tables ("good enough for us" [Elektroda, rickbronson, post #20823018]) and later self-tests cut the error to ±1 minute [Elektroda, p.kaczmarek2, post #20869623]

Why it matters: Accurate, timezone-aware switching lets IoT lights follow natural daylight without cloud services.

Quick Facts

Which firmware build includes the sunrise/sunset feature and how do I turn it on?

Pull Request #1001 merged into OpenBK v1.17.308; the code is wrapped in #define OBK_ENABLE_SUN_EVENTS inside obk_config.h. Re-compile with that flag set, or flash a nightly image after 2023-12-20 where the flag is already enabled [Elektroda, p.kaczmarek2, post #20869378]

How do I create a script that turns Relay 2 on at sunset and off at sunrise?

  1. Store the following as autoexec.bat.
  2. Replace the latitude, longitude, and ntp_timeZoneOfs with your own values.
  3. Reboot the module.

PowerSave 1
startDriver ntp
ntp_timeZoneOfs -8
ntp_SetLatlong 44.002130 -123.091473
waitFor NTPState 1
removeClockEvent 12; removeClockEvent 13
addClockEvent sunrise 0x7f 12 POWER2 OFF
addClockEvent sunset 0x7f 13 POWER2 ON
The waitFor line guarantees correct timezone before scheduling [Elektroda, p.kaczmarek2, post #20823027]

Can I delay the sunset action by 30 minutes to mimic civil twilight?

Yes. Inside the sunset handler, queue a one-shot repeating event:

addClockEvent sunset 0xff 21 backlog addRepeatingEvent 1800 1 POWER2 ON
1800 seconds = 30 minutes. Limitation: if the device reboots during the delay the queued event is lost [Elektroda, p.kaczmarek2, post #20879370]

Why do I keep seeing "NTP_CheckForReceive: Error while receiving" messages?

NTP uses UDP; occasional packet loss is expected. The driver polls every minute, retries ten times each second after a miss, then logs the error. "It’s normal, UDP is not reliable" [Elektroda, p.kaczmarek2, post #20824897] Failures clear automatically once the next valid packet arrives.

How can I preserve the correct ON/OFF state after a power outage?

At boot, compare $hour with $sunrise_hour and $sunset_hour. If current time is between sunset and sunrise, issue POWER2 ON; else POWER2 OFF. Save manual overrides with LFS_WriteStr and read them during boot to skip auto-correction when the user toggled the light [Elektroda, ilengyel, post #20971275]

Where can I get a complete BK7231N datasheet?

Only abridged documents are public. "The BK7231 information on our forum is all we know" [Elektroda, p.kaczmarek2, post #20843876] Full datasheets require NDA through Beken Corp.

What is the correct workflow for submitting code to OpenBK?

  1. Fork openshwprojects/OpenBK7231T_App on GitHub.
  2. Push your branch to your fork (git push origin my_branch).
  3. Click “Compare & pull request” to open a PR against the upstream repository. A direct push to upstream will fail with HTTP 403 [Elektroda, rickbronson, post #20855621]

Why was the BL602 target excluded from the sunrise/sunset commit?

BL602 lacks a hardware FPU; single-precision trig calls bloat code size and slow execution. The maintainer #ifdef-ed the feature to avoid >20 kB growth on that MCU [Elektroda, w2q, post #21389444] If you enable it, use fixed-point math or link a lightweight trig library.

What happens if I downgrade firmware after enabling sunrise/sunset?

If you stored coordinates in the config structure, older builds cannot parse the larger struct and may corrupt settings. Current code keeps coordinates in globals and expects them in autoexec.bat, so downgrades are safe [Elektroda, p.kaczmarek2, post #20846866]
Generated by the language model.
ADVERTISEMENT