Lua Function Reference

Last update: October 2016
Function Name Description Firmware Versions

request

Issues an HTTP Request.

3.00.00+

HTTPGetFile

Downloads a file by HTTP.

3.00.00+

pio

Controls SD interface as a PIO.

3.00.00+

ftp

Downloads or upload a file by FTP.

3.00.00+

deleted md5

Generates MD5.

3.00.00

hash

Generates hash.

3.00.00+

Scan

Scans SSID.

3.00.00+

GetScanInfo

Gets results of SSID scan.

3.00.00+

Connect

Enables Wireless LAN in STA mode.

3.00.00+

Establish

Enables Wireless LAN in AP mode.

3.00.00+

Bridge

Enables Wireless LAN in Internet passthrough mode.

3.00.00+

Disconnect

Disables Wireless LAN.

3.00.00+

sleep

Stops execution of the script during the specified timespan.

3.00.00+

sharedmemory

Gets data from shared memory or writes data to shared memory.

3.00.00+

SetCert

Sets root certification by file.

3.00.00+

strconvert

Converts the string.

3.00.00+

SetChannel

Sets the wireless channels.

3.00.01+

MailSend

Sends email by FlashAir.

3.00.01+

spi

Sets the SPI by FlashAir.

3.00.01+

ReadStatusReg

Gets the status register contents of the FlashAir.

3.00.00+

new ip

Gets or sets IP Address.

3.00.00+

new WlanLink

Confirms the connection.

3.00.00+

new remove

Removes a file.

3.00.00+

new rename

Renames a file.

3.00.00+

request

Issues an HTTP (Hyper Text Transfer Protocol) request.

Syntax

b, c, h = fa.request(url [, method [, headers [, file [, body [, bufsize [, redirect]]]]]])

Parameters

url

string. A URL.

method

string. HTTP request method. "GET" is used if a method not specified.

headers

table. A table of fields for the HTTP request header. No fields are added if none specified.

file

string. The path of a file to be sent in the HTTP request.

body

string. A message string to be sent in the HTTP request. If both file and body parameter are specified, this command sends the message after it places <!--WLANSDFILE--> into the contents of the file. If body parameter is not specified, it simply sends the file.

bufsize

number. Send buffer size for the HTTP request. See the Notes section.

redirect

boolean。 If true, enables HTTP redirection. Defaults to true.

Return values

b

string. HTTP response body.

c

number. HTTP status code.

h

string. HTTP response header.

Example

boundary = "--61141483716826"
contenttype = "multipart/form-data; boundary=" .. boundary
filepath = "sample.txt"
mes = "--".. boundary .. "¥r¥n"
 .."Content-Disposition: form-data; name=¥"file¥"; filename=¥""..file .."¥"¥r¥n"
 .."Content-Type: text/plain¥r¥n"
 .."¥r¥n"
 .."<!--WLANSDFILE-->¥r¥n"
 .."--" .. boundary .. "--¥r¥n"

blen = lfs.attributes(file,"size") + string.len(mes) - 17
b,c,h = fa.request{url = "http://192.168.0.1/upload.cgi",
 method = "POST",
 headers = {["Content-Length"] = tostring(blen),
 ["Content-Type"] = contenttype},
 file = filepath,
 body = mes
 }

Notes

  • The maximum payload size of an HTTP request is 3KiB.

  • A request may be canceled by TCP reset - especially when uploading a file to Flickr. Try to reduce the sending buffer size by setting bufsize to 1460 * 10 to avoid this issue.

HTTPGetFile

Downloads a file by HTTP.

Syntax

result = fa.HTTPGetFile(uri, filepath [, user, pass])

Parameters

uri

string. A URL.

filepath

string. A path to a file that will store the response.

user

string. A username parameter for Basic Authentication.

pass

string. A password parameter for Basic Authentication.

Return values

result

number.

1

Success.

nil

Error.

pio

Controls the SD interface with PIO (Programmed Input/Output).

Syntax

s, indata = fa.pio(ctrl, data)

Parameters

ctrl

number. Input or output. Set 0 for the bits you want to read from, and Set 1 for the bits you want to write to.

data

number. Sets the output. Set 0 to output low, or set 1 to output high.

Return values

s

number.

1

Success.

0

Error.

