Development of NodeMCU

Subsection 4.1: init.lua

init.lua can be used as the applicaiton entrance for NodeMCU. If no, can ignore; if has, then run it. Therefore, some codes can be put here to run automatically after power.

1     print("\n")
2     print("ESP8266 Started")
3     
4     local luaFile = {"fileName.lua"}
5     for i, f in ipairs(luaFile) do
6         if file.open(f) then
7           file.close()
8           print("Compile File:"..f)
9           node.compile(f)
10       print("Remove File:"..f)
11           file.remove(f)
12         end
13      end
14     
15     luaFile = nil
16     collectgarbage()
17     
18     dofile("fileName.lc");

line 1-2: print the character information; line 4: define the lua file, where "fileName" must be revised by different file. If many files need to be compiled, can be added in the later, such as, local luaFile = {"fileName1.lua","fileName2.lua"};

line 5: for cycle is used to complete the file operations for many files;

line 6: judge whether the file exists, if exists, then run, or ignore it;

line 7: close the opened files;

line 8-11: compete the complie, and generate "filename.lc" automatically;

line 15-16: release memory;

line 17: run the completed binary file.

Tips: In lua, the variable type is default set as global. If the variabe is only used in this file, a key word "local" must be added. After that, the value is set as nil, and use collectgarbage() to show the release memory.

When downloading and restart NodeMCU, if a warm on memory not enough happens, shown in the following.

  lua: init.lua:8: not enough memory

This is because memory consumption is big when Lua just starts, and it would normal after start is finished. Therefore, compile should be done after a while for the start.