logo elektroda
logo elektroda
X
logo elektroda

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

rickbronson 3639 59
ADVERTISEMENT
  • #1 20819097
    rickbronson
    Level 3  

    I'm thinking about adding sunrise and sunset to NTP events, anyone interested? Attached is a patch that I haven't tried yet. Any feedback is greatly appreciated.

    Rick
  • ADVERTISEMENT
  • #2 20819101
    rickbronson
    Level 3  

    The format would be something like this:

    // Example: do event every Thursday at sunrise
    // addClockEvent sunrise 0x08 12 POWER OFF

    The code I used is borrowed heavily from Tasmota, hope there isn't any licensing issue.
  • #3 20819240
    p.kaczmarek2
    Moderator Smart Home
    Very good job, GPL license is okay. My only concern would be memory size, but we can put that in the obk_config.h as a conditional include. Can you open a pull request on Github so we can review it?

    On a code review side, I would suggest some little changes, for a start, I'd think it's better to merge ntp_setLatitude with ntp_setLongitude into one command, you know, we are limited by flash memory, there is no need for two separate command I think
    Helpful post? Buy me a coffee.
  • #4 20820503
    rickbronson
    Level 3  

    Okay, thanks for the feedback. I folded both latitude and longitude into one command. I was worried about code space too but was very surprised that the whole thing took less than 900 bytes (not counting any possible increase in math library use via all the trig functions it uses, like atanf, sinf, etc). I'll be testing it during the coming week, these kinds of things take a while to test :)

    Cheers,

    Rick
  • #5 20823018
    rickbronson
    Level 3  

    I've tested the sunrise/sunset algorithm with various cities around the world and it seems to be accurate within +-2 minutes. Not good enough for a space launch blastoff time but maybe good enough for us? There might be an issue with setting it up in the autoexec.bat however. Maybe you have an idea how to fix it. Seems like we might need a "set event when network is live after boot". Also might need to force NTP when the timezone is set instead of doing:

    Info:NTP:NTP offset set, wait for next ntp packet to apply changes

    The above message means that I can't do this in my autoexec.bat:

    PowerSave 1
    addRepeatingEvent 10 1 backlog startDriver NTP; ntp_timeZoneOfs -8; NTP_SetLatlong 44.002130 -123.091473; addClockEvent sunrise 0x7f 12 POWER OFF; addClockEvent sunset 0x7f 13 POWER ON

    Since the offset won't have taken place when I do "addClockEvent sunrise" and I could (potentially) be in the wrong timezone.

    Any ideas about fixing the above is greatly appreciated.

    Rick
  • #6 20823027
    p.kaczmarek2
    Moderator Smart Home
    Hello, are you aware about:
    
    waitFor NTPState 1
    

    style of syntax?

    See random sample script with waitFor here: https://www.elektroda.com/rtvforum/topic4011890-30.html#20822736

    EDIT: Altough.... your question may not be strictly related to that, maybe we just need to recalculate the time in-place at the time of command execution... idk, take OLD offset, subtract it from current time and add a new one...?

    Added after 44 [minutes]:

    EDIT: I did it:
    https://github.com/openshwprojects/OpenBK7231...mmit/55e6ca9ac6a5891470b145c015ee6f3094e6096b
    Helpful post? Buy me a coffee.
  • #7 20824823
    rickbronson
    Level 3  

    >> EDIT: I did it:

    Thanks for that! And for the "waitFor NTPState 1" tip. I'll give it a try.

    I've been seeing these happen randomly (in groups of 10):
    Info:NTP:NTP_CheckForReceive: Error while receiving server's msg

    Any ideas?

    BTW, my server is:

    Info:NTP:Server=217.147.223.78, Time offset=-28800
  • #8 20824897
    p.kaczmarek2
    Moderator Smart Home
    Why do you think it's a problem? I've a looked at code and it seems normal, it happens when recv fails. It's UDP, it's not reliable, it may fail to receive data sometimes.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #9 20828244
    rickbronson
    Level 3  

    >> Why do you think it's a problem? I've looked at the code and it seems normal, it happens when recv fails. It's UDP, it's not reliable, it may fail to receive data sometimes.

    I guess maybe I was misled by the "Error" in the message, to me "Error" means there is a problem. But before you change the message verbiage, I have some NTP poll suggestions:
    - increase NTP poll time from 1 minute to ~15 minutes, judging from info on the web 1 minute is too fast.
    - remove polling (and error msg) each second for 10 seconds if poll fails, just quietly wait until the next polling period. If it fails for 5 polls in a row, issue a warning.

    Added after 8 [minutes]:

    I've been testing the sunrise/sunset code and it seems to work well. I've attached a patch to version 1.17.308. Please tell me if there are any issues.

    Here is an example autoexec on how to use it:

    PowerSave 1
    startDriver ntp
    ntp_timeZoneOfs -8
    waitFor NTPState 1
    ntp_SetLatLong 44.002130 -123.091473
    addClockEvent sunrise 0x7f 12 POWER OFF
    addClockEvent sunset 0x7f 13 POWER ON
  • #10 20828276
    rickbronson
    Level 3  
    I have some unrelated questions:

    - Do you know where I can get a *real* BK7231N datasheet. The ones in the forum are helpful but not the real thing.
    - is there way to put pin config's into autoexec, I'm looking for a way to setup the entire config in one file.

    Oh and by the way, I made a mistake in the amount of added space for the sunrise/sunset, it's actually around 11k. Maybe some of that comes from the trig functions that are used. I got this number from looking at the .asm file ending address for 1.17.308 vs my mods.

    Thanks for all the help

    Rick
  • #11 20843843
    rickbronson
    Level 3  

    Did you get a chance to look at the patch? If you think the 11k is too much I could #ifdef it with a config setting.

    Rick
  • #12 20843876
    p.kaczmarek2
    Moderator Smart Home
    Sorry for the late reply. I've checked the code, it's good, but why are you adding new fields to config structure? If config size changes between versions, then downgrade will not be possible.
    If you really need to use config structure, maybe reuse those fields:
    Code: C / C++
    Log in, to see the code


    Or maybe just use global variables and expect user to set them in autoexec.bat every time?

    The BK7231 information on our forum is all we know.

    You can use setPinRole to set pins via autoexec.bat : https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md

    All sunset/sunrise code should be within #define put in obk_config.h
    Helpful post? Buy me a coffee.
  • #13 20846823
    rickbronson
    Level 3  

    > Sorry for the late reply.
    No problem.

    > I've checked the code, it's good, but why are you adding new fields to the config structure? If the config size changes between versions, then downgrade will not be possible.
    > If you really need to use the config structure, maybe reuse those fields:
    > Code: c Expand Select all Copy to clipboard

    I did it that way because I thought it was the "right" way to do it. I don't really know this code that well, so how about you tell me the way you think it should be done, and I'll do it.

    > The BK7231 information on our forum is all we know.
    > You can use setPinRole to set pins via autoexec.bat: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md

    Thanks much for the info.

    > All sunset/sunrise code should be within #define put in obk_config.h

    Okay, I'll work on that.
  • #14 20846866
    p.kaczmarek2
    Moderator Smart Home
    If you are unsure about config save, then just let's use global variables and assume that it should be put in autoexec.bat and set on every startup that way
    Helpful post? Buy me a coffee.
  • #15 20851746
    omniron
    Level 10  
    Is sunrise and sunset working?
    Would be great!
  • #16 20851758
    p.kaczmarek2
    Moderator Smart Home
    Yes, what is the state of that? Can I merge?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #18 20852637
    p.kaczmarek2
    Moderator Smart Home
    commands.md is autogenerated from JSON comments in code, do you have a Github account?
    Helpful post? Buy me a coffee.
  • #19 20852775
    omniron
    Level 10  

    Thank you Rickbronson for this!
    Wow that is 11K (byte?), sorry.
  • #20 20852974
    rickbronson
    Level 3  

    >>20852637
    I do have a GitHub account: github.com/rickbronson
  • ADVERTISEMENT
  • #21 20853401
    p.kaczmarek2
    Moderator Smart Home
    Can you open a pull request with your code changes?
    Just move the variables from config to globals (or statics), and let's just assume they will be set every session through autoexec.bat
    Helpful post? Buy me a coffee.
  • #22 20854181
    rickbronson
    Level 3  

    Never done a git pull request but would like to try it. Before I do that, please take a quick look at the patch and see if you notice anything you'd like changed.
  • #24 20854291
    p.kaczmarek2
    Moderator Smart Home
    It's 99% okay and you can open pull request, however, I forgot to mention, that if you want to add autoexec to docs, you do not need to modify the MD file, you just modify the source json:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/json/autoexecExamples.json
    and put autoexec there:
    https://github.com/openshwprojects/OpenBK7231T_App/tree/main/docs/autoexecs
    then if you run:
    [BK7231N] I'm thinking about adding sunrise and sunset to NTP events, anyone interested?
    it will generate new MDs based on that info

    But again, it's not a problem, it's just documentation related, the code seems good now
    Helpful post? Buy me a coffee.
  • #25 20855621
    rickbronson
    Level 3  

    You'll have to excuse my ignorance of git. I've only used it a little. Here is what I get (on Debian):

    rick@laptop:~/boards/tuya-temp-humidity/OpenBK7231T_App$ git push -u origin sunevent_branch
    Username for 'https://github.com': rickbronson
    Password for 'https://rickbronson@github.com':
    remote: Permission to openshwprojects/OpenBK7231T_App.git denied to rickbronson.
    fatal: unable to access 'https://github.com/openshwprojects/OpenBK7231T_App/': The requested URL returned error: 403
    r
  • #26 20855929
    p.kaczmarek2
    Moderator Smart Home
    I haven't used git command line in a very long time, but don't you need first to push to your local cloned branch, like:
    
    https://github.com/rickbronson/OpenBK7231T_App/
    

    (I modified URL, so it's on your account now) and then open the pull request?

    These days I am using Github GUI 100% time, it is much easier, maybe you can try it out as well.
    Helpful post? Buy me a coffee.
  • #27 20856861
    rickbronson
    Level 3  

    Yeah, I tried the GUI first but found it very confusing.
    I went to: https://github.com/openshwprojects/OpenBK7231T_App/pulls
    and hit "New pull request" and then, very confusingly, it went to a "Compare changes" page. There was a "create pull request" on that page but it was grayed out. Here is the latest patch, just FYI

    Added after 1 [minutes]:

    Patch
  • #29 20859018
    p.kaczmarek2
    Moderator Smart Home
    Both Github GUI and Github itself should propose making you a pull request from your branch to parent repository, I think
    Helpful post? Buy me a coffee.
  • #30 20861889
    omniron
    Level 10  

    Does sunrise/set work? I can try it out if experimental

    Added after 1 [minutes]:

    Settings panel with options to enable timers and choose sunrise or sunset, along with save options.

Topic summary

The discussion revolves around the implementation of sunrise and sunset events in NTP (Network Time Protocol) for the BK7231N device. Rick proposes adding these features and shares a patch for feedback. Contributors express concerns about memory usage and suggest merging latitude and longitude commands to optimize space. Testing reveals the algorithm's accuracy within ±2 minutes, though issues arise with event setup in autoexec.bat due to NTP offset timing. Solutions include using "waitFor NTPState 1" for synchronization and adjusting polling intervals. The feature is successfully merged, with users sharing autoexec examples and discussing enhancements for manual overrides after power outages.
Summary generated by the language model.
ADVERTISEMENT