ESP32-S3 CircuitPython - how to connect to wifi only on boot and not soft reload?

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Adatory
 
Posts: 10
Joined: Sun Jun 05, 2022 1:07 pm

ESP32-S3 CircuitPython - how to connect to wifi only on boot and not soft reload?

Post by Adatory »

ESP32-S3 TFT
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)
In my code.py I did the following

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))
The server in code.py ends up saying "None" for 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?

User avatar
Adatory
 
Posts: 10
Joined: Sun Jun 05, 2022 1:07 pm

Re: ESP32-S3 CircuitPython - how to connect to wifi only on boot and not soft reload?

Post by Adatory »

I learned about the settings.toml and it works great for just staying connected to wifi.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”