lua初学--如何避免init.lua PANIC

1、初学lua,测试如下代码,ESP8266使用详解--基于Lua脚本语言

    init.lua和connect_wifi.lua,上传至ESP8266后,也测试正常了,但是后面想修改代码,发现传不了了。模块提示:

ANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)

PANIC: unprotected error in call to Lua API (attempt to index a nil value)

然后模块自己重启。试了好多次都不行,使用nodemcu-flasher-master才能重新刷的lua程序。(菜鸟新手一脸泪啊!!!)

以上具体原因还没搞明白,在wifi部分有如下描述:

Important

The WiFi subsystem is maintained by background tasks that must run periodically. Any function or task that takes longer than 15ms (milliseconds) may cause the WiFi subsystem to crash. To avoid these potential crashes, it is advised that the WiFi subsystem be suspended with wifi.suspend() prior to the execution of any tasks or functions that exceed this 15ms guideline.


可能是因为程序在执行wifi部分的功能,必须先使用 wifi.suspend() 函数然后在上传程序。但是测试也不行,这个情况下ESP8266无法处理这些指令,已接收就PANIC了,然后重启。

2、查看 NodeMCU文档资料NodeMCU Documentation,发现有如下描述:

How do I avoid a PANIC loop in init.lua?

Most of us have fallen into the trap of creating an init.lua that has a bug in it, which then causes the system to reboot and hence gets stuck in a reboot loop. If you haven't then you probably will do so at least once.

  • When this happens, the only robust solution is to reflash the firmware.
  • The simplest way to avoid having to do this is to keep the init.lua as simple as possible -- say configure the wifi and then start your app using a one-time tmr.alarm() after a 2-3 sec delay. This delay is long enough to issue a file.remove("init.lua") through the serial port and recover control that way.
  • Another trick is to poll a spare GPIO input pin in your startup. I do this on my boards by taking this GPIO plus Vcc to a jumper on the board, so that I can set the jumper to jump into debug mode or reprovision the software.
  • Also it is always best to test any new init.lua by creating it as init_test.lua, say, and manually issuing a dofile("init_test.lua") through the serial port, and then only rename it when you are certain it is working as you require.

介绍了三个方法:

1)在 init.lua 在配置wifi之前,先延迟个2-3秒,如果想更新程序,可以在这段时间内通过串口发送file.remove("init.lua")来清除init.lua文件。然后重启。

2)在init.lua 代码里设置一个GPIO input,由这个GPIO来决定是否进入debug mode

3)使用 init_test.lua 代替 init.lua,然后使用dofile("init_test.lua")来执行文件。这个是文档推荐的best way。




你可能感兴趣的:(lua初学--如何避免init.lua PANIC)