IoT Bytes

Bits and Bytes of IoT

Managing Files with NodeMCU

Pradeep Singh | 2nd April 2016

file

ModeMCU Firmware has inbuilt support for file operations using SPI Flash File System (SPIFFS), so you can perform operations like upload, delete, list, compile, run, etc directly on ESP8266 Module. SPIFFS allows data and scripts to be written and read from the flash memory in form of files instead of raw memory locations.

Lua source program can be saved, compiled and run directly on NodeMCU Module. Within NodeMCU, the file module is used by Lua scripts to interact with the SPIFFS filesystem. It means that we perform the file operations using simple Lua based NodeMCU functions/commands.

Let’s explore some of the file operations here.

List Files:

You can list all the available files on NodeMCU using following script –

for k,v in pairs(file.list()) do 
 print("File Name: " .. k .. " Size: " .. v)
end

Create File:

To create a new text file you can use following script. On NodeMCU, maximum filename length is 32 characters.

file.open("hello_world.txt", "w")
file.writeline("My First File on NodeMCU") 
file.close()

To create a Lua Script file you need to enclose the line in “[[ ]]”. Following is an example –

file.open("helloLua.lua", "w")
file.writeline([[a=1]])
file.writeline([[a=a+1]])
file.writeline([[print(a)]])
file.close()

NodeMCU allows a file to be opened in following mode –

"r": read mode (the default)
"w": write mode
"a": append mode
"r+": update mode, all previous data is preserved
"w+": update mode, all previous data is erased
"a+": append update mode, previous data is preserved, writing is only allowed at the end of file

Read File:

To read a file you can use following script –

file.open("hello_world.txt", "r")
while true 
do 
 line = file.readline() 
 if (line == nil) then 
 file.close()
 break 
 end
 print(line)
end

Rename File:

To rename a file on NodeMCU you can use following method –

file.rename("hello_world.txt", "helloIndia.txt")

Compile Lua Script File:

You can compile a Lua file on NodeMCU board using following method. After compilation a new file will be created with same name and “.lc” extension.

node.compile("helloLua.lua")

Execute Lua Script File:

You can execute a Lua script using following method –

dofile("helloLua.lua")

Delete File:

To delete a file from NodeMCU you can use following method –

file.remove("helloIndia.txt")
file.remove("helloLua.lua")

Format File System:

To format the file system you can use following method. Generally it takes from half to one minute time. Keep waiting till the time you see “format done.” message on the serial console.

file.format()

init.lua File:

“init.lua” acts as C main() function for NodeMCU. It is the first file that NodeMCU looks for after completing the boot process. If it finds this file, it tries to execute the contents otherwise ignores it.

In most of the cases the “init.lua” file will not be there by default. This is the reason why you see following message on serial console when you connect to NodeMCU for the first time –

lua: cannot open init.lua

Important thing to notice while creating init.lua file is that if there are errors in init.lua code, NodeMCU may fall into infinite reboot loop. So it becomes really important to test your code before adding that to init.lua file.

In case if you are unable to recover NodeMCU from reboot loop, you may try re-flashing the firmware using NodeMCU Flasher.

Advertisements
%d bloggers like this: