Search code examples
iotesp8266nodemcumicropython

How can I reset ESP8266 MicroPython after main.py crashes?


I have a NodeMCU ESP8266 board running MicroPython. I'm running a web server on my ESP8266. This is my first IoT project based on one of these boards.

The below is a snippet of the code.

This is being executed within main.py. Every now and then, something causes the code to crash (perhaps timing and request based). When main.py exits, for whatever reason, I'm dropped back at the python CLI.

I'd like for the board to reset when this happens (if there isn't a better way).

What is the best method of restarting/reseting the ESP8266?

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print('listening on', addr)

while True:
    cl, addr = s.accept()
    print('client connected from', addr)
    cl_file = cl.makefile('rwb', 0)
    print("Request:")
    while True:
        line = cl_file.readline()
        print("Line:" , line)
        if not line or line == b'\r\n':
            print("breaking")
            break
        if line == b'GET /active HTTP/1.1\r\n':

Solution

  • You could execute your code (which should be outside of the main.py -> other file) from the boot or the main.py. if it drops out it should execute the following code, which could trigger a reset.

    You may have to catch the error first.

    I hope I helped