0

PyPortal Invalid Syntax
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PyPortal Invalid Syntax

by slowjim on Thu Feb 06, 2020 4:50 pm

Hello

I am new to Circuit Python and PyPortal.

I have been trying to set up my Pyportal. I have been able to do the basic wifi check, however when I try to run a simple script such as Nasa image of the day I receive a message such as this:

code.py output:
Traceback (most recent call last):
File "code.py", line 3, in <module>
File "adafruit_pyportal.py", line 77, in <module>
File "adafruit_pyportal.py", line 73, in <module>
File "secrets.py", line 8
SyntaxError: invalid syntax

lines 1 to 3 in code.py are:

import time
import board
from adafruit_pyportal import PyPortal

line 8 refers in secrets refers to the time zone:
'timezone' : "XXXXXXXXXX/XXXXXXXX", # http://worldtimeapi.org/timezones


All the libraries seem to installed correctly, so I am not sure what is going wrong.

Thanks for your time, cheers slowJim

Note: I am using circuitpython 4.1.2

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Thu Feb 06, 2020 4:57 pm

Hi SlowJim,

Which guide have you been following to setup your secrets.py and timezone information?

mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Thu Feb 06, 2020 6:32 pm

Hi

Thanks for the response.

Originally I was using the Adafruit PyPortal Titano guide as I was setting up a Titano but realised the wifi chip is not working or damaged (that was going to be my next question). I then went on to use the PyPortal NASA Image of the Day Viewer guide to set up my standard PyPortal.

Cheers

James

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Thu Feb 06, 2020 7:00 pm

James,

We definitely need to get your internet going and make sure your secrets file looks good. Can you paste in the formatting of your secrets file as it sounds like there is an error in there we need to fix. I don't want to see your password or secret keys, but just to make sure the quotes, commas and colons all look good. This is from the example NASA PyPortal.

Code: Select all | TOGGLE FULL SIZE
 # This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'home ssid',
    'password' : 'my password',
    'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
    'github_token' : 'fawfj23rakjnfawiefa',
    'hackaday_token' : 'h4xx0rs3kret',
    }


mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Thu Feb 06, 2020 8:30 pm

Hi

Spot on! My secrets file was wrong, I must have overridden it at some stage. I was able to connect through my phone hotspot as I'm not at home. I still received and error:

Traceback (most recent call last):
File "code.py", line 32, in <module>
File "code.py", line 29, in <module>
File "adafruit_pyportal.py", line 740, in fetch
File "adafruit_pyportal.py", line 737, in fetch
File "adafruit_pyportal.py", line 733, in fetch
File "adafruit_pyportal.py", line 730, in fetch
File "adafruit_pyportal.py", line 586, in wget
KeyError: content-length

The area of code is:

26 while True:
27 response = None
28 try:
29 response = pyportal.fetch()
30 print("Response is", response)
31 except RuntimeError as e:
32 print("Some error occured, retrying! -", e)
33
34 time.sleep(30*60) # 30 minutes till next check

I am thinking that it might be because of the hotspot or the fact I don't have an SD card inserted.

Sorry for the hassle, I really appreciate your help.

Cheers

slowjim

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Fri Feb 07, 2020 4:45 pm

Let me know when you get a chance to give this setup a try with your home router (instead of phone tether). I can't tell from this error exactly what the issue, but dealing with one stable home WiFi will reduce the complexity.

mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Fri Feb 07, 2020 8:11 pm

Hi mikeysklar, happy Friday

I have connected to my home wifi and inserted a freshly formatted SD card. Unfortunately, I still have errors...

Set background to /pyportal_startup.bmp
ESP firmware: bytearray(b'1.2.2\x00')
Connecting to AP XXXXXXXXXXX
Set background to /nasa_background.bmp
Traceback (most recent call last):
File "code.py", line 24, in <module>
File "adafruit_pyportal.py", line 264, in __init__
File "adafruit_pyportal.py", line 260, in __init__
File "adafruit_sdcard.py", line 113, in __init__
File "adafruit_sdcard.py", line 145, in _init_card
File "adafruit_sdcard.py", line 236, in _cmd
OverflowError: value must fit in 1 byte(s)

Sorry I made an error in a previous post, I have a pyportal pynt, however from what I have read the code should still work. I have not made any aolterations to the code from the guide, I simply copied it and downloaded onto the board with the other required elements from the guide.

Thanks again for sticking with me.

Cheers

James

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by slowjim on Sat Feb 08, 2020 7:00 am

Hi

I have an update. After playing around a bit I decided to reload all the software and start again using the 5.x uf2 and lib. It seemed to get a bit further, eg it displayed the NASA splash but then went into error:

Traceback (most recent call last):
File "code.py", line 32, in <module>
File "code.py", line 29, in <module>
File "adafruit_pyportal.py", line 874, in fetch
File "adafruit_pyportal.py", line 871, in fetch
File "adafruit_pyportal.py", line 867, in fetch
File "adafruit_pyportal.py", line 861, in fetch
File "adafruit_pyportal.py", line 658, in wget
KeyError: content-length