indata

number. The input.

Notes

Bit assignment.

  • CMD : 0x01

  • D0 : 0x02

  • D1 : 0x04

  • D2 : 0x08

  • D3 : 0x10

ftp

Download or upload a file via FTP (File Transfer Protocol).

Syntax

result = fa.ftp(cmd, uri, filename)

Parameters

cmd

string. FTP command.

"get"

Download.

"put"

Upload.

url

string. A URI of an FTP server.

filename

string. A path to a file on a FlashAir.

Return values

result

number.

1

Success.

nil

Error.

Example

fa.ftp("get", "ftp://user:pass@ftp.test.com:21/test", "test");

md5

This is obsolete. Use hash.

hash

Generates a hash value.

Syntax

hash = fa.hash(name, data, key)

Parameters

name

string. select hash's type (md5,sha1,sha256,hmac-sha256).

data

string. data.

key

string. secret key(used if name was hmac-sha256).

Return values

hash value

string. hash value, as a hexadecimal number.

Scan

Scans SSID. Call GetScanInfo() to retrieve scan results.

Syntax

count = fa.Scan([ssid])

Parameters

ssid

string. Scan the specified SSID. Or, find nearby SSIDs if none specified.

Return values

count

number. The number of APs found in the scan.

Notes

This command is unavailable if the wireless LAN is enabled (by Connect, Establish, or Bridge).

GetScanInfo

Gets results of SSID scan.

Syntax

ssid, other = fa.GetScanInfo(num)

Parameters

num

number. An index of the item in the scan result.

Return values

ssid

string. SSID。

other

table. AP information.

Notes

This command is unavailable if the wireless LAN is enabled (by Connect, Establish, or Bridge).

Connect

Enables Wireless LAN in STA mode.

Syntax

fa.Connect(ssid, networkkey)

Parameters

ssid

string. SSID of AP that the FlashAir will connect to.

networkkey

string. Security key of AP that the FlashAir will connect to.

Return values

None.

Establish

Enables Wireless LAN in AP mode.

Syntax

fa.Establish(ssid, networkkey, encmode)

Parameters

ssid

string. SSID of AP that a FlashAir establishes.

networkkey

string. Security key of AP that a FlashAir establishes.

encmode

number. Encoding mode of AP that a FlashAir establishes. See Notes section.

Return values

None.

Notes

encmode must be one of the following values:

Value Description

0

Open system, No encryption.

1

Open system, WEP

3

WPA-PSK, TKIP

4

WPA-PSK, AES

5

WPA2-PSK, TKIP

6

WPA2-PSK, AES

Bridge

Enables Wireless LAN in Internet passthrough mode.

Syntax

fa.Bridge(ssid, networkkey, encmode, brgssid, brgnetworkkey)

Parameters

ssid

string. SSID of AP that a FlashAir establishes.

networkkey

string. Security key of AP that a FlashAir establishes.

encmode

string. Encoding mode of AP that a FlashAir establishes. See Notes section of Establish command.

brgssid

string. SSID of AP that the FlashAir will connects to.

brgnetworkkey

string. Security key of AP that the FlashAir will connects to.

Return values

None.

Disconnect

Disables Wireless LAN.

Syntax

fa.Disconnect()

Parameters

None.

Return values

None.

sleep

Stops execution of the script during the specified timespan.

Syntax

sleep(msec)

Parameters

msec

number. Duration to sleep between 0~(232 - 1) in millisecond.

Return values

None.

sharedmemory

Gets data from shared memory or writes data to shared memory.

Syntax

fa.sharedmemory(command, addr, len, wdata)

Parameters

command

string. execution command(write, read).

addr

number. address(0~511).

len

number. data length(0~512).

wdata

string. the string of wrriten data(Enabled by write command).

Response

write ⇒  failure:nil successful:1 
read  ⇒  failure:nil successful: the string of read data

Example

res1 = fa.sharedmemory("write", 0, 8, "12345678")
res2 = fa.sharedmemory("read", 1, 4, 0)
print("res=",res2)
  
 Result:res=2345

SetCert

Sets or clears root certification by file(X.509 binary encode with DER).

Syntax

fa.SetCert(filename)

Parameters

filename

string. Sets the DER file's path and the file name,Clears the root certification if omitted.

