Skip to content

Instantly share code, notes, and snippets.

@anecdata
anecdata / about.py
Last active August 31, 2022 02:09
CircuitPython: basic info about a device
print(f"\nBOARD")
import os
print(f"Name: {os.uname().machine}")
import microcontroller
uid = ''.join('%02X' % _ for _ in microcontroller.cpu.uid)
print(f"CPU UID: {uid}")
import sys
print(f"Platform: {sys.platform}")
import board
print(f"board_id: {board.board_id}")
@anecdata
anecdata / cadata.py
Last active February 25, 2023 00:12
Espressif port: context.load_verify_locations(cadata=CA_STRING)
import wifi
import socketpool
import ssl
import adafruit_requests
# local
from secrets import secrets
URL = "https://jwst.nasa.gov/content/webbLaunch/flightCurrentState2.0.json?unique=1643741752388"
@anecdata
anecdata / RADS.md
Last active November 1, 2022 15:36
RADS: Redundant Array of mDNS http Servers

In the configuration pictured below, multiple identically-configured Adafruit CircuitPython 8.0.0-beta.0 on 2022-08-18; Adafruit QT Py ESP32S2 with ESP32S2 are used to create a Redundant Array of mDNS http Servers. The devices could be physically together or scattered around, as long as they are on the same LAN. Once configured, no USB connection is necessary for operation.

Each is running CircuitPython 8 Web Workflow: https://docs.circuitpython.org/en/latest/docs/workflows.html#web

Web Workflow automatically connects to a Wi-Fi Access Point based on credentials in a /.env file in the root of the CIRCUITPY drive filesystem, and creates an HTTP Server (port can be changed with CIRCUITPY_WEB_API_PORT= in the /.env file; it's port 80 by default).

In code.py, run an HTTP Server using (for example): https://docs.circuitpython.org/projects/httpserver/en/latest/api.html Make sure that the ports are different between the Web Workflow HTTP Server and the code.py HTTP Server (e.g., set

@anecdata
anecdata / ntp.py
Last active February 23, 2023 19:22
NTP: manual or automatic (for CircuitPython ports with native wifi) & alternative(s)
import time
import rtc
import wifi
import socketpool
import adafruit_ntp
from secrets import secrets
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
@anecdata
anecdata / code.py
Created September 29, 2022 00:23
Raspberry Pi Pico W CircuitPython requests code for CP issue #6958
import time
import supervisor
import microcontroller
import wifi
import socketpool
import adafruit_requests
from secrets import secrets
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
@anecdata
anecdata / main.py
Created September 29, 2022 00:24
Raspberry Pi Pico W MicroPython requests code for uP issue #9455
import time
import machine
import network
import urequests as requests
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("ssid", "passw0rd")
@anecdata
anecdata / code.py
Last active October 8, 2022 05:47
CircuitPython: Raspberry Pi Pico W + WIZnet W5100S Ethernet Hat
import os
import board
import busio
import digitalio
import time
import adafruit_wiznet5k.adafruit_wiznet5k
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as wiznet5k_socket
@anecdata
anecdata / code.py
Last active December 15, 2022 20:36
wifi connect regression-tester
import os, time, wifi, socketpool
from secrets import secrets
SLEEP = 1
pool = socketpool.SocketPool(wifi.radio)
def connect():
try:
wifi.radio.connect(secrets['ssid'], secrets['password'])
@anecdata
anecdata / code.py
Last active October 30, 2024 16:00
CircuitPython asyncio HTTP server
# SPDX-FileCopyrightText: 2023 anecdata
#
# SPDX-License-Identifier: MIT
import time
import traceback
import asyncio
import board
import digitalio
import wifi
@anecdata
anecdata / code.py
Last active October 2, 2023 02:59
CircuitPython WIZnet Ethernet Tester
import os
import board
import busio
import digitalio
import time
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_wiznet5k.adafruit_wiznet5k
import adafruit_requests