Development of NodeMCU

Subsection 3.5: File Operation

NodeMCU uses files to save Lua code because of the illustrative feature of Lua. Lua source program can be compiled and run online. Therefore, file operation is very important.

NodeMCU can support many types of file operations, like upload, delete, compile to binary file, run, etc. Moreover, LuaLoader also provides the richful file operations.

(1) "Upload File":upload the prepaired Lua source code into NodeMCU. For example, upload init.lua is shown as folows.

print("NodeMCU Started")
print("Type something to start")

click "Upload" to complete the upload function.


The uploaded file can be shown in the below of "Upload File". The button " " beside "Upload File" is used to open the Lua file and edit it.

(2) The button "cat" is used to check and read the file.

For example, if the following code is downloaded into the NodeMCU.

print("\n\ncat init.lua\n") "init.lua", "r") 
while true 
line = file.readline() 
if (line == nil) then break 
print(string.sub(line, 1, -2)) 

where tmr.wdclr() is used to clear the watchdog to avoid to trig it. The results is as follows.

cat init.lua
print("NodeMCU Started")
print("Type something to start")

Tips:as for a long-time cycle or traffic, tmr.wdclr() is called to clear the watchdog counter, and to avoid overflow and restart.

(3) run the files

Click the button "dofile" to run this file, and output the result, similar to dofile("init.lua").

dofile(init.lua)  19, Feb.,2015  16:07:24
NodeMCU Started
Type something to start

(4) file compile

click "compile" button to compile, similar to node.compile(). After compile, "" is generate automatically.


However, if the "print("NodeMCU Started")" in init.lua is changed into "print("NodeMCU Started"", then an error would happen with an error prompt.


  stdin:1: init.lua:3: ')' expected (to close '(' at line 2)

  near 'print'

 Tips: the file format of Lua binary is If the source code of .lua is changed into binary file, the program efficiency would be improved, and can reduce the memory usage. So, .lc file is suggested to used in the program.

(5) run lc file

click "dolc" to run the lc file, and output the results. it is similar to dofile(""). The result is the same as that of dofile("").

(6) file list

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

  The results is as follows.

  init.lua 70 bytes 160 bytes

(7) delete file

click "remove", which is similar to the "file.remove("file.lua")".

For more details about LuaLoader, please visit:

(8) format file system

click "Format" button to format the file system, which is similar to the

format done.