Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / mdns_finder.py
Created August 31, 2022 02:03
CircuitPython mDNS finder for ESP32-S2/S3
import time
import wifi
import ipaddress
import socketpool
import mdns
from secrets import secrets
MDNSFINDTIMEOUT = 5
pool = socketpool.SocketPool(wifi.radio)
@anecdata
anecdata / wwotg.md
Last active September 16, 2023 03:35
Web Workflow OTG

Using iOS to place files onto a CircuitPython [CP] device has been possible for several years: https://blog.adafruit.com/2019/09/19/program-circuitpython-devices-with-iphone-ios-13/ https://learn.adafruit.com/use-circuitpython-devices-with-iphone-ipad/copy-edit-paste

However, iOS doesn't support USB CDC, so there is no REPL or serial capability.

Goal:

A "traveling light" configuration for setting up and using CircuitPython Web Workflow without a large computer. Basic ingredients would be an iPhone, one or two small CP microcontrollers (QT Py ESP32-S2 for example, but perhaps including a device without native-USB, like ESP32-C3 or ESP32), and assorted accessories.

@anecdata
anecdata / code.py
Created March 20, 2022 03:37
UDP Server ESP32SPI read-only (very rudimentary)
import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
from secrets import secrets
@anecdata
anecdata / code.py
Last active February 11, 2022 04:32
HTTPS request via TCP Socket - CircuitPython on Espressif
# initial read only (typically headers)
import wifi
import socketpool
import ssl
import ipaddress
from secrets import secrets
HOST = "example.com"
PATH = "/"
@anecdata
anecdata / code.py
Last active August 29, 2022 04:28
Simple HTTPS request - CircuitPython on Espressif
import wifi
import socketpool
import ssl
import adafruit_requests
# local
from secrets import secrets
URL = "https://example.com"