nodeMcu API Instruction
+nodeMcu API Instruction
version 0.9.2 build 2014-11-18
change log:
2014-11-18
+bug fixed: net.socket:connect() has no effect.
2014-11-18
bug fixed: as a tcp server, the connection can’t closed with :close().
tcp server: inactive connection will closed by server in 30s (previously 180s).
add a test api node.input() to put lua chunk into lua interpretor, multi-line supported.
@@ -1094,8 +1161,9 @@ key is triged only when key is released
Returns
nil
Example
- node.restart();
-
+node.restart(); +
See also
@@ -1111,8 +1179,9 @@ key is triged only when key is releasedReturns
nil
Example
- node.dsleep(us);
-
+node.dsleep(us); +
See also
@@ -1127,8 +1196,9 @@ key is triged only when key is releasedReturns
number:chip ID
Example
- id = node.chipid();
-
+id = node.chipid(); +
See also
@@ -1143,8 +1213,9 @@ key is triged only when key is releasedReturns
number: system heap size left in bytes
Example
- heap_size = node.heap();
-
+heap_size = node.heap(); +
See also
@@ -1161,8 +1232,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
@@ -1178,9 +1250,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
@@ -1196,9 +1269,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
@@ -1214,13 +1288,14 @@ 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. +
See also
@@ -1236,9 +1311,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()
@@ -1261,11 +1337,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()
@@ -1281,11 +1358,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()
@@ -1302,11 +1380,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()
@@ -1323,12 +1402,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()
@@ -1345,12 +1425,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()
@@ -1366,13 +1447,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()
@@ -1393,15 +1475,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()
@@ -1417,11 +1500,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
@@ -1439,8 +1523,9 @@ fail: returns nilReturns
current mode after setup
Example
- wifi.setmode(wifi.STATION)
-
+wifi.setmode(wifi.STATION) +
See also
@@ -1455,8 +1540,9 @@ fail: returns nilReturns
wifi operation mode
Example
- print(wifi.getmode())
-
+print(wifi.getmode()) +
See also
@@ -1472,8 +1558,9 @@ succeed_callback: callback function called after configuration, which is calledReturns
nil
Example
- wifi.startsmart(6, cb())
-
+wifi.startsmart(6, cb()) +
See also
@@ -1488,8 +1575,9 @@ succeed_callback: callback function called after configuration, which is calledReturns
nil
Example
- wifi.stopsmart()
-
+wifi.stopsmart() +
See also
@@ -1506,8 +1594,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()
@@ -1523,8 +1612,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.connect()
-
+wifi.sta.connect() +
See also
- wifi.sta.disconnect()
@@ -1540,8 +1630,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.disconnect()
-
+wifi.sta.disconnect() +
See also
- wifi.sta.config()
@@ -1557,8 +1648,9 @@ password: string which is less than 64 bytes.
Returns
nil
Example
- wifi.sta.autoconnect()
-
+wifi.sta.autoconnect() +
See also
- wifi.sta.config()
@@ -1575,9 +1667,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
@@ -1592,9 +1685,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
@@ -1608,17 +1702,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
@@ -1633,8 +1729,9 @@ password: string which is less than 64 bytes.Returns
ip address in string, for example:”192.168.0.111”
Example
- wifi.ap.getip()
-
+wifi.ap.getip() +
See also
@@ -1649,8 +1746,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()
-
+wifi.ap.getmac() +
See also
@@ -1666,9 +1764,10 @@ password: string which is less than 64 bytes.Returns
nil
Example
- -- delay 100us
- tmr.delay(100)
-
+-- delay 100us + tmr.delay(100) +
See also
@@ -1683,9 +1782,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
@@ -1702,9 +1802,10 @@ function do(): callback function for alarm timed outReturns
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
@@ -1720,14 +1821,15 @@ function do(): callback function for alarm timed outReturns
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
@@ -1746,10 +1848,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
@@ -1764,9 +1866,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
@@ -1782,11 +1885,12 @@ level: gpio.HIGH or gpio.LOWReturns
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()
@@ -1804,19 +1908,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()
@@ -1835,9 +1939,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
@@ -1852,8 +1957,9 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
- pwm.close(0)
-
+pwm.close(0) +
See also
@@ -1868,8 +1974,9 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
- pwm.start(0)
-
+pwm.start(0) +
See also
@@ -1884,8 +1991,9 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
- pwm.stop(0)
-
+pwm.stop(0) +
See also
@@ -1902,8 +2010,9 @@ clock: 1~500, pwm frequency.Returns
nil
Example
- pwm.setclock(0, 100)
-
+pwm.setclock(0, 100) +
See also
@@ -1918,8 +2027,9 @@ clock: 1~500, pwm frequency.Returns
number:pwm frequency of pin
Example
- print(pwm.getclock(0))
-
+print(pwm.getclock(0)) +
See also
@@ -1935,8 +2045,9 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
- pwm.setduty(0, 50)
-
+pwm.setduty(0, 50) +
See also
@@ -1951,24 +2062,24 @@ duty: 0~100, pwm duty cycle in percentageReturns
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
@@ -1987,8 +2098,9 @@ secure: true or false, true for safe link, false for ordinary linkReturns
net.server sub module
Example
- net.createServer(net.TCP, true)
-
+net.createServer(net.TCP, true) +
See also
@@ -2004,8 +2116,9 @@ secure: true or false, true for safe link, false for ordinary linkReturns
net.server sub module
Example
- net.createConnection(net.UDP, false)
-
+net.createConnection(net.UDP, false) +
See also
@@ -2023,14 +2136,15 @@ function(net.socket): callback function, pass to Caller function as param if a cReturns
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
@@ -2045,11 +2159,12 @@ function(net.socket): callback function, pass to Caller function as param if a cReturns
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
@@ -2093,11 +2208,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
@@ -2194,8 +2310,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
@@ -2211,31 +2328,31 @@ len: data lengthReturns
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))
+
-