Skip to content

HTTP Server crash into safe mode #10428

Closed
@kenidowa

Description

@kenidowa

CircuitPython version and board name

Adafruit CircuitPython 9.2.4 on 2025-01-29; Adafruit QT Py ESP32 PICO with ESP32
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.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.

Code/REPL

# SPDX-FileCopyrightText: 2023 Michał Pokusa
#
# SPDX-License-Identifier: Unlicense

from asyncio import create_task, gather, run
from asyncio import sleep as async_sleep

import board
import microcontroller
import neopixel
import socketpool
import wifi

from adafruit_httpserver import GET, Request, Response, Server, Websocket

#******************
print(dir(microcontroller))
print(dir(microcontroller.Processor))
print(dir(microcontroller.cpu))
print(microcontroller.cpu.frequency)
print(microcontroller.cpu.voltage)


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

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

websocket: Websocket = None

HTML_TEMPLATE = """
<html lang="en">
    <head>
        <title>Websocket Client</title>
    </head>
    <body>
        <p>Cycle Count: <strong>-</strong></p>
        <p>NeoPixel Color: <input type="color"></p>
        <script>
            const cpuTemp = document.querySelector('strong');
            const colorPicker = document.querySelector('input[type="color"]');

            let ws = new WebSocket('ws://' + location.host + '/connect-websocket');

            ws.onopen = () => console.log('WebSocket connection opened');
            ws.onclose = () => console.log('WebSocket connection closed');
            ws.onmessage = event => cpuTemp.textContent = event.data;
            ws.onerror = error => cpuTemp.textContent = error;

            colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200);

            function debounce(callback, delay = 1000) {
                let timeout
                return (...args) => {
                    clearTimeout(timeout)
                    timeout = setTimeout(() => {
                    callback(...args)
                  }, delay)
                }
            }
        </script>
    </body>
</html>
"""


@server.route("/client", GET)
def client(request: Request):
    return Response(request, HTML_TEMPLATE, content_type="text/html")


@server.route("/connect-websocket", GET)
def connect_client(request: Request):
    global websocket

    if websocket is not None:
        websocket.close()  # Close any existing connection

    websocket = Websocket(request)

    return websocket


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


async def handle_http_requests():
    while True:
        server.poll()

        await async_sleep(0)


async def handle_websocket_requests():
    while True:
        if websocket is not None:
            if (data := websocket.receive(fail_silently=True)) is not None:
                r, g, b = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)
                pixel.fill((r, g, b))

        await async_sleep(0)


async def send_websocket_messages(cpu_temp):
    while True:
        if websocket is not None:
            #cpu_temp = round(microcontroller.cpu.frequency, 0)
            cpu_temp +=1
            #cpu_temp = round(0.5, 2)
            websocket.send_message(str(cpu_temp), fail_silently=True)

        await async_sleep(1)


async def main():
    await gather(
        create_task(handle_http_requests()),
        create_task(handle_websocket_requests()),
        create_task(send_websocket_messages(0)),
    )


run(main())


ruit CircuitPython 9.2.4 on 2025-01-29; Adafruit QT Py ESP32 PICO with ESP32

Behavior

OUTPUT from Serial port:

soft reboot

code.py output:
['__class__', '__name__', 'Pin', 'Processor', 'ResetReason', 'RunMode', '__dict__', 'cpu', 'delay_us', 'disable_interrupts', 'enable_interrupts', 'nvm', 'on_next_reset', 'pin', 'reset', 'watchdog']
['__class__', '__name__', '__bases__', '__dict__', 'frequency', 'reset_reason', 'temperature', 'uid', 'voltage']
['__class__', 'frequency', 'reset_reason', 'temperature', 'uid', 'voltage']
240000000
None
Started development server on http://192.168.4.235:5000
192.168.5.13 -- "GET /client" 438 -- "200 OK" 1244 -- 31ms
192.168.5.13 -- "GET /connect-websocket" 511 -- "101 Switching Protocols" 149 -- 31ms
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 271414342, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:396
ho 0 tail 12 room 4
load:0x40078000,len:13892
load:0x40080400,len:4
load:0x40080404,len:3160
entry 0x40080558
Serial console setup

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.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.
Press reset to exit safe mode.

Description

Run this code on ESP32-PICO ran for about 2 days and then crashed hard. This code comes from
This link: https://learn.adafruit.com/networking-in-circuitpython/http-server-examples

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions