Machine.reset() bricks board. #9270
-
Hi! I've tried to follow the Raspberry Pi Foundation's web server guide to create an API on my ESP8266 but upon CTRL+C (it doesn't happen all the time) the board will lock up and become unresponsive to all actions (saving code, running code, reading new files, seeing serial console or erasing the flash with esptool) the one other time this hapened pluging the board in while holding reset and repeatidly trying to flash a new version of micropython worked but that doesn't seam to work this time. Here's the code, it uses a lot of self-written modules but I don't think many are relevent here. Only the main.py import shift_register_driver as driver
from machine import Pin
import time, machine, wifi
try:
input("start?")
ip = wifi.connect()
connection = wifi.open_socket(ip)
wifi.serve(connection)
except KeyboardInterrupt:
machine.reset() wifi.py import network
import socket
from time import sleep
import machine
from secrets import secrets
from connection_handler import connection_handler
ssid = secrets["ssid"]
password = secrets["password"]
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print("Connected on " + ip)
return ip
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def serve(connection):
#Start a web server
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
connection_handler(request[1:])
client.send("Fancy webpage")
client.close() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Because I don't understand what happens in your main.py I would try: import utime, wifi
try:
ip = wifi.connect()
connection = wifi.open_socket(ip)
wifi.serve(connection)
except:
print ('something went wrong')
utime.sleep(10) Sometimes I find it helpful to throw some 1 second delays in, ie between the three statements in the try |
Beta Was this translation helpful? Give feedback.
-
Rather than implementing web servers from scratch, I wish more tutorials recommended using a proper web server. e.g. microdot https://github.com/miguelgrinberg/microdot In general, it is almost impossible to truly "brick" a device. To recover your device it is not sufficient to just re-flash MicroPython because you also need to erase the filesystem. Using the (On STM32/pyboard we have a way to trigger a filesystem reset without re-flashing, it would be good to add that to the other ports, especially rp2). I'm not sure why your program is causing this to happen though. In general what @davefes suggests is a good idea. Another option is to add a time.sleep(1) to the start of your main.py so you always have a chance to Ctrl-C before your code starts running. |
Beta Was this translation helpful? Give feedback.
Rather than implementing web servers from scratch, I wish more tutorials recommended using a proper web server. e.g. microdot https://github.com/miguelgrinberg/microdot
In general, it is almost impossible to truly "brick" a device. To recover your device it is not sufficient to just re-flash MicroPython because you also need to erase the filesystem. Using the
erase_flash
option to esptool before re-flashing the firmware to your esp8266 will solve this.(On STM32/pyboard we have a way to trigger a filesystem reset without re-flashing, it would be good to add that to the other ports, especially rp2).
I'm not sure why your program is causing this to happen though. In general what @davefes su…