First steps


Connecting the board

Connect your NodeMcu board to an USB port and open ESPlorer.

To recognize the serial port, you must connect the device before opening ESPlorer or click the refresh button after plugging the board.

Choose the right COM port (1), click on "Open" (2), and set your baud rate to 115200 (3).

Press the reset button at your NodeMcu board.

If you don't see the Lua prompt...

... but instead you see garbage like this:

... change your baud rate (3) and reset the board until you get the Lua prompt. Usually the boards work with 115200, but I have seen different baud rates. I don't know why yet.

I find that you have to reset the board often to get things done. I reset it after operations like uploading programs and sometimes even before the operation, to make sure Lua prompt is available. My suggestion is: when in doubt, RESET IT!

Hello world

Type print "hello world" in the text field at the bottom right (1), click the "Send" button (2). You should see "hello world" at the console (3).

Note that to copy and paste commands at ESPlorer you must use ctrl-C ctrl-V. Right clicks don't work.

Light up a a led

Type gpio.mode(4, gpio.OUTPUT) and click the "Send" button.

Now type gpio.write(4, gpio.LOW) and click "Send". A led should light up at your board. The led lights at LOW because it is connected to a pull-up resistor.

If the led doesn't light up, try with other pin numbers instead of 4. This is a recurrent issue when working with NodeMcu boards. Manufactures and developers seem to change the pin definitions more than they should.

First program (just sent, not uploaded)

Copy and paste the code below to the left window of ESPlorer:

gpio.mode(4, gpio.OUTPUT)
while 1 do
  gpio.write(4, gpio.HIGH)
  tmr.delay(1000000)         -- just waits here 1,000,000 us = 1 second
  gpio.write(4, gpio.LOW)
  tmr.delay(1000000)         -- just waits here 1,000,000 us = 1 second
end

Then click at "Send to ESP" button.

Your led should be blinking once every second.

The "Send to ESP" button just sends the text over the serial line. Its like typing every line and clicking the "Send" button.

Saving (uploading) a program to the the NodeMcu

Save your program in your computer as "blink.lua" using the "Save" button at the top left. Then (reset the board and) click on the "Save to ESP" button at the bottom left.

To run it, reset your board, type dofile ("blink.lua")and click "Send" . Your program will run and the led will blink again.

Listing your files

So far the best way I found to list the files is (reseting your board and) sending this line:

for k,v in pairs(file.list()) do l = string.format("%-15s",k) print(l.."   "..v.." bytes") end

You will get a nice, formated list of your files with their sizes at the console window.

I suggest you add this line to the "snippets" that ESPlorer has. Those are lines of code that get sent by clicking a button:

If you use that command to list files now, since we only have "blink.lua", you get:

A list of all file commands can be found here: https://nodemcu.readthedocs.io/en/master/en/modules/file/

Compiled programs

You may want to send you file as a "semi-compiled" file (bytecode) to the NodeMcu, as these kind of files will run faster. To do that, you click on "Save&Compile" or "Save&Compile&Run". If you do that with our blink.lua, you will have:

Interesting that the compiled program (blink.lc) is larger than the non-compiled form. I believe that is not always the case.

The init.lua file

When you turn on your board or reset it, NodeMcu always looks for a file named "init.lua" to execute. If you know MS-DOS, this is a kind of AUTOEXEC.BAT. This allows you to have a program that will run when the board is powered-on, instead of waiting for commands from the serial port.

You may have noticed that when you reset your board, NodeMcu informs that:

Be careful when you create or rename a file to "init.lua". If the program there enters a loop, there is no way of exiting, and you will probably have to re-flash your firmware.

results matching ""

    No results matching ""