nodeMcu API Instruction
+中文版本
version 0.9.2 build 2014-11-18
change log:
@@ -1096,8 +1162,9 @@ key is triged only when key is released
Returns
nil
Example
- node.restart();
-
+
+
See also
-
@@ -1113,8 +1180,9 @@ key is triged only when key is released
Returns
nil
Example
- node.dsleep(us);
-
+
+
See also
-
@@ -1129,8 +1197,9 @@ key is triged only when key is released
Returns
number:chip ID
Example
- id = node.chipid();
-
+
+
See also
-
@@ -1145,8 +1214,9 @@ key is triged only when key is released
Returns
number: system heap size left in bytes
Example
- heap_size = node.heap();
-
+ heap_size = node.heap();
+
+
See also
-
@@ -1163,8 +1233,9 @@ Default function: long: change LED blinking rate, short: reset chip
Returns
nil
Example
- node.key("long", function(){print('hello world')})
-
+ node.key("long", function(){print('hello world')})
+
+
See also
-
@@ -1180,9 +1251,10 @@ High: LED off time. Unit: milliseconds, time resolution: 80~100ms
Returns
nil
Example
- -- turn led on forever.
- node.led(0);
-
+ -- turn led on forever.
+ node.led(0);
+
+
See also
-
@@ -1198,9 +1270,10 @@ same as pcall(loadstring(str)) but support multi seperated line.
Returns
nil
Example
- -- never use node.input() in console. no effect.
- sk:on("receive", function(conn, payload) node.input(payload) end)
-
+ -- never use node.input() in console. no effect.
+ sk:on("receive", function(conn, payload) node.input(payload) end)
+
+
See also
-
@@ -1216,33 +1289,35 @@ serial_debug: 1 output also show in serial. 0: no serial output.
Returns
nil
Example
- function tonet(str)
- sk:send(str)
- -- print(str) WRONG!!! never ever print something in this function
- -- because this will cause a recursive function call!!!
- end
- node.ouput(tonet, 1) -- serial also get the lua output.
-
+ function tonet(str)
+ sk:send(str)
+ -- print(str) WRONG!!! never ever print something in this function
+ -- because this will cause a recursive function call!!!
+ end
+ node.ouput(tonet, 1) -- serial also get the lua output.
+
+
+
+ -- a simple telnet server
+ s=net.createServer(net.TCP)
+ s:listen(2323,function(c)
+ con_std = c
+ function s_output(str)
+ if(con_std~=nil)
+ then con_std:send(str)
+ end
+ end
+ node.output(s_output, 0) -- re-direct output to function s_ouput.
+ c:on("receive",function(c,l)
+ node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
+ end)
+ c:on("disconnection",function(c)
+ con_std = nil
+ node.output(nil) -- un-regist the redirect output function, output goes to serial
+ end)
+ end)
+
- -- a simple telnet server
- s=net.createServer(net.TCP)
- s:listen(23,function(c)
- con_std = c
- function s_output(str)
- if(con_std~=nil)
- then con_std:send(str)
- end
- end
- node.output(s_output, 0) -- re-direct output to function s_ouput.
- c:on("receive",function(c,l)
- node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
- end)
- c:on("disconnection",function(c)
- con_std = nil
- node.output(nil) -- un-regist the redirect output function, output goes to serial
- end)
- end)
-
See also
-
@@ -1258,9 +1333,10 @@ serial_debug: 1 output also show in serial. 0: no serial output.
Returns
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()
@@ -1283,11 +1359,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()
@@ -1303,11 +1380,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()
@@ -1324,11 +1402,12 @@ mode:
file content in string, line by line, include EOL(‘\n’)
return nil when EOF.
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()
@@ -1345,12 +1424,13 @@ return nil when EOF.
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()
@@ -1367,12 +1447,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()
@@ -1388,13 +1469,14 @@ 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()
@@ -1415,15 +1497,16 @@ offset: default 0
success: returns the final file position
fail: 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.seek("set")
- print(file.readline())
- 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.seek("set")
+ print(file.readline())
+ file.close()
+
+
See also
- file.open()
@@ -1439,11 +1522,12 @@ fail: returns 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
-
+ l = file.list();
+ for k,v in l do
+ print("name:"..k..", size:"..v)
+ end
+
+
See also
- file.remove()
@@ -1461,8 +1545,9 @@ fail: returns nil
Returns
current mode after setup
Example
- wifi.setmode(wifi.STATION)
-
+ wifi.setmode(wifi.STATION)
+
+
See also
- wifi.getmode()
@@ -1477,8 +1562,9 @@ fail: returns nil
Returns
wifi operation mode
Example
- print(wifi.getmode())
-
+
+
See also
- wifi.setmode()
@@ -1494,8 +1580,9 @@ succeed_callback: callback function called after configuration, which is called
Returns
nil
Example
- wifi.startsmart(6, cb())
-
+ wifi.startsmart(6, cb())
+
+
See also
- wifi.stopsmart()
@@ -1510,8 +1597,9 @@ succeed_callback: callback function called after configuration, which is called
Returns
nil
Example
- wifi.stopsmart()
-
+
+
See also
- wifi.startsmart()
@@ -1528,8 +1616,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.config("myssid","mypassword")
-
+ wifi.sta.config("myssid","mypassword")
+
+
See also
- wifi.sta.connect()
@@ -1545,8 +1634,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.connect()
-
+
+
See also
- wifi.sta.disconnect()
@@ -1562,8 +1652,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.disconnect()
-
+
+
See also
- wifi.sta.config()
@@ -1579,8 +1670,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.autoconnect()
-
+
+
See also
- wifi.sta.config()
@@ -1597,9 +1689,10 @@ password: string which is less than 64 bytes.
Returns
ip address in string, for example:”192.168.0.111”
Example
- -- print current ip
- print(wifi.sta.getip())
-
+ -- print current ip
+ print(wifi.sta.getip())
+
+
See also
- wifi.sta.getmac()
@@ -1614,9 +1707,10 @@ password: string which is less than 64 bytes.
Returns
mac address in string, for example:”18-33-44-FE-55-BB”
Example
- -- print current mac address
- print(wifi.sta.getmac())
-
+ -- print current mac address
+ print(wifi.sta.getmac())
+
+
See also
- wifi.sta.getip()
@@ -1630,17 +1724,19 @@ password: string which is less than 64 bytes.
Parameters
cfg: lua table to setup ap.
Example:
- cfg={}
- cfg.ssid="myssid"
- cfg.pwd="mypwd"
- wifi.ap.setconfig(cfg)
-
+ cfg={}
+ cfg.ssid="myssid"
+ cfg.pwd="mypwd"
+ wifi.ap.setconfig(cfg)
+
+
Returns
nil
Example
- wifi.ap.config(ssid, 'password')
-
+ wifi.ap.config(ssid, 'password')
+
+
See also
-
@@ -1655,8 +1751,9 @@ password: string which is less than 64 bytes.
Returns
ip address in string, for example:”192.168.0.111”
Example
- wifi.ap.getip()
-
+
+
See also
- wifi.ap.getmac()
@@ -1671,8 +1768,9 @@ password: string which is less than 64 bytes.
Returns
mac address in string, for example:”1A-33-44-FE-55-BB”
Example
- wifi.ap.getmac()
-
+
+
See also
- wifi.ap.getip()
@@ -1688,9 +1786,10 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- -- delay 100us
- tmr.delay(100)
-
+ -- delay 100us
+ tmr.delay(100)
+
+
See also
- tmr.now()
@@ -1705,9 +1804,10 @@ password: string which is less than 64 bytes.
Returns
uint32: value of counter
Example
- -- print current value of counter
- print(tmr.now())
-
+ -- print current value of counter
+ print(tmr.now())
+
+
See also
- tmr.delay()
@@ -1724,9 +1824,10 @@ 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 )
-
+ -- print "hello world" every 1000ms
+ tmr.alarm(1000, 1, function() print("hello world") end )
+
+
See also
- tmr.now()
@@ -1742,14 +1843,15 @@ 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 )
+ -- print "hello world" every 1000ms
+ tmr.alarm(1000, 1, function() print("hello world") end )
- -- something else
+ -- something else
+
+ -- stop alarm
+ tmr.stop()
+
- -- stop alarm
- tmr.stop()
-
See also
- tmr.now()
@@ -1768,10 +1870,10 @@ mode: gpio.OUTPUT or gpio.INPUT, or gpio.INT(interrupt mode)
Returns
nil
Example
- -- set gpio 0 as output.
- gpio.mode(0, gpio.OUTPUT)
+ -- set gpio 0 as output.
+ gpio.mode(0, gpio.OUTPUT)
+
-
See also
- gpio.read()
@@ -1786,9 +1888,10 @@ mode: gpio.OUTPUT or gpio.INPUT, or gpio.INT(interrupt mode)
Returns
number:0 - low, 1 - high
Example
- -- read value of gpio 0.
- gpio.read(0)
-
+ -- read value of gpio 0.
+ gpio.read(0)
+
+
See also
- gpio.mode()
@@ -1804,11 +1907,12 @@ level: gpio.HIGH or gpio.LOW
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)
-
+ -- 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
- gpio.mode()
@@ -1826,19 +1930,19 @@ function(level): callback function when triggered. The gpio level is the param.
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)
+ -- 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
- gpio.mode()
@@ -1857,9 +1961,10 @@ 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)
-
+ -- set pin index 0 as pwm output, frequency is 100Hz, duty cycle is 50-50.
+ pwm.setup(0, 100, 50)
+
+
See also
- pwm.start()
@@ -1874,8 +1979,9 @@ duty: 0~100, pwm duty cycle in percentage
Returns
nil
Example
- pwm.close(0)
-
+
+
See also
- pwm.start()
@@ -1890,8 +1996,9 @@ duty: 0~100, pwm duty cycle in percentage
Returns
nil
Example
- pwm.start(0)
-
+
+
See also
- pwm.stop()
@@ -1906,8 +2013,9 @@ duty: 0~100, pwm duty cycle in percentage
Returns
nil
Example
- pwm.stop(0)
-
+
+
See also
- pwm.start()
@@ -1924,8 +2032,9 @@ clock: 1~500, pwm frequency.
Returns
nil
Example
- pwm.setclock(0, 100)
-
+
+
See also
- pwm.getclock()
@@ -1940,8 +2049,9 @@ clock: 1~500, pwm frequency.
Returns
number:pwm frequency of pin
Example
- print(pwm.getclock(0))
-
+
+
See also
- pwm.setclock()
@@ -1957,8 +2067,9 @@ duty: 0~100, pwm duty cycle in percentage
Returns
nil
Example
- pwm.setduty(0, 50)
-
+
+
See also
- pwm.getduty()
@@ -1973,24 +2084,24 @@ duty: 0~100, pwm duty cycle in percentage
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.
+ -- 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
- pwm.setduty()
@@ -2009,8 +2120,9 @@ secure: true or false, true for safe link, false for ordinary link
Returns
net.server sub module
Example
- net.createServer(net.TCP, true)
-
+ net.createServer(net.TCP, true)
+
+
See also
- net.createConnection()
@@ -2026,8 +2138,9 @@ secure: true or false, true for safe link, false for ordinary link
Returns
net.server sub module
Example
- net.createConnection(net.UDP, false)
-
+ net.createConnection(net.UDP, false)
+
+
See also
- net.createServer()
@@ -2045,14 +2158,15 @@ function(net.socket): callback function, pass to Caller function as param if a c
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)
-
+ -- 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
- net.createServer()
@@ -2067,11 +2181,12 @@ function(net.socket): callback function, pass to Caller function as param if a c
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)
+ -- close server
+ sv:close()
+
+
See also
- net.createServer()
@@ -2115,11 +2230,12 @@ If event is”receive”, the second param is received data in string.
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")
-
+ 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
- net.createServer()
@@ -2216,8 +2332,9 @@ data: data can be numbers, string or lua table.
Returns
nil
Example
- i2c.write(0, "hello", "world")
-
+ i2c.write(0, "hello", "world")
+
+
See also
- i2c.read()
@@ -2233,31 +2350,31 @@ len: data length
Returns
string:data received.
Example
- id=0
- sda=1
- scl=0
+ 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))
+
-
See also
- i2c.write()