nodeMcu API Instruction
+nodeMcu API Instruction
version 0.2 2014-11-10
change log:
+2014-11-11
+add file.seek() api to file module
+now max 6 pwm channel is supported
2014-11-10
change log module to file module
now file operation support multiple read/write
@@ -1070,8 +1138,9 @@ blank.bin: 0x7e000
Returns
nil
Example
- node.restart();
-
+node.restart(); +
See also
@@ -1087,8 +1156,9 @@ blank.bin: 0x7e000Returns
nil
Example
- node.dsleep(us);
-
+node.dsleep(us); +
See also
@@ -1103,8 +1173,9 @@ blank.bin: 0x7e000Returns
number:chip ID
Example
- id = node.chipid();
-
+id = node.chipid(); +
See also
@@ -1119,8 +1190,9 @@ blank.bin: 0x7e000Returns
number: system heap size left in bytes
Example
- heap_size = node.heap();
-
+heap_size = node.heap(); +
See also
@@ -1137,8 +1209,9 @@ Default function: long: change LED blinking rate, short: reset chipReturns
nil
Example
- node.key("long", function(){print('hello world')})
-
+node.key("long", function(){print('hello world')}) +
See also
@@ -1154,9 +1227,10 @@ High: LED off time. Unit: milliseconds, time resolution: 80~100msReturns
nil
Example
- -- turn led on forever.
- node.led(0);
-
+-- turn led on forever. + node.led(0); +
See also
@@ -1172,9 +1246,10 @@ High: LED off time. Unit: milliseconds, time resolution: 80~100msReturns
nil
Example
- -- remove "foo.lua" from file system.
- file.remove("foo.lua")
-
+-- remove "foo.lua" from file system. + file.remove("foo.lua") +
See also
- file.open()
@@ -1197,11 +1272,12 @@ mode:
Returns
nil
Example
- -- open 'init.lua', print the first line.
- file.open("init.lua", "r")
- print(file.readline())
- file.close()
-
+-- open 'init.lua', print the first line. + file.open("init.lua", "r") + print(file.readline()) + file.close() +
See also
- file.close()
@@ -1217,11 +1293,12 @@ mode:
Returns
nil
Example
- -- open 'init.lua', print the first line.
- file.open("init.lua", "r")
- print(file.readline())
- file.close()
-
+-- open 'init.lua', print the first line. + file.open("init.lua", "r") + print(file.readline()) + file.close() +
See also
- file.open()
@@ -1237,11 +1314,12 @@ mode:
Returns
file content in string, line by line
Example
- -- print the first line of 'init.lua'
- file.open("init.lua", "r")
- print(file.readline())
- file.close()
-
+-- print the first line of 'init.lua' + file.open("init.lua", "r") + print(file.readline()) + file.close() +
See also
- file.open()
@@ -1258,12 +1336,13 @@ mode:
true: write ok.
nil: there is error
Example
- -- open 'init.lua' in 'a+' mode
- file.open("init.lua", "a+")
- -- write 'foo bar' to the end of the file
- file.writeline('foo bar')
- file.close()
-
+-- open 'init.lua' in 'a+' mode + file.open("init.lua", "a+") + -- write 'foo bar' to the end of the file + file.writeline('foo bar') + file.close() +
See also
- file.open()
@@ -1280,12 +1359,13 @@ nil: there is error
true: write ok.
nil: there is error
Example
- -- open 'init.lua' in 'a+' mode
- file.open("init.lua", "a+")
- -- write 'foo bar' to the end of the file
- file.write('foo bar')
- file.close()
-
+-- open 'init.lua' in 'a+' mode + file.open("init.lua", "a+") + -- write 'foo bar' to the end of the file + file.write('foo bar') + file.close() +
See also
- file.open()
@@ -1301,847 +1381,912 @@ nil: there is error
Returns
nil
Example
- -- open 'init.lua' in 'a+' mode
- file.open("init.lua", "a+")
- -- write 'foo bar' to the end of the file
- file.write('foo bar')
- file.flush()
- file.close()
-
+-- open 'init.lua' in 'a+' mode + file.open("init.lua", "a+") + -- write 'foo bar' to the end of the file + file.write('foo bar') + file.flush() + file.close() +
See also
- file.open()
- file.writeline()
file.list()
+ +file.seek()
Description
-list all files.
+Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.
Syntax
-file.list()
+file.seek(whence, offset)
Parameters
-nil
+whence:
+“set”: base is position 0 (beginning of the file);
+“cur”: base is current position;(default value)
+“end”: base is end of file;
+offset: default 0
Returns
-a lua table which contains the {file name: file size} pairs
+success: returns the final file position
+fail: returns nil
Example
- l = file.list();
- for k,v in l do
- print("name:"..k..", size:"..v)
- end
-
+-- open 'init.lua' in 'a+' mode + file.open("init.lua", "a+") + -- write 'foo bar' to the end of the file + file.write('foo bar') + file.flush() + file.seek("set") + print(file.readline()) + file.close() +
See also
+- file.open()
+- file.writeline()
file.list()
+Description
+list all files.
+Syntax
+file.list()
+Parameters
+nil
+Returns
+a lua table which contains the {file name: file size} pairs
+Example
+l = file.list(); + for k,v in l do + print("name:"..k..", size:"..v) + end +
See also
wifi module
CONSTANT
wifi.STATION, wifi.SOFTAP, wifi.STATIONAP
wifi.setmode(mode)
-Description
+Description
setup wifi operation mode.
-Syntax
+Syntax
wifi.setmode(mode)
-Parameters
+Parameters
mode: value should be: wifi.STATION, wifi.SOFTAP or wifi.STATIONAP
-Returns
+Returns
current mode after setup
-Example
- wifi.setmode(wifi.STATION)
-
+Example
+wifi.setmode(wifi.STATION) +
See also
+ +See also
wifi.getmode(mode)
-Description
+Description
get wifi operation mode.
-Syntax
+Syntax
wifi.getmode()
-Parameters
+Parameters
nil
-Returns
+Returns
wifi operation mode
-Example
- print(wifi.getmode())
-
+Example
+print(wifi.getmode()) +
See also
+ +See also
wifi.startsmart()
-Description
+Description
starts to auto configuration, if success set up ssid and pwd automatically .
-Syntax
+Syntax
wifi.startsmart(channel, function succeed_callback())
-Parameters
+Parameters
channel: 1~13, startup channel for searching, if nil, default to 6. 20 seconds for each channel.
succeed_callback: callback function called after configuration, which is called when got password and connected to AP.
Returns
-nil
-Example
- wifi.startsmart(6, cb())
-
-
-See also
- - -wifi.stopsmart()
-Description
-stop the configuring process.
-Syntax
-wifi.stopsmart()
-Parameters
-nil
Returns
nil
Example
- wifi.stopsmart()
-
+wifi.startsmart(6, cb()) +
See also
+ + +wifi.stopsmart()
+Description
+stop the configuring process.
+Syntax
+wifi.stopsmart()
+Parameters
+nil
+Returns
+nil
+Example
+wifi.stopsmart() +
See also
wifi.sta module
wifi.sta.config()
-Description
+Description
set ssid and password in station mode.
-Syntax
+Syntax
wifi.sta.config(ssid, password)
-Parameters
+Parameters
ssid: string which is less than 32 bytes.
password: string which is less than 64 bytes.
Returns
+Returns
nil
-Example
- wifi.sta.config("myssid","mypassword")
-
+Example
+wifi.sta.config("myssid","mypassword") +
See also
+ +See also
- wifi.sta.connect()
- wifi.sta.disconnect()
wifi.sta.connect()
-Description
-connect to AP in station mode.
-Syntax
-wifi.sta.connect()
-Parameters
-nil
-Returns
-nil
-Example
- wifi.sta.connect()
-
-
-See also
-- wifi.sta.disconnect()
-- wifi.sta.config()
wifi.sta.disconnect()
Description
-disconnect from AP in station mode.
+connect to AP in station mode.
Syntax
-wifi.sta.disconnect()
+wifi.sta.connect()
Parameters
nil
Returns
nil
Example
- wifi.sta.disconnect()
-
+wifi.sta.connect() +
See also
+- wifi.sta.disconnect()
+- wifi.sta.config()
wifi.sta.disconnect()
+Description
+disconnect from AP in station mode.
+Syntax
+wifi.sta.disconnect()
+Parameters
+nil
+Returns
+nil
+Example
+wifi.sta.disconnect() +
See also
- wifi.sta.config()
- wifi.sta.connect()
wifi.sta.autoconnect()
-Description
+Description
auto connect to AP in station mode.
-Syntax
+Syntax
wifi.sta.autoconnect(auto)
-Parameters
+Parameters
auto: 0 to disable auto connecting. 1 to enable auto connecting
-Returns
+Returns
nil
-Example
- wifi.sta.autoconnect()
-
+Example
+wifi.sta.autoconnect() +
See also
+ +See also
- wifi.sta.config()
- wifi.sta.connect()
- wifi.sta.disconnect()
wifi.sta.getip()
-Description
-get ip address in station mode.
-Syntax
-wifi.sta.getip()
-Parameters
-nil
-Returns
-ip address in string, for example:”192.168.0.111”
-Example
- -- print current ip
- print(wifi.sta.getip())
-
-
-See also
- - -wifi.sta.getmac()
Description
-get mac address in station mode.
+get ip address in station mode.
Syntax
-wifi.sta.getmac()
+wifi.sta.getip()
Parameters
nil
Returns
-mac address in string, for example:”18-33-44-FE-55-BB”
+ip address in string, for example:”192.168.0.111”
Example
- -- print current mac address
- print(wifi.sta.getmac())
-
+-- print current ip + print(wifi.sta.getip()) +
See also
+ + +wifi.sta.getmac()
+Description
+get mac address in station mode.
+Syntax
+wifi.sta.getmac()
+Parameters
+nil
+Returns
+mac address in string, for example:”18-33-44-FE-55-BB”
+Example
+-- print current mac address + print(wifi.sta.getmac()) +
See also
wifi.ap module
wifi.ap.config()
-Description
+Description
set ssid and password in ap mode.
-Syntax
+Syntax
wifi.ap.config(cfg)
-Parameters
+Parameters
cfg: lua table to setup ap.
-Example:
- cfg={}
- cfg.ssid="myssid"
- cfg.pwd="mypwd"
- wifi.ap.setconfig(cfg)
-
+Example:
+cfg={} + cfg.ssid="myssid" + cfg.pwd="mypwd" + wifi.ap.setconfig(cfg) +
Returns
+ +Returns
nil
-Example
- wifi.ap.config(ssid, 'password')
-
+Example
+wifi.ap.config(ssid, 'password') +
See also
+ +See also
wifi.ap.getip()
-Description
-get ip in ap mode.
-Syntax
-wifi.ap.getip()
-Parameters
-nil
-Returns
-ip address in string, for example:”192.168.0.111”
-Example
- wifi.ap.getip()
-
-
-See also
- - -wifi.ap.getmac()
Description
-get mac address in ap mode.
+get ip in ap mode.
Syntax
-wifi.ap.getmac()
+wifi.ap.getip()
Parameters
nil
Returns
-mac address in string, for example:”1A-33-44-FE-55-BB”
+ip address in string, for example:”192.168.0.111”
Example
- wifi.ap.getmac()
-
+wifi.ap.getip() +
See also
+ + +wifi.ap.getmac()
+Description
+get mac address in ap mode.
+Syntax
+wifi.ap.getmac()
+Parameters
+nil
+Returns
+mac address in string, for example:”1A-33-44-FE-55-BB”
+Example
+wifi.ap.getmac() +
See also
timer module
tmr.delay()
-Description
+Description
delay us micro seconds.
-Syntax
+Syntax
tmr.dealy(us)
-Parameters
+Parameters
us: delay time in micro second
-Returns
+Returns
nil
-Example
- -- delay 100us
- tmr.delay(100)
-
+Example
+-- delay 100us + tmr.delay(100) +
See also
+ +See also
tmr.now()
-Description
+Description
return the current value of system counter: uint32, us.
-Syntax
+Syntax
tmr.now()
-Parameters
+Parameters
nil
-Returns
+Returns
uint32: value of counter
-Example
- -- print current value of counter
- print(tmr.now())
-
+Example
+-- print current value of counter + print(tmr.now()) +
See also
+ +See also
tmr.alarm()
-Description
+Description
alarm time.
-Syntax
+Syntax
tmr.alarm(interval, repeat, function do())
-Parameters
+Parameters
Interval: alarm time, unit: millisecond
repeat: 0 - one time alarm, 1 - repeat
function do(): callback function for alarm timed out
Returns
-nil
-Example
- -- print "hello world" every 1000ms
- tmr.alarm(1000, 1, function() print("hello world") end )
-
-
-See also
- - -tmr.stop()
-Description
-stop alarm.
--Note: only one alarm is allowed, the previous one would be replaced if tmr.alarm() called again before tmr.stop().
Syntax
-tmr.stop()
-Parameters
-nil.
Returns
nil
Example
- -- print "hello world" every 1000ms
- tmr.alarm(1000, 1, function() print("hello world") end )
+ -- print "hello world" every 1000ms
+ tmr.alarm(1000, 1, function() print("hello world") end )
+
- -- something else
-
- -- stop alarm
- tmr.stop()
-
See also
+ +tmr.stop()
+Description
+stop alarm.
+-Note: only one alarm is allowed, the previous one would be replaced if tmr.alarm() called again before tmr.stop().
Syntax
+tmr.stop()
+Parameters
+nil.
+Returns
+nil
+Example
+-- print "hello world" every 1000ms + tmr.alarm(1000, 1, function() print("hello world") end ) + + -- something else + + -- stop alarm + tmr.stop() +
See also
+GPIO module
CONSTANT
gpio.OUTPUT, gpio.INPUT, gpio.INT, gpio.HIGH, gpio.LOW
gpio.mode()
-Description
+Description
initialize pin to GPIO mode, set the pin in/out mode.
-Syntax
+Syntax
gpio.mode(pin, mode)
-Parameters
+Parameters
pin: 0~11, IO index
mode: gpio.OUTPUT or gpio.INPUT, or gpio.INT(interrupt mode)
Returns
+Returns
nil
-Example
- -- set gpio 0 as output.
- gpio.mode(0, gpio.OUTPUT)
+Example
+ -- set gpio 0 as output.
+ gpio.mode(0, gpio.OUTPUT)
+
-
-See also
+See also
gpio.read()
-Description
+Description
read pin value.
-Syntax
+Syntax
gpio.read(pin)
-Parameters
+Parameters
pin: 0~11, IO index
-Returns
+Returns
number:0 - low, 1 - high
-Example
- -- read value of gpio 0.
- gpio.read(0)
-
+Example
+-- read value of gpio 0. + gpio.read(0) +
See also
+ +See also
gpio.write()
-Description
+Description
set pin value.
-Syntax
+Syntax
gpio.write(pin)
-Parameters
+Parameters
pin: 0~11, IO index
level: gpio.HIGH or gpio.LOW
Returns
+Returns
nil
-Example
- -- set pin index 1 to GPIO mode, and set the pin to high.
- pin=1
- gpio.mode(pin, gpio.OUTPUT)
- gpio.write(pin, gpio.HIGH)
-
+Example
+-- set pin index 1 to GPIO mode, and set the pin to high. + pin=1 + gpio.mode(pin, gpio.OUTPUT) + gpio.write(pin, gpio.HIGH) +
See also
+ +See also
gpio.trig()
-Description
+Description
set the interrupt callback function for pin.
-Syntax
+Syntax
gpio.trig(pin, type, function(level))
-Parameters
+Parameters
pin: 0~11, IO index
type: “up”, “down”, “both”, “low”, “high”, which represent rising edge, falling edge, both edge, low level, high level trig mode separately.
function(level): callback function when triggered. The gpio level is the param. Use previous callback function if undefined here.
Returns
+Returns
nil
-Example
- -- use pin 0 as the input pulse width counter
- pulse0 = 0
- du = 0
- gpio.mode(0,gpio.INT)
- function pin0cb(level)
- du = tmr.now() – pulse0
- print(du)
- pulse0 = tmr.now()
- if level == 1 then gpio.trig(0, "down ") else gpio.trig(0, "up ") end
- end
- gpio.trig(0, "down ",pin0cb)
+Example
+ -- use pin 0 as the input pulse width counter
+ pulse0 = 0
+ du = 0
+ gpio.mode(0,gpio.INT)
+ function pin0cb(level)
+ du = tmr.now() – pulse0
+ print(du)
+ pulse0 = tmr.now()
+ if level == 1 then gpio.trig(0, "down ") else gpio.trig(0, "up ") end
+ end
+ gpio.trig(0, "down ",pin0cb)
+
-
-See also
+See also
PWM module
pwm.setup()
-Description
+Description
set pin to PWM mode. Only 3 pins can be set to PWM mode at the most.
-Syntax
+Syntax
pwm.setup(pin, clock, duty)
-Parameters
+Parameters
pin: 0~11, IO index
clock: 1~500, pwm frequency
duty: 0~100, pwm duty cycle in percentage
Returns
-nil
-Example
- -- set pin index 0 as pwm output, frequency is 100Hz, duty cycle is 50-50.
- pwm.setup(0, 100, 50)
-
-
-See also
- - -pwm.close()
-Description
-quit PWM mode for specified pin.
-Syntax
-pwm.close(pin)
-Parameters
-pin: 0~11, IO index
Returns
nil
Example
- pwm.close(0)
-
+-- set pin index 0 as pwm output, frequency is 100Hz, duty cycle is 50-50. + pwm.setup(0, 100, 50) +
See also
- -pwm.start()
+ +pwm.close()
Description
-pwm starts, you can detect the waveform on the gpio.
+quit PWM mode for specified pin.
Syntax
-pwm.start(pin)
+pwm.close(pin)
Parameters
pin: 0~11, IO index
Returns
nil
Example
- pwm.start(0)
-
+pwm.close(0) +
See also
- - -pwm.stop()
+ + +pwm.start()
Description
-pause the output of PWM waveform.
+pwm starts, you can detect the waveform on the gpio.
Syntax
-pwm.stop(pin)
+pwm.start(pin)
Parameters
pin: 0~11, IO index
Returns
nil
Example
- pwm.stop(0)
-
+pwm.start(0) +
See also
- - -pwm.setclock()
+ + +pwm.stop()
Description
-set pwm frequency for pin.
--Note: setup pwm frequency will synchronously change others if there are any. Only one PWM frequency can be allowed for the system.
pause the output of PWM waveform.
Syntax
-pwm.setclock(pin, clock)
+pwm.stop(pin)
Parameters
-pin: 0~11, IO index.
-clock: 1~500, pwm frequency.
pin: 0~11, IO index
Returns
nil
Example
- pwm.setclock(0, 100)
-
+pwm.stop(0) +
See also
+ + +pwm.setclock()
+Description
+set pwm frequency for pin.
+-Note: setup pwm frequency will synchronously change others if there are any. Only one PWM frequency can be allowed for the system.
Syntax
+pwm.setclock(pin, clock)
+Parameters
+pin: 0~11, IO index.
+clock: 1~500, pwm frequency.
Returns
+nil
+Example
+pwm.setclock(0, 100) +
See also
pwm.getclock()
-Description
+Description
get pwm frequency of pin.
-Syntax
+Syntax
pwm.getclock(pin)
-Parameters
+Parameters
pin: 0~11, IO index.
-Returns
+Returns
number:pwm frequency of pin
-Example
- print(pwm.getclock(0))
-
+Example
+print(pwm.getclock(0)) +
See also
+ +See also
pwm.setduty()
-Description
+Description
set duty clycle for pin.
-Syntax
+Syntax
pwm.setduty(pin, duty)
-Parameters
+Parameters
pin: 0~11, IO index
duty: 0~100, pwm duty cycle in percentage
Returns
-nil
-Example
- pwm.setduty(0, 50)
-
-
-See also
- - -pwm.getduty()
-Description
-get duty clycle for pin.
-Syntax
-pwm.getduty(pin)
-Parameters
-pin: 0~11, IO index
Returns
nil
Example
- -- D0 is connected to green led
- -- D1 is connected to blue led
- -- D2 is connected to red led
- pwm.setup(0,500,50)
- pwm.setup(1,500,50)
- pwm.setup(2,500,50)
- pwm.start(0)
- pwm.start(1)
- pwm.start(2)
- function led(r,g,b)
- pwm.setduty(0,g)
- pwm.setduty(1,b)
- pwm.setduty(2,r)
- end
- led(50,0,0) -- set led to red
- led(0,0,50) -- set led to blue.
+ pwm.setduty(0, 50)
+
-
See also
+ + +pwm.getduty()
+Description
+get duty clycle for pin.
+Syntax
+pwm.getduty(pin)
+Parameters
+pin: 0~11, IO index
+Returns
+nil
+Example
+-- D0 is connected to green led + -- D1 is connected to blue led + -- D2 is connected to red led + pwm.setup(0,500,50) + pwm.setup(1,500,50) + pwm.setup(2,500,50) + pwm.start(0) + pwm.start(1) + pwm.start(2) + function led(r,g,b) + pwm.setduty(0,g) + pwm.setduty(1,b) + pwm.setduty(2,r) + end + led(50,0,0) -- set led to red + led(0,0,50) -- set led to blue. +
See also
net module
CONSTANT
net.TCP, net.UDP
net.createServer()
-Description
-create a server.
-Syntax
-net.createServer(type, secure)
-Parameters
-type: net.TCP or net.UDP
-secure: true or false, true for safe link, false for ordinary link
Returns
-net.server sub module
-Example
- net.createServer(net.TCP, true)
-
-
-See also
- - -net.createConnection()
Description
-create a client.
+create a server.
Syntax
-net.createConnection(type, secure)
+net.createServer(type, secure)
Parameters
type: net.TCP or net.UDP
secure: true or false, true for safe link, false for ordinary link
Returns
net.server sub module
Example
- net.createConnection(net.UDP, false)
-
+net.createServer(net.TCP, true) +
See also
+ + +net.createConnection()
+Description
+create a client.
+Syntax
+net.createConnection(type, secure)
+Parameters
+type: net.TCP or net.UDP
+secure: true or false, true for safe link, false for ordinary link
Returns
+net.server sub module
+Example
+net.createConnection(net.UDP, false) +
See also
net.server module
listen()
-Description
+Description
listen on port from [ip] address.
-Syntax
+Syntax
net.server.listen(port,[ip],function(net.socket))
-Parameters
+Parameters
port: port number
ip:ip address string, can be omitted
function(net.socket): callback function, pass to Caller function as param if a connection is created successfully
Returns
-nil
-Example
- -- create a server
- sv=net.createServer(net.TCP, false)
- -- server listen on 80, if data received, print data to console, and send "hello world" to remote.
- sv:listen(80,function(c)
- c:on("receive", function(sck, pl) print(pl) end)
- c:send("hello world")
- end)
-
-
-See also
- - -close()
-Description
-close server.
-Syntax
-net.server.close()
-Parameters
-nil
Returns
nil
Example
- -- create a server
- sv=net.createServer(net.TCP, false)
- -- close server
- sv:close()
-
+-- create a server + sv=net.createServer(net.TCP, false) + -- server listen on 80, if data received, print data to console, and send "hello world" to remote. + sv:listen(80,function(c) + c:on("receive", function(sck, pl) print(pl) end) + c:send("hello world") + end) +
See also
+ +close()
+Description
+close server.
+Syntax
+net.server.close()
+Parameters
+nil
+Returns
+nil
+Example
+-- create a server + sv=net.createServer(net.TCP, false) + -- close server + sv:close() +
See also
+net.socket module
connect()
-Description
+Description
connect to remote.
-Syntax
+Syntax
connect(port, ip)
-Parameters
+Parameters
port: port number
ip: ip address in string
Returns
-nil
-See also
- - -send()
-Description
-send data to remote via connection.
-Syntax
-send(string, function(sent))
-Parameters
-string: data in string which will be sent to remote
-function(sent): callback function for sending string
Returns
nil
See also
+ +send()
+Description
+send data to remote via connection.
+Syntax
+send(string, function(sent))
+Parameters
+string: data in string which will be sent to remote
+function(sent): callback function for sending string
Returns
+nil
+See also
+on()
-Description
+Description
register callback function for event.
-Syntax
+Syntax
on(event, function cb())
-Parameters
+Parameters
event: string, which can be: “connection”, “reconnection”, “disconnection”, “receive”, “sent”
function cb(net.socket, [string]): callback function. The first param is the socket.
If event is”receive”, the second param is received data in string.
Returns
+Returns
nil
-Example
- sk=net.createConnection(net.TCP, false)
- sk:on("receive", function(sck, c) print(c) end )
- sk:connect(80,"192.168.0.66")
- sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
-
+Example
+sk=net.createConnection(net.TCP, false) + sk:on("receive", function(sck, c) print(c) end ) + sk:connect(80,"192.168.0.66") + sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") +
See also
+ +See also
close()
-Description
-close socket.
-Syntax
-close()
-Parameters
-nil
-Returns
-nil
-See also
- - -dns()
Description
-get domain ip
+close socket.
Syntax
-dns(domain, function(net.socket, ip))
+close()
Parameters
-domain: domain name.
-function (net.socket, ip): callback function. The first param is the socket, the second param is the ip address in string.
nil
Returns
nil
See also
+ +dns()
+Description
+get domain ip
+Syntax
+dns(domain, function(net.socket, ip))
+Parameters
+domain: domain name.
+function (net.socket, ip): callback function. The first param is the socket, the second param is the ip address in string.
Returns
+nil
+See also
+i2c module
CONSTANT
i2c.SLOW, i2c.TRANSMITTER, i2c. RECEIVER. FAST(400k)is not supported for now.
i2c.setup()
-Description
+Description
initialize i2c.
-Syntax
+Syntax
i2c.setup(id, pinSDA, pinSCL, speed)
-Parameters
+Parameters
id = 0
pinSDA: 0~11, IO index
pinSCL: 0~11, IO index
speed: i2c.SLOW
Returns
-nil
-See also
- - -i2c.start()
-Description
-start i2c transporting.
-Syntax
-i2c.start(id)
-Parameters
-id = 0
Returns
nil
See also
- -i2c.stop()
+ +i2c.start()
Description
-stop i2c transporting.
+start i2c transporting.
Syntax
-i2c.stop(id)
+i2c.start(id)
Parameters
id = 0
Returns
nil
See also
- -i2c.address()
+ +i2c.stop()
Description
-setup i2c address and read/write mode.
+stop i2c transporting.
Syntax
-i2c.address(id, device_addr, direction)
+i2c.stop(id)
Parameters
-id=0
-device_addr: device address.
-direction: i2c.TRANSMITTER for writing mode , i2c. RECEIVER for reading mode
id = 0
Returns
nil
See also
- -i2c.write()
+ +i2c.address()
Description
-write data to i2c, data can be multi numbers, string or lua table.
+setup i2c address and read/write mode.
Syntax
-i2c.write(id, data1, data2,…)
+i2c.address(id, device_addr, direction)
Parameters
id=0
-data: data can be numbers, string or lua table.
+direction: i2c.TRANSMITTER for writing mode , i2c. RECEIVER for reading mode
Returns
nil
-Example
- i2c.write(0, "hello", "world")
-
-
See also
+ +i2c.write()
+Description
+write data to i2c, data can be multi numbers, string or lua table.
+Syntax
+i2c.write(id, data1, data2,…)
+Parameters
+id=0
+data: data can be numbers, string or lua table.
Returns
+nil
+Example
+i2c.write(0, "hello", "world") +
See also
+i2c.read()
-Description
+Description
read data for len bytes.
-Syntax
+Syntax
i2c.read(id, len)
-Parameters
+Parameters
id=0
len: data length
Returns
+Returns
string:data received.
-Example
- id=0
- sda=1
- scl=0
+Example
+ id=0
+ sda=1
+ scl=0
- -- initialize i2c, set pin1 as sda, set pin0 as scl
- i2c.setup(id,sda,scl,i2c.SLOW)
+ -- initialize i2c, set pin1 as sda, set pin0 as scl
+ i2c.setup(id,sda,scl,i2c.SLOW)
- -- user defined function: read from reg_addr content of dev_addr
- function read_reg(dev_addr, reg_addr)
- i2c.start(id)
- i2c.address(id, dev_addr ,i2c.TRANSMITTER)
- i2c.write(id,reg_addr)
- i2c.stop(id)
- i2c.start(id)
- i2c.address(id, dev_addr,i2c.RECEIVER)
- c=i2c.read(id,1)
- i2c.stop(id)
- return c
- end
+ -- user defined function: read from reg_addr content of dev_addr
+ function read_reg(dev_addr, reg_addr)
+ i2c.start(id)
+ i2c.address(id, dev_addr ,i2c.TRANSMITTER)
+ i2c.write(id,reg_addr)
+ i2c.stop(id)
+ i2c.start(id)
+ i2c.address(id, dev_addr,i2c.RECEIVER)
+ c=i2c.read(id,1)
+ i2c.stop(id)
+ return c
+ end
- -- get content of register 0xAA of device 0x77
- reg = read_reg(0x77, 0xAA)
- pirnt(string.byte(reg))
+ -- get content of register 0xAA of device 0x77
+ reg = read_reg(0x77, 0xAA)
+ pirnt(string.byte(reg))
+
-
-