pimoroni / enviro

MIT License
101 stars 79 forks source link

Board crashes loading time from RTC with OSError: [Errno 22] EINVAL in init.py #215

Open sjefferson99 opened 2 months ago

sjefferson99 commented 2 months ago

This is a continuation of #89 which was closed after reflashing the board seemed to fix. In case anyone else experiences this quite edge case error, this may help resolve as I believe reflashing was a coincidence given the probable cause.

I experienced this error after the board was on the bench for a few months while I replaced my shed that hosted the weather station.

The error was happening in enviro/init.py in this section, specifically the rtc().datetime() line:

# intialise the pcf85063a real time clock chip
rtc = PCF85063A(i2c)
i2c.writeto_mem(0x51, 0x00, b'\x00') # ensure rtc is running (this should be default?)
rtc.enable_timer_interrupt(False)

t = rtc.datetime()
# BUG ERRNO 22, EINVAL, when date read from RTC is invalid for the pico's RTC.
RTC().datetime((t[0], t[1], t[2], t[6], t[3], t[4], t[5], 0)) # synch PR2040 rtc too

I can see this was noted in a previous bug fix run in the comment added by @ZodiusInfuser

Printing the tuple obtained from the RTC showed what appeared to be a value of 25 in the month field t[1] if I have the RTC output correct.

(1907, 25, 25, 18, 14, 41, 6)
some minutes later
(1907, 25, 25, 18, 26, 30, 6)

As I was sat on an uncomfortable toolbox in a shed with a dying laptop battery, I simply placed this hack in to get me over the error and into the time sync code that would set the RTC correctly.

rtc = PCF85063A(i2c)
i2c.writeto_mem(0x51, 0x00, b'\x00') # ensure rtc is running (this should be default?)
rtc.enable_timer_interrupt(False)

t = rtc.datetime()
print(t)
month = t[1]
if month > 12:
    month = 12
print(month)
# BUG ERRNO 22, EINVAL, when date read from RTC is invalid for the pico's RTC.
RTC().datetime((t[0], month, t[2], t[6], t[3], t[4], t[5], 0)) # synch PR2040 rtc too

This gave the expected output and allowed the code to progress and I suggest someone dropping something similar in to unlock themselves if it happens in the future. You should be able to return the code to the original lines once the time has sync'd from NTP and back to RTC as it weirdly doesn't provide the wrong month once the RTC is set to a more correct time (not Edwardian).

(1907, 25, 25, 18, 32, 3, 6)
12
1907-12-25 18:32:09 [info     / 111kB] > IP: 192.168.13.2, Subnet: 255.255.255.0, Gateway: 192.168.13.1, DNS: 192.168.13.1
1907-12-25 18:32:10 [info     / 108kB] > Elapsed: 5632ms
1907-12-25 18:32:10 [warning  / 106kB]   - took 5.632 seconds to connect to wifi
2024-04-05 11:33:24 [info     / 102kB]   - rtc synched

It is probably worth writing some error handling code to deal with each tuple element properly as a first step, which I may add here and PR in. But it is also probably that this is being caused by the Pimoroni RTC driver or even further upstream, which would warrant some investigation for any other RTC users on Pimoroni firmware @Gadgetoid ? Or otherwise please correct me on the expected format of the output for PCF85063A(i2c).datetime() output.