Return values

result

number.

1

successful.

nil

failure.

Example

lres = fa.SetCert("DerSha256.cer")
	if (res == nil) then
		print("cert err")
	end

strconvert

Converts the SJIS characters to UTF-8 , and the utf8 characters to SJIS.

Syntax

fa.strconvert(format, orgstr)

Parameters

format

string. Converts the SJIS characters to UTF-8 if specify 「sjis2utf8」, and converts the UTF-8 characters to SJIS if specify 「utf82sjis」.

orgstr

string. The original data before convert.

Response

successful: converted strings.
failure:nil

Example

str = "あかさたなはまやらわ"
print("testStr=", str)
a = fa.strconvert("sjis2utf8", str )
print("toUTF8=", a)
b = fa.strconvert("utf82sjis", a)
print("toSJIS=", b)

SetChannel

Sets the wireless channels.

Syntax

fa.SetChannel(channelNo)

Parameters

channelNo

number. channel(0~11 or 0x0~0xb, specify 0 is AUTO).

Return values

None

Example

fa.Disconnect()
fa.SetChannel("0xB")
fa.Establish("flashair3", "12345678", "6")

MailSend

Sends email by FlashAir(enables to attach a file).

Syntax

fa.MailSend(from,headers,body,server,user,password, attachment, ContentType)

Parameters

from

string. Sender e-mail address.

headers

string. e-mail header.

body

string. message body.

server

string. mail server(smtp).

user

string. user's ID.

password

string. password.

attachment

string. selects the file attachment(OPTION).

ContentType

string. selects a type of the contents(REQUIRED only when attachment is valid).

Response

successful: MailSend is success.
failure:nil(failed to send).

Example

from = "fromaddr@yahoo.co.jp"
rcpt = "toaddr@gmail.com"
a = fa.MailSend {
  from = from,
  headers = "To: "..rcpt.."\r\nFrom: "..from.."\r\nSubject: test",
  body = "HELLO HELLO",
  server = "smtp.mail.yahoo.co.jp",
  user = "flashair",
  password ="1234567890",
  attachment = "FBYF.jpg",
  ContentType = "image/jpg"
}

spi

Sets the SPI by FlashAir.

Syntax

fa.spi(command,data)

command

init

changes the clock period(default:1000).

mode

select the mode of the SPI(value:0~3. default:3).

bit

the number of assigned bits for transmit(default:8).

write

write command.

read

read command.

cs

controls the signals of Chip Select(CS=ON if 1, CS=OFF if 0).

part of Response

write : the received data when write command was issued.
read : the read data when read command was issued.
otherwise: any status.

Example

fa.spi("init", 1001)

ReadStatusReg

Gets the status register contents of the FlashAir.

Syntax

reg = fa.ReadStatusReg()

Parameters

None

Return values

Returns the wireless LAN application status register contents as a string. Refer to Wireless LAN Application Status Register for details.

Example

Get IP address of the card.

local ipaddress = string.sub(fa.ReadStatusReg(),160,168)

ip

Gets or sets IP Address of FlashAir.

Syntax

ip, mask, gw = fa.ip(ipaddress, subnetmask, gateway)

Parameters

ipaddress

string. IP Address of FlashAir.

subnetmask

string. subnet mask.

gateway

string. IP Address of gateway.

Return values

ip

string. IP Address.

mask

string. subnet mask.

gw

string. gateway.

example

-- When getting values
ip, mask, gw = fa.ip()
-- When setting values
fa.ip("192.168.11.2", "255.255.255.0", "192.168.11.1")

Confirms the FlashAir is wirelessly connected to the network.

Syntax

result = fa.WlanLink()

Parameters

None

Return values

result

number.

1

connected.

0

disconnected.

remove

Removes a file.

Syntax

fa.remove(filepath)

Parameters

filepath

string. The file to be removed.

Return values

None

example

fa.remove("/DCIM/100__TSB/DSC_100.JPG")

rename

Moves or renames a file or a directory.

Syntax

fa.rename(oldfile, newfile)

Parameters

oldfile

string. old file path.

newfile

string. new file path.

Return values

None

example

fa.rename("/DCIM/100__TSB/DSC_100.JPG","/DCIM/100__TSB/DSC_101.JPG")