CircuitPython 8.x
I want to connect to Wifi once on boot and then listen on that IP address later with HttpServer in code.py and not disconnect and reconnect during soft reload.
In my boot.py I did the following:
Code: Select all
import wifi
from secrets import secrets # pylint: disable=no-name-in-module
ssid, password = secrets["WIFI_SSID"], secrets["WIFI_PASSWORD"] # pylint: disable=no-member
print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
print("Connected to", ssid)
Code: Select all
import board
import neopixel
import socketpool
import busio
import mdns
import microcontroller
from adafruit_httpserver.mime_type import MIMEType
from adafruit_httpserver.request import HTTPRequest
from adafruit_httpserver.response import HTTPResponse
from adafruit_httpserver.server import HTTPServer
from adafruit_httpserver.methods import HTTPMethod
mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "esp32s3"
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)
pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool)
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
@server.route("/cpu-information")
def cpu_information_handler(request: HTTPRequest):
"""
Return the current CPU temperature, frequency, and voltage as JSON.
"""
data = {
"temperature": microcontroller.cpu.temperature,
"frequency": microcontroller.cpu.frequency,
"voltage": microcontroller.cpu.voltage,
}
with HTTPResponse(request, content_type=MIMEType.TYPE_JSON) as response:
response.send(json.dumps(data))
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
server.serve_forever(str(wifi.radio.ipv4_address))
If I follow the tutorial in the httpserver docs and connect to wifi in code.py, everything works. https://docs.circuitpython.org/projects ... -with-mdns
Why this matters is disconnecting and reconnecting to wifi on every soft reload takes a while and really slows me down.
I dont understand why I cannot connect to wifi once on boot, and then my code cant just grab the wifi address later?