Skip to content

Programming with Lua

yichone edited this page Dec 1, 2016 · 1 revision

How to use node module

Restart your device

node.restart()

How to use file module

Create a file on file system (SPIFFS)

file.open("yourFile.lua","w+")  -- if the file is not exist, create one
file.write("Input your content here")
--file.writeline("Your content here")  -- write a line
file.close()

Read an existing file from file system

file.open("yourFile.lua","r")
file.read()  -- Use file.read(n) to read the first n characters of the file
--file.readline()  -- read a line
file.close()

Append something new to an existing file

file.open("yourFile.lua","a")
file.write("Your content here")
file.close()

Remove file from file system

file.remove("yourFile.lua")

How to encode/decode with cjson module

encodeString = cjson.encode({key1=value1, key2=value2})
print(encodeString)  -- {'key1':'value1', 'key2':'value2'}
decodeResult = cjson.decode(encodeString)

How to use base64

The utils module contains some useful tools, such as base64, date tool, etc. More useful tools will be added to this module, then user can invoke them from this module.

str = "hello world";
encrypt = utils.base64_encode(str);
decrypt = utils.base64_decode(encrypt);

How to use template module

Test

The template module is a sample to show users how to create their own module. Invoke the start and stop method to test.

template.start()
template.stop()
Clone this wiki locally