MatrixPortal M4 Moon Phase Clock Code appears Broken

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
oracledude
 
Posts: 16
Joined: Sat Dec 21, 2013 12:18 am

MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

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.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by dastels »

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

//| 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

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

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

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

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'

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by dastels »

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

            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

#print(moon_data)
Dave

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

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

Thanks for the tips.

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

Code: Select all

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

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.

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

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

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

Code: Select all

Retrieving data...Hit Moon server error

User avatar
nerna
 
Posts: 5
Joined: Wed Jan 04, 2017 8:49 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by nerna »

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

Code: Select all

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

    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

            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.

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

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

That worked like a champ. Thank you very much!

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

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by oracledude »

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

    # 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)

User avatar
fid
 
Posts: 74
Joined: Wed Sep 25, 2013 3:00 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by fid »

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. :-)

User avatar
tuumi
 
Posts: 6
Joined: Wed Dec 01, 2021 1:16 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by tuumi »

Hi,
I just ordered all the part for the moon project and was hoping to making this for my son. He's two and loves pointing out the moon. Thought this would be fun to have in his room.
I have zero experience with coding but I thought this could be an easy follow the directions exactly sort of project. I'm at a stand still right now until I get screws to attach the power connectors.
After reading this thread it seem that it may not be as easy as following the steps. Was the project updated or will I have to make the fixes described above? Is there someone here who can walk me through this if I run into trouble?

User avatar
portmorgan
 
Posts: 4
Joined: Fri Sep 23, 2011 10:45 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by portmorgan »

I, too, am struggling with the Moon Clock. After following the instructions, I was successful in getting the clock to run....once. I wanted to change the orientation so I removed the USB C power cord. Upon powering up the clock again, I get what looks like a swan in the upper left corner, that changes to a splash screen, after many minutes the swan returns with the word "Running."

I have completely reinstalled from scratch starting at the bootloader. Same thing occurs. The orientation code is working because the splash screen changes based on rotation. Network appears to be working since I get a valid IP address (according to my router) and I'm able to ping the clock.

I have not yet set up a debugging environment. Ideally, I like to use PlatformIO but I need to find a tutorial for setting it up with this board and circuitpython.

I'm open to suggestions.

User avatar
portmorgan
 
Posts: 4
Joined: Fri Sep 23, 2011 10:45 am

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by portmorgan »

I am struggling to get the moonclock code to work in my environment (see my previous post).

I have narrowed the issue to inexplicable behavior between my router (DD_WRT running current code base) and the adafruit_requests.py library. I created the minimal program to demonstrate the issue (code & serial terminal printout follow). Interestingly, this minimal program work flawlessly when using my Verizon Wireless phone as a hotspot. Specifically, I get a "RuntimeError: Sending request failed" only on the https url. I have confirmed this behavior using other https urls. Ideas to try or pointing out my error(s) are welcome.

Code: Select all

import board
import busio
from adafruit_matrixportal.network import Network
from adafruit_matrixportal.matrix import Matrix

try:
    from secrets import secrets
except ImportError:
    print('WiFi secrets are kept in secrets.py, please add them there!')
    raise

NETWORK = Network(status_neopixel=board.NEOPIXEL, debug=False)
NETWORK.connect()
print("\nMatrixPortal IP address is", NETWORK.ip_address,"\n")

time_url = 'http://worldtimeapi.org/api/timezone/America/Denver'
print("Get the time from: ",time_url,"\n")
foo = NETWORK.fetch_data(time_url)
print(foo)

url = 'https://api.met.no/weatherapi/sunrise/2.0/.json?lat=30&lon=105&date=2022-01-22&offset=-07:00'
print("\nGet the moondata from: ",url,"\n")
foo = NETWORK.fetch_data(url)
print(foo)
++++++++++++++ serial terminal output ++++++++++++
code.py output:
Connecting to AP PortMorgan

MatrixPortal IP address is 10.0.0.203

Get the time from: http://worldtimeapi.org/api/timezone/America/Denver

Retrieving data...Reply is OK!
{"timezone": "America/Denver", "utc_datetime": "2022-01-21T22:01:28.940523+00:00", "raw_offset": -25200, "client_ip": "143.131.13.175", "dst_from": null, "unixtime": 1642802488, "utc_offset": "-07:00", "datetime": "2022-01-21T15:01:28.940523-07:00", "week_number": 3, "abbreviation": "MST", "day_of_year": 21, "day_of_week": 5, "dst": false, "dst_offset": 0, "dst_until": null}

Get the moondata from: https://api.met.no/weatherapi/sunrise/2 ... set=-07:00

Retrieving data...Traceback (most recent call last):
File "code.py", line 23, in <module>
File "adafruit_portalbase/network.py", line 559, in fetch_data
File "adafruit_portalbase/network.py", line 478, in fetch
File "adafruit_requests.py", line 847, in get
File "adafruit_requests.py", line 693, in request
File "adafruit_requests.py", line 546, in _get_socket
RuntimeError: Sending request failed

Code done running
.
++++++++++++++++ end of serial output ++++++++++++++++

User avatar
makeinspires
 
Posts: 32
Joined: Tue Nov 19, 2013 11:58 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by makeinspires »

Following up on this, just spend an hour plus trying to get this project to work, tried multiple times from scratch... Pulled secrets.py from another project I know that works. Nothing - the matrix does not light up at all. I get a double blinking red light on the NeoPixel on the Matrix Portal.
Any ideas?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: MatrixPortal M4 Moon Phase Clock Code appears Broken

Post by tannewt »

A double red blink means Python code ended with an exception. Connect to the serial output to see what the error is.

https://learn.adafruit.com/welcome-to-c ... al-console

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”