Galactic Unicorn modified clock example

I published on Github a repo with a modified clock example for the Galactic Unicorn.
clock modified

The original version is at:
clock original

Cool. What does it look like when it is all together and programmed?

Hi johans. Thank you for your reaction although I don’t understand your question.
My ‘clock modified’ and the ‘clock original’ (by Pimoroni) are similar but different scripts.
I created my ‘mod’ because I wanted to do something different regarding the clock.
Don’t have the intention to put the original (Pimoroni’s) and my examples together if that is what you trying to suggest.
Regarding my ‘clock modified’. It is ‘programmed’ in the sense of being transferred to the flash memory of the Galactic Unicorn (in fact the Raspberry Pi Pico W). It ran on my Galactic Unicorn board for more than 24 hours without error. During the night you can even dim the brilliance of the LEDs to zero and in the morning push on ‘LUX +’ to see the clock again. My example is programmed to connect to internet at an interval of 10 minutes. At each interval an updated date & time is fetched from an NTP time service. Then the built-in realtime clock (RTC) is updated (synchronized) with that updated date & time. I did this because I have the experience that the built-in RTC (of microcontrollers) sometimes tend to run faster than the real world time.

I can see in your code that you use buttons to adapt the UTC-time to local-time. This works, but it would be so much simpler if you would query the local-time directly, e.g. from worldtimeapi.org. You don’t even have to provide a time-zone for that, because worldtimeapi.org guesses your timezone from your IP. Added bonus: no more trouble with daylight-saving-time.

Thank you for your reply. Nice you looked into the code. The programming of the buttons was just a kinda ‘finger exercise’ so to speak. Thank you for your tip about worldtimeapi.org. I have several other projects with NTP and RTC’s however never came across worldtimeapi.org. Great that is takes the ‘burden’ of the DST problem. Makes the code smaller and less prone to bugs. I am going to see and try to implement it.

Just a little warning: worldtimeapi.org is free, but unreliable and has rate-limiting. For normal use, this is no problem, because once every ten minutes is no problem and if an update fails, the next will succeed. But during development/testing, the rate-limiting might hurt.

You will probably find many examples in the internet for dealing with worldtimeapi.org. I have an example in CircuitPython, but this is not very CP specific:

  def _get_remotetime(self):
    """ query time from time-server """

    response = self._wifi.get(settings.TIMEAPI_URL).json()

    current_time = response["datetime"]
    the_date, the_time = current_time.split("T")
    year, month, mday = [int(x) for x in the_date.split("-")]
    the_time = the_time.split(".")[0]
    hours, minutes, seconds = [int(x) for x in the_time.split(":")]

    year_day = int(response["day_of_year"])
    week_day = int(response["day_of_week"])
    week_day = 6 if week_day == 0 else week_day-1
    is_dst   = int(response["dst"])

    return time.struct_time(
      (year, month, mday, hours, minutes, seconds, week_day, year_day, is_dst))

Ah great, thank you for this additional info and the code example Bernard!
I first have to go out for my weekly groceries shopping. Later today I’ll dive into it.
Thanks!

I removed function adjust_utc_offset() which was a leftover from the original clock.py example from Pimoroni. The volume up/down buttons I already was using for that purpose instead of changing the utc_offset. The utc_offset can now be set at key TZ_OFFSET in the file ‘clock_mod_secrets.py’.

I added key NTP_SERVER to file ‘clock_mod_secrets.py’,
to be able to choose a favorite NTP server. Then in function sync_time() there are now:

        ntptime.host = ntp_server
        ntptime.settime()