circuitpython 8.0 rc 1

A new version of CircuitPython is near final release. Version 8 RC 1 was released today (yesterday, actually, but it wasn’t officially documented until today). I’m interested because now there’s an official CircuitPython released specifically for the Raspberry Pi Pico W ( https://circuitpython.org/board/raspberry_pi_pico_w/ ). I’m starting with one of my Pico W boards, and then moving around to all the devices that currently run 7.3.3. So far nothing has leaped out as a problem, but these are very early days. I know that WiFi works, but I don’t know if Bluetooth is working or not.

One application I’ve been tweaking along the way is an automatic quote displayer that queries the Adafruit quote server and prints it out. The response comes back in JSON. The original code, written for CP 7, only printed the response out in a raw format. This version parses the JSON and line wraps long quotes to less than 80 columns. Here’s a typical output.

...
   Injustice anywhere is a threat to justice everywhere.
 - Martin Luther King, Jr.

   I never am really satisfied that I understand anything; because, understand 
   it well as I may, my comprehension can only be an infinitesimal fraction 
   of all I want to understand.
 - Ada Lovelace

   Adversity is revealing of character.
 - Unknown

   The most exciting phrase to hear in science, the one that heralds new discoveries, 
   is not 'Eureka!' but 'That's funny...'.
 - Isaac Asimov

   The whole point of getting things done is knowing what to leave undone.
 - Oswald Chambers
...

And here’s the code.

# Copyright (c) 2023 William H. Beebe, Jr.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Derived/modified from"
# 2022 Liz Clark for Adafruit Industries

import os
import time
import ssl
import wifi
import socketpool
import microcontroller
import adafruit_requests
import json
import gc

# Adafruit quotes URL
#
quotes_url = "https://www.adafruit.com/api/quotes.php"

# Connect to a local access point.
# WIFI_SSID and WIFI_PASSWORD should be defined in your settings.toml file.
#
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))

pool = socketpool.SocketPool(wifi.radio)
session = adafruit_requests.Session(pool, ssl.create_default_context())
print("\n\nFetching quotes from {}\n".format(quotes_url))
wrap_limit = 72

while True:
    try:
        time.sleep(5)
        gc.collect()

        # Get a quote. Parse the JSON response.
        #
        response = session.get(quotes_url)
        dic = dict(json.loads(response.text)[0])
        qstr = dic['text'] + "."

        # Primitive line wrapping for long text lines.
        #
        while len(qstr) > wrap_limit:
            cut = qstr.find(" ", wrap_limit, len(qstr)) + 1
            if cut > 0:
                print("   {}".format(qstr[:cut]))
                qstr = qstr[cut:]
            else:
                break

        if len(qstr) > 0:
            print("   {}".format(qstr))
        print(" - {}\n".format(dic['author']))

        response.close()
        gc.collect()
    # pylint: disable=broad-except
    except ValueError as e:
        print("Error: bad JSON")
        response.close()
    except Exception as e:
        print("Error:\n", str(e))
        print("Resetting microcontroller in 10 seconds")
        time.sleep(10)
        microcontroller.reset()

As they say, more to come.

One thought on “circuitpython 8.0 rc 1

Comments are closed.