Managing Files with NodeMCU | NodeMCU Examples
In this article, We learn “Managing Files with NodeMCU”. NodeMCU is most popular development board for IoT projects. NodeMCU is an open source IoT platform. It includes firmware which runs on the ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. The term “NodeMCU” by default refers to the firmware rather than the development kits. The firmware uses the Lua scripting language. It is based on the eLua project, and built on the Espressif Non-OS SDK for ESP8266. It uses many open source projects, such as lua-cjson and SPIFFS (SPI Flash File System).
NodeMCU 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.
Managing Files with NodeMCU
Let’s explore some of the file operations here.
List Files:
You can list all the available files, 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. 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:
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:
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:
dofile("helloLua.lua")
Delete File:
file.remove("helloIndia.txt") file.remove("helloLua.lua")
Format File System:
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.
NodeMCU Flasher
NodeMCU flasher is a firmware programmer for NodeMCU DEVKIT V0.9. You can use it to program NodeMCU DEVKIT or your own ESP8266 board. You MUST set GPIO0 to LOW before programming, and NodeMCU DEVKIT V0.9 will do it automatically. It will be cross platform and open source.