Skip to content

Hard crash running httpserver on ESP32S2 #8674

Open
@efc

Description

@efc

CircuitPython version

Adafruit CircuitPython 8.2.8 on 2023-11-16; Adafruit QT Py ESP32S2 with ESP32S2

Code/REPL

# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import board
import neopixel
import mdns
import socketpool
import wifi

from adafruit_httpserver import Server, Route, as_route, Request, Response, GET, POST

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "efcx"
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)

pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, "/static", debug=True)

pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)

@server.route("/")
def base(request: Request):
    """
    Serve a default static plain text message.
    """
    return Response(request, "Hello from the CircuitPython HTTP Server!")

# This is the simplest way to register a route. It uses the Server object in current scope.
@server.route("/change-neopixel-color", GET)
def change_neopixel_color_handler_query_params(request: Request):
    """Changes the color of the built-in NeoPixel using query/GET params."""

    # e.g. /change-neopixel-color?r=255&g=0&b=0

    r = request.query_params.get("r") or 0
    g = request.query_params.get("g") or 0
    b = request.query_params.get("b") or 0

    pixel.fill((int(r), int(g), int(b)))

    return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")


# This is another way to register a route. It uses the decorator that converts the function into
# a Route object that can be imported and registered later.
@as_route("/change-neopixel-color/form-data", POST)
def change_neopixel_color_handler_post_form_data(request: Request):
    """Changes the color of the built-in NeoPixel using POST form data."""

    data = request.form_data  # e.g. r=255&g=0&b=0 or r=255\r\nb=0\r\ng=0
    r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0)

    pixel.fill((int(r), int(g), int(b)))

    return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")


def change_neopixel_color_handler_post_json(request: Request):
    """Changes the color of the built-in NeoPixel using JSON POST body."""

    data = request.json()  # e.g {"r": 255, "g": 0, "b": 0}
    r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0)

    pixel.fill((r, g, b))

    return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")


# You can always manually create a Route object and import or register it later.
# Using this approach you can also use the same handler for multiple routes.
post_json_route = Route(
    "/change-neopixel-color/json", POST, change_neopixel_color_handler_post_json
)


def change_neopixel_color_handler_url_params(
    request: Request, r: str = "0", g: str = "0", b: str = "0"
):
    """Changes the color of the built-in NeoPixel using URL params."""

    # e.g. /change-neopixel-color/255/0/0

    pixel.fill((int(r), int(g), int(b)))

    return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")


# Registering Route objects
server.add_routes(
    [
        change_neopixel_color_handler_post_form_data,
        post_json_route,
        # You can also register a inline created Route object
        Route(
            path="/change-neopixel-color/<r>/<g>/<b>",
            methods=GET,
            handler=change_neopixel_color_handler_url_params,
        ),
    ]
)


server.serve_forever(str(wifi.radio.ipv4_address))

Behavior

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.

NeoPixel on board flashing three times every second or so.

Description

I left this code (pretty much just sample code) running on the board overnight while attached to my computer via USB. I was able to visit the website a few times (just going to root). But in the morning I found the board in safe mode with this message on the serial panel of mu.

Additional information

I have no idea if the httpserver just crashed after some period of time or if the Mac did something to kill the attached board.

Generally, how long and adafruit_httpserver servers run without cycling the board? If the board does need to be reset every now and then for stability, how often do you recommend doing that?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions