Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active November 1, 2022 15:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anecdata/e446693b7791cc65e90600a4ed43d4df to your computer and use it in GitHub Desktop.
Save anecdata/e446693b7791cc65e90600a4ed43d4df to your computer and use it in GitHub Desktop.
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 the code.py HTTP Server to listen on port 8080).

This allows access to the Web Workflow HTTP Server for the CIRCUITPY filesystem and the code.py HTTP Server for arbitrary response bodies or file serving.

Further, since any one of these devices may respond to http://circuitpython.local:8080 due to automatic mDNS configuration as part of Web Workflow, there is redunancy on the LAN for the features of the code.py server. Typically, hosts will cache the IP address associated with the .local name for about two minutes, so access at any arbitrary time could get a response from any one of the RADS devices. RADS devices can be added or taken away (or go down) at will, but as long as one is up the server features are available.

@anecdata
Copy link
Author

anecdata commented Sep 22, 2022

import time
import traceback
import socketpool
import wifi
import rtc
import adafruit_httpserver
import adafruit_ntp

PORT = 8080
TZOFFSET = -5

pool = socketpool.SocketPool(wifi.radio)
server = adafruit_httpserver.HTTPServer(pool)
ntp = adafruit_ntp.NTP(pool, tz_offset=TZOFFSET)
rtc.RTC().datetime = ntp.datetime

@server.route("/")
def base(request):
    return adafruit_httpserver.HTTPResponse(filename="/boot_out.txt")

@server.route("/test")
def base(request):
    return adafruit_httpserver.HTTPResponse(body=f"Success\n{time.localtime()}")

print(f"Listening on http://{wifi.radio.ipv4_address}:{PORT}")
server.start(str(wifi.radio.ipv4_address), port=PORT)
while True:
    try:
        if not wifi.radio.ipv4_address:
            print(f"Reconnecting...")
            wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
        server.poll()
    except Exception as ex:
        traceback.print_exception(ex, ex, ex.__traceback__)
        continue

@anecdata
Copy link
Author

RADS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment