Jul 20

Recover from PANIC - ESP8266

Category: Design,electronics   — Published by goeszen on July 20, 2017 at 2:39 pm

So you've managed to upload a faulty init.lua script? Is your console output constantly complaining about "PANIC: unprotected error in call to Lua API (attempt to index a nil value)" and the LEDs on the board are either flashing or lighting up in quick succession? Well, then that means your ESP8266 NodeMCU has entered an infinite reboot crash loop.

- Just uploading a working init.lua won't help, as the PANIC messages keep interrupting the upload process, right?

- Re-flashing the whole firmware doesn't work either, right? (at least for me). It seems the filesystem eeprom / flash / whatever survives the replacement of the firmware...

Here's the simple fix:
Use ESPlorer (or some other serial console) to connect to the board. Ignore the PANIC messages and keep sending this lua command to delete the init.lua file:

Enter "file.remove("init.lua")" into the execute-single-command (or whatever it is called) box on the bottom-right corner of ESPlorer and keep mashing the "Send" button until one command slips through and NodeMCU recovers. That's it. After that you've got acces to the device again and can continue to upload (working) lua scripts from there on.


You then have to *really* flash the entire memory of the ESP8266. Just uploading a new firmware isn't enough. I've had a PANIC situation where my init.lua was triggering the crash/reboot after a few milliseconds, and that wasn't enough to apply the above trick of removing the file, as even executing this single command took longer. And flashing the firmware replaced the firmware but the init.lua file was still around.

The solution in this situation was to use a blank.bin flash-file and hit the memory area where the file is (and a bit of everything else along the way - really wiping/destroying what's on the ESP.

I've followed Pratik Panda's experience here. He is also offering a blank .bin file if you can't find it in NodeMCU's repository (like me).
So I went into flash mode on the NodeMCU by pressing Flash and RST simulateneously. Then executed:

$ sudo python ../esptool/esptool.py --port /dettyUSB0 write_flash -fm=dio 0x000000 ../nodemcu-firmware/blank_1MB.bin

to hit the 0x000000 memory area, and did a reset. The NodeMCU was dead and silent. Good. After wiping it, I re-flashed a new firmware, in my case:

$ sudo python ../esptool/esptool.py --port /dettyUSB0 write_flash -fm=dio -fs=32m 0x00000 ../nodemcu-firmware/nodemcu_float_0.9.6-dev_20150704.bin

and the device came back up with the well known "no init.lua found" (or similar) message. Why that works, and writing the firmware alone, also starting at 0x00000 (ok, one zero less here...) I don't know. I've also omitted the overall size of the blank.bin file, ... It worked, kinda brute-force.
Could be that esptool's erase-flash switch does the same thing, for example like so:

esptool.py --port erase_flash

(via) - but I haven't explored that yet.

Anyway. Ready for more tinkering.


  • One way to prevent this is using a "check first" init.lua script, as described here ( Can't recover from PANIC unprotected error in call) - but I don't like the delay / detaour that introduces.
  • Related to the idea of using a wrapper script (as mentioned above) is to execute in-development scripts only via the dofile("") facility and always under a name other than init.lua - like testing.lua. Only when you're sure it works, you switch to actually the automatically executed init.lua file-name.
  • esptool offers an erase_flash switch, but I haven't tried it yet.
  • It seems (as mentioned in this question "How to recover from infinite reboot loops in NodeMCU?") (as I understand it) that it's possible to just flash / erase the filesystem area of the board's memory (or what is Urmil doing by flashing blank.bin here??) I've used this knowledge in the above "If that doesn't work" brute force solution, but it seems (seems! I don't know how) you can hit the file-system more precisely.
  • I don't know if this is a 0.9.6 firmware issue only.


This post is part of a series of Mini Tutorials on the ESP8266:
Previous post: Blink the internal LED
Next post: So where is pin D1??

I'm tinkering here with a NodeMCU ESP8266 board.
I'm using the ESP8266 Java IDE ESPlorer.
The firmware is version 0.9.6 build 20150704 powered by Lua 5.1.4

Leave a Reply