Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

MatrixPortal M4 Moon Phase Clock Code appears Broken
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Tue Oct 05, 2021 1:17 pm

Trying to get the clock from https://learn.adafruit.com/moon-phase-c ... trixportal working on my MP M4, but it looks like the checked in code is no longer working. Both the time check and the moon phase pull return different json than expected.

The update_time() call at line 272 fails, debug print out is:
Fetching moon data via https://api.met.no/weatherapi/sunrise/2 ... e=2000-01-

After that, the boot screen stays onscreen until a stack crash with the following output:
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Traceback (most recent call last):
File "code.py", line 280, in <module>
File "code.py", line 135, in __init__
TypeError: function takes 1 positional arguments but 9 were given


Code is directly copy/pasted from github repo. secrets.py is formatted correctly, tested with MetroClock script.

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by dastels on Tue Oct 05, 2021 2:38 pm

Are you using the latest version of CircuitPython (7.0.0) https://circuitpython.org/board/matrixportal_m4/ and libraries from the latest 7.x library bundle https://circuitpython.org/libraries? If not, delete everything in the CIRCUITPY/lib directory, empty trash, and copy new library files/directories from the bundle. See https://learn.adafruit.com/welcome-to-circuitpython if you are unfamiliar with any of this.

This guide/project may be a bit out of date compared to the latest more recent versions of CircuitPython. It looks like the time.struct_time expects a tuple of the 9 values, rather than the 9 values directly as arguments. I.e.:
Code: Select all | TOGGLE FULL SIZE
//| class struct_time:
//|     def __init__(self, time_tuple: Sequence[int]) -> None:
//|         """Structure used to capture a date and time.  Can be constructed from a `struct_time`, `tuple`, `list`, or `namedtuple` with 9 elements.

You can try putting square brackets around the 9 arguments to make it a list. I suspect that will fix the error you posted.

Dave

dastels
 
Posts: 7967
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Tue Oct 05, 2021 3:45 pm

I am running the most recent CircuitPython 7.0 binary and libraries.

After adding brackets around a time struct call, it looks like the json calls are still misaligned. Time still isn't set, and the moonphase info is unused.

Code: Select all | TOGGLE FULL SIZE
Connecting to AP %%%%%%%
Using stored geolocation:  **.**** **.****
Retrieving data...Reply is OK!
Fetching moon data via https://api.met.no/weatherapi/sunrise/2.0/.json?lat=**.****&lon=**.****&date=2000-01-01&offset=+00:00
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Connecting to AP %%%%%%%
Retrieving data...Reply is OK!
Fetching moon data via https://api.met.no/weatherapi/sunrise/2.0/.json?lat=**.****&lon=**.****&date=2000-01-01&offset=+00:00
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Retrieving data...Reply is OK!
Traceback (most recent call last):
  File "code.py", line 307, in <module>
AttributeError: 'MoonData' object has no attribute 'midnight'

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by dastels on Tue Oct 05, 2021 4:38 pm

Hmm.. this is a frustrating bit of Python... attribute are added dynamically as they are assigned. If that code doesn't get run for some reason... no attribute.

The moon code does the Very Bad Thing of quietly eating eating all exceptions that might be thrown from the code that assigns/creates the midnight attribute. So it might not be getting created without any mention of a problem.

Try adding a println in the except block that starts on line 167 of code.py:
Code: Select all | TOGGLE FULL SIZE
            except:
                # Moon server error (maybe), try again after 15 seconds.
                # (Might be a memory error, that should be handled different)
                time.sleep(15)

Just something to let you know if that's what's happening. If it happens 5 times (due to the for loop), the code will happily continue without things being properly initialized.

You could also uncomment line 151:
Code: Select all | TOGGLE FULL SIZE
#print(moon_data)


Dave

dastels
 
Posts: 7967
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Tue Oct 05, 2021 5:31 pm

Thanks for the tips.

So couple errors related to JSON then.
First, update_time is failing:

Code: Select all | TOGGLE FULL SIZE
try:
    DATETIME, UTC_OFFSET = update_time(TIMEZONE)
except:
    DATETIME, UTC_OFFSET = time.localtime(), '+00:00'
    print('DATETIME update_time FAILED')
LAST_SYNC = time.mktime(DATETIME)


