
I can’t deny that a dual-core M0+ based board with built-in WiFi and Bluetooth radios for US$6 is a great bargain for tinkering with. As long as you were willing to use MicroPython, then you could program in a variant of Python all you wanted to. But I couldn’t configure the WiFi radio to be an access point. Now that there’s a version of Circuit Python that will work on this board (8.0.0.beta 4) and will work with the WiFi radio, I flashed one of my Pico Ws with this beta release and decided to give it a spin. The release notes say that I can configure the W’s WiFi as an access point. I’ve yet to try that.
Instead I decided to copy the following example code off the Adafruit site and exercise the WiFi radio with this. Note that this code uses the new .env
feature, where you can create an environment file with various parameters. In this case, my WIFI_SSID
and WIFI_PASSWORD
are in my .env
file instead of out in the open code. A very nice touch.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries## SPDX-License-Identifier: MITimport osimport timeimport sslimport wifiimport socketpoolimport microcontrollerimport adafruit_requestsimport json# Adafruit quotes URL#quotes_url = "https://www.adafruit.com/api/quotes.php"# Connect to your local hostspot.# WIFI_SSID and WIFI_PASSWORD should be defined in your .env 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("Fetching quotes from ", quotes_url)print()while True:try:time.sleep(10)# Get a single quote#response = session.get(quotes_url)dic = dict(json.loads(response.text)[0])pstr = "Quote: " + dic['text'] + "."print(pstr)pstr = "Author: " + dic['author']print(pstr)print()response.close()# pylint: disable=broad-exceptexcept ValueError as e:print("Error: bad JSON - ", response.text)response.close()except Exception as e:print("Error:\n", str(e))print("Resetting microcontroller in 10 seconds")time.sleep(10)microcontroller.reset()
I can’t recall where I copied the original code, but it’s undergone a number of tweaks, in particular parsing the JSON response from the URL. The original code simply dumped the response from the website. I parsed it, created a dictionary from it, then used the dictionary to cleanly print the quote and the quote’s author on two lines. I did some other cleanup, as well as adding an exception for malformed JSON so that the board isn’t reset every time a JSON response string gets munged. And if you’re wondering why I built a string rather than just printing it, printing a complex string adds an extra space between any two strings, and I’m just lazy and use string construction to build my final string. I’m sure there’s a better way for embedded CircuitPython, and if I find it, I’ll post an update. I’m also looking to find out what other specific exceptions I need to look for. Resetting a board on all exceptions is too lazy, even for me.
What follows is a typical run.
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.code.py output:Fetching quotes from https://www.adafruit.com/api/quotes.phpQuote: The nice thing about standards is that you have so many to choose from; furthermore, if you do not like any of them, you can just wait for next year's model.Author: Andrew S. TanenbaumQuote: If you want to find the secrets of the universe, think in terms of energy, frequency and vibration.Author: Nikola TeslaQuote: The nice thing about standards is that you have so many to choose from; furthermore, if you do not like any of them, you can just wait for next year's model.Author: Andrew S. TanenbaumQuote: What I cannot create, I do not understand.Author: Richard FeynmanQuote: Invention, it must be humbly admitted, does not consist in creating out of void, but out of chaos.Author: Mary Shelley...
You must be logged in to post a comment.