From b67b552e393cc8e92e6d2403887df9b604037847 Mon Sep 17 00:00:00 2001
From: funshine nodeMcu API Instruction
+nodeMcu API Instruction
version 0.9.2 build 2014-11-18
@@ -1161,9 +1096,8 @@ key is triged only when key is released change log:
nil
node.restart(); -
node.restart();
+
nil
node.dsleep(us); -
node.dsleep(us);
+
number:chip ID
id = node.chipid(); -
id = node.chipid();
+
number: system heap size left in bytes
heap_size = node.heap(); -
heap_size = node.heap();
+
nil
node.key("long", function(){print('hello world')}) -
node.key("long", function(){print('hello world')})
+
nil
-- turn led on forever. - node.led(0); -
-- turn led on forever.
+ node.led(0);
+
nil
-- 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)
+
nil
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)
+
nil
-- remove "foo.lua" from file system. - file.remove("foo.lua") -
-- remove "foo.lua" from file system.
+ file.remove("foo.lua")
+
- file.open()
@@ -1337,12 +1283,11 @@ mode:
nil
-- 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()
+
- file.close()
@@ -1358,12 +1303,11 @@ mode:
nil
-- 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()
+
- file.open()
@@ -1380,12 +1324,11 @@ mode:
file content in string, line by line, include EOL(‘\n’)
return nil when EOF.
-- 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()
+
- file.open()
@@ -1402,13 +1345,12 @@ return nil when EOF.
true: write ok.
nil: there is error
-- 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()
+
- file.open()
@@ -1425,13 +1367,12 @@ nil: there is error
true: write ok.
nil: there is error
-- 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()
+
- file.open()
@@ -1447,14 +1388,13 @@ nil: there is error
nil
-- 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()
+
- file.open()
@@ -1475,16 +1415,15 @@ offset: default 0
success: returns the final file position
fail: returns nil
-- 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()
+
- file.open()
@@ -1500,12 +1439,11 @@ fail: returns nil
a lua table which contains the {file name: file size} pairs
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
+
current mode after setup
wifi.setmode(wifi.STATION) -
wifi.setmode(wifi.STATION)
+
wifi operation mode
print(wifi.getmode()) -
print(wifi.getmode())
+
nil
wifi.startsmart(6, cb()) -
wifi.startsmart(6, cb())
+
nil
wifi.stopsmart() -
wifi.stopsmart()
+
nil
wifi.sta.config("myssid","mypassword") -
wifi.sta.config("myssid","mypassword")
+
- wifi.sta.connect()
@@ -1612,9 +1545,8 @@ password: string which is less than 64 bytes.
nil
wifi.sta.connect() -
wifi.sta.connect()
+
- wifi.sta.disconnect()
@@ -1630,9 +1562,8 @@ password: string which is less than 64 bytes.
nil
wifi.sta.disconnect() -
wifi.sta.disconnect()
+
- wifi.sta.config()
@@ -1648,9 +1579,8 @@ password: string which is less than 64 bytes.
nil
wifi.sta.autoconnect() -
wifi.sta.autoconnect()
+
- wifi.sta.config()
@@ -1667,10 +1597,9 @@ password: string which is less than 64 bytes.
ip address in string, for example:”192.168.0.111”
-- print current ip - print(wifi.sta.getip()) -
-- print current ip
+ print(wifi.sta.getip())
+
mac address in string, for example:”18-33-44-FE-55-BB”
-- print current mac address - print(wifi.sta.getmac()) -
-- print current mac address
+ print(wifi.sta.getmac())
+
cfg: lua table to setup ap.
cfg={} - cfg.ssid="myssid" - cfg.pwd="mypwd" - wifi.ap.setconfig(cfg) -
cfg={}
+ cfg.ssid="myssid"
+ cfg.pwd="mypwd"
+ wifi.ap.setconfig(cfg)
+
nil
wifi.ap.config(ssid, 'password') -
wifi.ap.config(ssid, 'password')
+
ip address in string, for example:”192.168.0.111”
wifi.ap.getip() -
wifi.ap.getip()
+
mac address in string, for example:”1A-33-44-FE-55-BB”
wifi.ap.getmac() -
wifi.ap.getmac()
+
nil
-- delay 100us - tmr.delay(100) -
-- delay 100us
+ tmr.delay(100)
+
uint32: value of counter
-- print current value of counter - print(tmr.now()) -
-- print current value of counter
+ print(tmr.now())
+
nil
-- 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 )
+
nil
-- 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() -
nil
-- set gpio 0 as output. - gpio.mode(0, gpio.OUTPUT) -
-- set gpio 0 as output.
+ gpio.mode(0, gpio.OUTPUT)
+
number:0 - low, 1 - high
-- read value of gpio 0. - gpio.read(0) -
-- read value of gpio 0.
+ gpio.read(0)
+
nil
-- 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)
+
- gpio.mode()
@@ -1908,19 +1826,19 @@ function(level): callback function when triggered. The gpio level is the param.
nil
-- 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)
+
- gpio.mode()
@@ -1939,10 +1857,9 @@ duty: 0~100, pwm duty cycle in percentage
nil
-- 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)
+
nil
pwm.close(0) -
pwm.close(0)
+
nil
pwm.start(0) -
pwm.start(0)
+
nil
pwm.stop(0) -
pwm.stop(0)
+
nil
pwm.setclock(0, 100) -
pwm.setclock(0, 100)
+
number:pwm frequency of pin
print(pwm.getclock(0)) -
print(pwm.getclock(0))
+
nil
pwm.setduty(0, 50) -
pwm.setduty(0, 50)
+
nil
-- 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.
+
net.server sub module
net.createServer(net.TCP, true) -
net.createServer(net.TCP, true)
+
net.server sub module
net.createConnection(net.UDP, false) -
net.createConnection(net.UDP, false)
+
nil
-- 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)
+
nil
-- 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()
+
nil
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")
+
nil
i2c.write(0, "hello", "world") -
i2c.write(0, "hello", "world")
+
string:data received.
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)) -