A common scheme for Lua scripted NodeMCU modules with automatically start the script init.lua is to incorporate some logic to test the condition of a GPIO pin to determine whether to boot to the application or drop to the lua prompt for programming etc. In fact the scheme can be elaborated to provide a simple multi level selection based on the time the input condition is applied.
The obvious pin to use is the pin that commonly has a “BOOT” or “FLASH” button on it, GPIO0 or D3. It is used to activate the ESP8266 boot loader if it is low during boot, so it must be left high at boot to allow the lua interpreter to run, but it can be pulled low shortly after boot up and tested from init.lua.
An example init script follows.
print("\n\nHold Pin00 low for 1s t0 stop boot.") print("\n\nHold Pin00 low for 3s for config mode.") tmr.delay(1000000) if gpio.read(3) == 0 then print("Release to stop boot...") tmr.delay(1000000) if gpio.read(3) == 0 then print("Release now (wifi cfg)...") print("Starting wifi config mode...") dofile("wifi_setup.lua") return else print("...boot stopped") return end end print("Starting app.lua") dofile("app.lua")
Above is a pic of the helper. The DIP switch allows selection of the BOOT pulse in 1s increments. It has four connections, ground, Vdd, BootOut, and Reset (optional). The button near the DIP switch resets the helper which in turn will apply a 10ms reset pulse to the Reset line.
The minimum connection is ground, Vdd (from the target), and BootOut; the device will initiate when the power is applied and this is the means to reset targets that do not have a usable reset line.
Above is the underside of the helper.
The helper uses a PIC12F510, C source code follows:
//Owen Duffy 2017-05-03 //Pins: //GP0-3 duration (seconds) //GP4 /RESET out (open collector) //GP5 /BOOT out (open collector) #include <xc.h> #define _XTAL_FREQ 4000000 #define DEBOUNCE 10 #pragma config MCLRE=ON,CP=OFF,WDT=OFF,IOSCFS=0,OSC=2 unsigned char i,sel; void main(void) { ADCON0=0x00; //disable analogue input C1ON=0; //disable comparator OPTION=0b10111111; TRIS=0b00101111; GPIObits.GP4=0; __delay_ms(10); //assert 10ms RESET GPIObits.GP4=1; TRIS=0b00111111; while(1){ for(i=DEBOUNCE;--i;){ __delay_ms(10); if(!GPIObits.GP4)i=DEBOUNCE; } __delay_ms(580); //wait for ESP8266 boot sel=~(GPIO)&0x07; if(sel){ TRIS=0b00011111; GPIObits.GP5=0; for(;sel--;) __delay_ms(1000); GPIObits.GP5=1; TRIS=0b00111111; } while(GPIObits.GP4); //wait for RESET } }
The device emits a 20ms low reset pulse on GP4 on power up, and about 700ms after power up emits a timed low ‘Boot’ pulse on GP5 for interaction with the init.lua script … The DIP switches set the duration of the Boot pulse in seconds.
The capture above shows the power trace, the tx line from the ESP8266 showing its activity, the ResetOut line and the BootOut line. The traffic on the early part of the BootOut line is from the ESP8266.
If you choose to connect the ResetOut line to the target, resets on that line will also recycle the helper.
The helper has turned out to work very well with the modified relay board, and with ESPlorer, it makes development and testing nearly as convenient as the NodeMCU dev board.