I looked through the REPL. It connects to wifi, sets the background, retrieves the data...Reply is ok, shows the original URL, shows the converted URL, then goes to fetch the stream from adafruit io, then it throws the error. I get the same error whether I soft start it from the REPL or reset it using the button on the back, or connect it to an external power supply (5V from mains).
I looked back at similar problems on the forum but I couldn’t quite see where I had gone wrong. I have tried the portal with and without a 16GB SD card and get the same result.

Thanks again for your time

Cheers

James

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Sat Feb 08, 2020 6:55 pm

James,

Thank you for the update. Which example code are you running for the NASA PyPortal errors you are currently seeing. There are several on this page:

https://learn.adafruit.com/pyportal-nas ... et-connect

mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Sat Feb 08, 2020 8:56 pm

Hi mikeysklar

Thanks for your post

I ran through all the scripts on the page you sent just to make sure everything was in working order. I went on to the APOD code, applied my API and saved to the board and received the same error as before.

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Sun Feb 09, 2020 4:35 pm

James,

Please drop the code in-line here since I'm still not sure which APOD example is crashing out on you. just use the code button to embed it in the forum thread.

mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Sun Feb 09, 2020 5:30 pm

Hi mikeysklar

Please find the code below. Thanks

Code: Select all | TOGGLE FULL SIZE
import time
import board
from adafruit_pyportal import PyPortal

# Set up where we'll be fetching data from
DATA_SOURCE = "https://api.nasa.gov/planetary/apod?api_key=TEFHvH0LHbPHk1IvWIzD6gypz98lxasavBz6HUhe"
# There's a few different places we look for data in the photo of the day
IMAGE_LOCATION = ["url"]
TITLE_LOCATION = ["title"]
DATE_LOCATION = ["date"]

# the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=(TITLE_LOCATION, DATE_LOCATION),
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/nasa_background.bmp",
                    text_font=cwd+"/fonts/Arial-12.bdf",
                    text_position=((5, 220), (5, 200)),
                    text_color=(0xFFFFFF, 0xFFFFFF),
                    text_maxlen=(50, 50), # cut off characters
                    image_json_path=IMAGE_LOCATION,
                    image_resize=(320, 240),
                    image_position=(0, 0))

while True:
    response = None
    try:
        response = pyportal.fetch()
        print("Response is", response)
    except RuntimeError as e:
        print("Some error occured, retrying! -", e)

    time.sleep(30*60)  # 30 minutes till next check

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Sun Feb 09, 2020 6:52 pm

The libraries you are importing don't sound right for your setup if you have the Titano.

I think you will need to follow the titano guide and use code that looks more like this that includes all the necessary esp32 CircuitPython libraries that are required.

https://learn.adafruit.com/adafruit-pyp ... et-connect

Code: Select all | TOGGLE FULL SIZE
 # adafruit_requests usage with an esp32spi_socket
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(b'MY_SSID_NAME', b'MY_SSID_PASSWORD')
    except RuntimeError as e:
        print("could not connect to AP, retrying: ",e)
        continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)

# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "http://httpbin.org/get"
JSON_POST_URL = "http://httpbin.org/post"

print("Fetching text from %s"%TEXT_URL)
response = requests.get(TEXT_URL)
print('-'*40)

print("Text Response: ", response.text)
print('-'*40)
response.close()

print("Fetching JSON data from %s"%JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print('-'*40)

print("JSON Response: ", response.json())
print('-'*40)
response.close()

data = '31F'
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print('-'*40)

json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp['data'])
print('-'*40)
response.close()

json_data = {"Date" : "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print('-'*40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp['json'])
print('-'*40)
response.close()


mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Re: PyPortal Invalid Syntax

by slowjim on Mon Feb 10, 2020 1:02 am

Hi again

I ran the code you provided above. I get some errors with this code as well. The following is copied from REPL:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Connecting to AP...
Connected to XXXXXXXX RSSI: -42
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
Text Response: This is a test of Adafruit WiFi!
If you can read this, its working :)

----------------------------------------
Fetching JSON data from http://httpbin.org/get
Traceback (most recent call last):
File "code.py", line 47, in <module>
File "adafruit_requests.py", line 274, in get
File "adafruit_requests.py", line 236, in request
File "adafruit_requests.py", line 220, in request
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 109, in readline
RuntimeError: Didn't receive full response, failing out

Code

46 print("Fetching JSON data from %s"%JSON_GET_URL)
47 response = requests.get(JSON_GET_URL)
48 print('-'*40)

I put those libraries into the APOD code and I get the following (After setting the sleep) to 10 seconds:

Connecting to AP JPhoneX
Retrieving data...Some error occured, retrying! - Didn't receive full response, failing out
Retrieving data...Some error occured, retrying! - Timed out waiting for SPI char
Connecting to AP JPhoneX

It seems to be consistently doing this. Note after the second error, the pyportal tries to reconnect even though it is still connected.

Cheers

James

slowjim
 
Posts: 18
Joined: Fri Sep 27, 2019 3:49 am

Re: PyPortal Invalid Syntax

by mikeysklar on Mon Feb 10, 2020 5:06 pm

James,

While not fully functional the responses you are getting with the ESP32 libraries in the APOD code look promising.

The first response was not complete and the second timed out which makes me think it is possibly an issue with going through your phone. Are you able to use a stable WiFi?

mikeysklar
 
Posts: 734
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.