nodeMcu API Instruction
+nodeMcu API Instruction
version 0.9.2 build 2014-11-18
change log:
@@ -1161,9 +1096,8 @@ key is triged only when key is releasedReturns
nil
Example
-node.restart(); -
node.restart();
+
See also
@@ -1179,9 +1113,8 @@ key is triged only when key is releasedReturns
nil
Example
-node.dsleep(us); -
node.dsleep(us);
+
See also
@@ -1196,9 +1129,8 @@ key is triged only when key is releasedReturns
number:chip ID
Example
-id = node.chipid(); -
id = node.chipid();
+
See also
@@ -1213,9 +1145,8 @@ 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
@@ -1232,9 +1163,8 @@ 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
@@ -1250,10 +1180,9 @@ 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
@@ -1269,10 +1198,9 @@ 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
@@ -1288,14 +1216,33 @@ 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(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
@@ -1311,10 +1258,9 @@ 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()
@@ -1337,12 +1283,11 @@ 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()
@@ -1358,12 +1303,11 @@ 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()
@@ -1380,12 +1324,11 @@ 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()
@@ -1402,13 +1345,12 @@ 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()
@@ -1425,13 +1367,12 @@ 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()
@@ -1447,14 +1388,13 @@ 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()
@@ -1475,16 +1415,15 @@ 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()
@@ -1500,12 +1439,11 @@ 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
@@ -1523,9 +1461,8 @@ fail: returns nilReturns
current mode after setup
Example
-wifi.setmode(wifi.STATION) -
wifi.setmode(wifi.STATION)
+
See also
@@ -1540,9 +1477,8 @@ fail: returns nilReturns
wifi operation mode
Example
-print(wifi.getmode()) -
print(wifi.getmode())
+
See also
@@ -1558,9 +1494,8 @@ succeed_callback: callback function called after configuration, which is calledReturns
nil
Example
-wifi.startsmart(6, cb()) -
wifi.startsmart(6, cb())
+
See also
@@ -1575,9 +1510,8 @@ succeed_callback: callback function called after configuration, which is calledReturns
nil
Example
-wifi.stopsmart() -
wifi.stopsmart()
+
See also
@@ -1594,9 +1528,8 @@ 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()
@@ -1612,9 +1545,8 @@ password: string which is less than 64 bytes.
Returns
nil
Example
-wifi.sta.connect() -
wifi.sta.connect()
+
See also
- wifi.sta.disconnect()
@@ -1630,9 +1562,8 @@ password: string which is less than 64 bytes.
Returns
nil
Example
-wifi.sta.disconnect() -
wifi.sta.disconnect()
+
See also
- wifi.sta.config()
@@ -1648,9 +1579,8 @@ password: string which is less than 64 bytes.
Returns
nil
Example
-wifi.sta.autoconnect() -
wifi.sta.autoconnect()
+
See also
- wifi.sta.config()
@@ -1667,10 +1597,9 @@ 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
@@ -1685,10 +1614,9 @@ 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
@@ -1702,19 +1630,17 @@ 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
@@ -1729,9 +1655,8 @@ 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
@@ -1746,9 +1671,8 @@ 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
@@ -1764,10 +1688,9 @@ password: string which is less than 64 bytes.Returns
nil
Example
--- delay 100us - tmr.delay(100) -
-- delay 100us
+ tmr.delay(100)
+
See also
@@ -1782,10 +1705,9 @@ 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
@@ -1802,10 +1724,9 @@ 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
@@ -1821,15 +1742,14 @@ 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 - - -- stop alarm - tmr.stop() -
See also
@@ -1848,10 +1768,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
@@ -1866,10 +1786,9 @@ 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
@@ -1885,12 +1804,11 @@ 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()
@@ -1908,19 +1826,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()
@@ -1939,10 +1857,9 @@ 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
@@ -1957,9 +1874,8 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
-pwm.close(0) -
pwm.close(0)
+
See also
@@ -1974,9 +1890,8 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
-pwm.start(0) -
pwm.start(0)
+
See also
@@ -1991,9 +1906,8 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
-pwm.stop(0) -
pwm.stop(0)
+
See also
@@ -2010,9 +1924,8 @@ clock: 1~500, pwm frequency.Returns
nil
Example
-pwm.setclock(0, 100) -
pwm.setclock(0, 100)
+
See also
@@ -2027,9 +1940,8 @@ clock: 1~500, pwm frequency.Returns
number:pwm frequency of pin
Example
-print(pwm.getclock(0)) -
print(pwm.getclock(0))
+
See also
@@ -2045,9 +1957,8 @@ duty: 0~100, pwm duty cycle in percentageReturns
nil
Example
-pwm.setduty(0, 50) -
pwm.setduty(0, 50)
+
See also
@@ -2062,24 +1973,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
@@ -2098,9 +2009,8 @@ 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
@@ -2116,9 +2026,8 @@ 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
@@ -2136,15 +2045,14 @@ 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
@@ -2159,12 +2067,11 @@ 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
@@ -2208,12 +2115,11 @@ 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
@@ -2310,9 +2216,8 @@ data: data can be numbers, string or lua table.Returns
nil
Example
-i2c.write(0, "hello", "world") -
i2c.write(0, "hello", "world")
+
See also
@@ -2328,31 +2233,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)) -