Then the moon sync code runs, pulls some JSON but still errors, then error out every subsequent run.
Code: Select all | TOGGLE FULL SIZE
Retrieving data...Reply is OK!
{'moonposition': {'time': '2000-01-01T00:00:00+00:00', 'elevation': '-52.065801165', 'range': '405946.911254765', 'azimuth': '313.287471198', 'desc': 'LOCAL MOON POSITION Elv: -52.066 deg, Azi: 313.287, Rng: 405946.9 km', 'phase': '82.59566934'}, 'sunrise': {'desc': 'LOCAL DIURNAL SUN RISE', 'time': '2000-01-01T13:20:27+00:00'}, 'solarnoon': {'desc': 'LOCAL DIURNAL MAXIMUM SOLAR ELEVATION (Max= 28.28578)', 'time': '2000-01-01T18:05:52+00:00', 'elevation': '28.2857836'}, 'sunset': {'time': '2000-01-01T22:51:17+00:00', 'desc': 'LOCAL DIURNAL SUN SET'}, 'moonset': {'time': '2000-01-01T19:44:10+00:00', 'desc': 'LOCAL DIURNAL MOON SET'}, 'date': '2000-01-01', 'solarmidnight': {'desc': 'LOCAL DIURNAL MINIMUM SOLAR ELEVATION (Min= -74.35238)', 'time': '2000-01-01T06:05:33+00:00', 'elevation': '-74.352378126'}, 'moonphase': {'time': '2000-01-01T00:00:00+00:00', 'desc': 'LOCAL MOON STATE * MOON PHASE= 82.6 (waning crescent)', 'value': '82.59566934'}, 'moonshadow': {'time': '2000-01-01T00:00:00+00:00', 'desc': 'LOCAL MOON STATE * SHADOW ANGLES (azi=284.0,ele=-27.0)', 'elevation': '-27.035670465', 'azimuth': '283.993070479'}, 'low_moon': {'desc': 'LOCAL DIURNAL MINIMUM MOON ELEVATION (Min= -60.79489)', 'time': '2000-01-01T01:54:12+00:00', 'elevation': '-60.794893547'}, 'moonrise': {'desc': 'LOCAL DIURNAL MOON RISE', 'time': '2000-01-01T08:38:42+00:00'}, 'high_moon': {'desc': 'LOCAL DIURNAL MAXIMUM MOON ELEVATION (Max= 39.61053)', 'time': '2000-01-01T14:11:53+00:00', 'elevation': '39.610532351'}}
Hit Moon server error
Retrieving data...Reply is OK!
Hit Moon server error
Retrieving data...Reply is OK!
Hit Moon server error
Retrieving data...


Certainly seems flaky at best.

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Tue Oct 05, 2021 5:34 pm

Added another sanity check and restarted. This time it doesn't even pull the moon JSON on any iterations

Code: Select all | TOGGLE FULL SIZE
Retrieving data...Hit Moon server error

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by nerna on Fri Oct 08, 2021 1:07 pm

I was also getting the same error when running the moon phase clock code with CP7.0.0 and latest libraries.

Code: Select all | TOGGLE FULL SIZE
Retrieving data...Reply is OK!
Traceback (most recent call last):
  File "code.py", line 280, in <module>
  File "code.py", line 135, in __init__
TypeError: function takes 1 positional arguments but 9 were given

There are a couple places in code.py where time.struct_time is called.
Add an additional set of parenthesis around the 9 elements to satisfy the time_tuple requirements.

Here's what the change looks like to the original code:

Starting at line 55:
Code: Select all | TOGGLE FULL SIZE
    return time.struct_time((int(year_month_day[0]),
                            int(year_month_day[1]),
                            int(year_month_day[2]),
                            int(hour_minute_second[0]),
                            int(hour_minute_second[1]),
                            int(hour_minute_second[2].split('.')[0]),
                            -1, -1, is_dst))

Also starting at line 129:
Code: Select all | TOGGLE FULL SIZE
            datetime = time.localtime(time.mktime(time.struct_time((
                datetime.tm_year,
                datetime.tm_mon,
                datetime.tm_mday,
                datetime.tm_hour + hours_ahead,
                datetime.tm_min,
                datetime.tm_sec,
                -1, -1, -1))))

After adding the extra set of parenthesis in both places, the code started working again. Cheers.

nerna
 
Posts: 3
Joined: Wed Jan 04, 2017 8:49 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Fri Oct 08, 2021 1:33 pm

That worked like a champ. Thank you very much!

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by oracledude on Fri Oct 08, 2021 2:44 pm

Another thing I noticed is that the color strings are RBG, not RGB. Had pink instead of amber for the PM event until I switched 2nd and 3rd pairs of values.

Code: Select all | TOGGLE FULL SIZE
    # Show event time in green if a.m., amber if p.m.
    GROUP[8].color = GROUP[9].color = (0x0000FF if EVENT_TIME.tm_hour < 12
                                       else 0xC00040)

oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

by fid on Mon Oct 25, 2021 1:47 am

Thank you for getting this fixed. Two weeks later I attempted the same project and was getting ready to revert to CircuitPython 6 to see if I could get it to work. The answers sure saved me a bit of trouble. :-)

fid
 
Posts: 62
Joined: Wed Sep 25, 2013 3:00 pm

Please be positive and constructive with your questions and comments.