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("\n")
print("NodeMCU Started")
print("Type something to start")

click "Upload" to complete the upload function.

Tips:

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") 
file.open( "init.lua", "r") 
while true 
do 
line = file.readline() 
if (line == nil) then break 
end 
print(string.sub(line, 1, -2)) 
tmr.wdclr()
end 
file.close()

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

cat init.lua
print("\n")
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
dofile("init.lua")
NodeMCU Started
Type something to start

(4) file compile

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

  node.compile("init.lua")

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

  node.compile("init.lua")

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

  near 'print'

 Tips: the file format of Lua binary is Lua.lc. 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("init.lc"). The result is the same as that of dofile("init.lc").

(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

  init.lc 160 bytes

(7) delete file

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

For more details about LuaLoader, please visit: http://benlo.com/esp8266/index.html#LuaLoader.

(8) format file system

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

file.format("file.lua").
file.format()
format done.