diff --git a/0.9.2/512k-flash/nodemcu_512k.bin b/0.9.2/512k-flash/nodemcu_512k.bin index 89bf0c28..b51cddc3 100644 Binary files a/0.9.2/512k-flash/nodemcu_512k.bin and b/0.9.2/512k-flash/nodemcu_512k.bin differ diff --git a/README.html b/README.html index a5f358eb..6ec3d874 100644 --- a/README.html +++ b/README.html @@ -1014,9 +1014,74 @@ body .markdown-body page-break-after: avoid; } } -README

NodeMcu

+README

NodeMcu

A lua based firmware for wifi-soc esp8266

-

version 0.9.2 build 2014-11-19

+

version 0.9.2 build 2014-11-20

Change log

change log

变更日志

@@ -1066,98 +1131,106 @@ for most esp8266 modules, just pull GPIO0 down and restart.

braudrate:9600

Start play

Connect to your ap

-
    print(wifi.sta.getip())
-    --0.0.0.0
-    wifi.setmode(wifi.STATION)
-    wifi.sta.config("SSID","password")
-    print(wifi.sta.getip())
-    --192.168.18.110
-
+
    print(wifi.sta.getip())
+    --0.0.0.0
+    wifi.setmode(wifi.STATION)
+    wifi.sta.config("SSID","password")
+    print(wifi.sta.getip())
+    --192.168.18.110
+
+

Manipulate hardware like a arduino

-
    pin = 1
-    gpio.mode(pin,gpio.OUTPUT)
-    gpio.write(pin,gpio.HIGH)
-    print(gpio.read(pin))
-
+
    pin = 1
+    gpio.mode(pin,gpio.OUTPUT)
+    gpio.write(pin,gpio.HIGH)
+    print(gpio.read(pin))
+
+

Write network application in nodejs style

-
    -- A simple http client
-    conn=net.createConnection(net.TCP, false) 
-    conn:on("receive", function(conn, payload) print(c) end )
-    conn:connect(80,"115.239.210.27")
-    conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
-        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
-
+
    -- A simple http client
+    conn=net.createConnection(net.TCP, 0) 
+    conn:on("receive", function(conn, payload) print(c) end )
+    conn:connect(80,"115.239.210.27")
+    conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
+        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
+
+

Or a simple http server

-
    -- A simple http server
-    srv=net.createServer(net.TCP) 
-    srv:listen(80,function(conn) 
-      conn:on("receive",function(conn,payload) 
-        print(payload) 
-        conn:send("<h1> Hello, NodeMcu.</h1>")
-      end) 
-    end)
-
+
    -- A simple http server
+    srv=net.createServer(net.TCP) 
+    srv:listen(80,function(conn) 
+      conn:on("receive",function(conn,payload) 
+        print(payload) 
+        conn:send("<h1> Hello, NodeMcu.</h1>")
+      end) 
+    end)
+
+

Do something shining

-
  function led(r,g,b) 
-    pwm.setduty(0,r) 
-    pwm.setduty(1,g) 
-    pwm.setduty(2,b) 
-  end
-  pwm.setup(0,500,50) 
-  pwm.setup(1,500,50) 
-  pwm.setup(2,500,50)
-  pwm.start(0) 
-  pwm.start(1) 
-  pwm.start(2)
-  led(50,0,0) -- red
-  led(0,0,50) -- blue
-
+
  function led(r,g,b) 
+    pwm.setduty(0,r) 
+    pwm.setduty(1,g) 
+    pwm.setduty(2,b) 
+  end
+  pwm.setup(0,500,50) 
+  pwm.setup(1,500,50) 
+  pwm.setup(2,500,50)
+  pwm.start(0) 
+  pwm.start(1) 
+  pwm.start(2)
+  led(50,0,0) -- red
+  led(0,0,50) -- blue
+
+ -
  lighton=0
-  tmr.alarm(1000,1,function()
-    if lighton==0 then 
-      lighton=1 
-      led(50,50,50) 
-    else 
-      lighton=0 
-      led(0,0,0) 
-    end 
-  end)
-
+
  lighton=0
+  tmr.alarm(1000,1,function()
+    if lighton==0 then 
+      lighton=1 
+      led(50,50,50) 
+    else 
+      lighton=0 
+      led(0,0,0) 
+    end 
+  end)
+
+

If you want to run something when system started

-
  --init.lua will be excuted
-  file.open("init.lua","w")
-  file.writeline([[print("Hello, do this at the beginning.")]])
-  file.close()
-  node.restart()  -- this will restart the module.
-
+
  --init.lua will be excuted
+  file.open("init.lua","w")
+  file.writeline([[print("Hello, do this at the beginning.")]])
+  file.close()
+  node.restart()  -- this will restart the module.
+
+

With below code, you can telnet to your esp8266 now

-
    -- 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(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)
+
+

Check this out

Tencent QQ group: 309957875

diff --git a/README.md b/README.md index 66b74d9e..f1448815 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # **NodeMcu** # ###A lua based firmware for wifi-soc esp8266 -version 0.9.2 build 2014-11-19 +version 0.9.2 build 2014-11-20 # Change log [change log](https://github.com/funshine/nodemcu-firmware/wiki/nodemcu_api_en#change_log)
[变更日志](https://github.com/funshine/nodemcu-firmware/wiki/nodemcu_api_cn#change_log) @@ -76,7 +76,7 @@ braudrate:9600 ```lua -- A simple http client - conn=net.createConnection(net.TCP, false) + conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(c) end ) conn:connect(80,"115.239.210.27") conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n" diff --git a/nodemcu_api_cn.html b/nodemcu_api_cn.html index dbeeb515..19db1b99 100644 --- a/nodemcu_api_cn.html +++ b/nodemcu_api_cn.html @@ -1014,11 +1014,81 @@ body .markdown-body page-break-after: avoid; } } -nodemcu_api_cn

nodeMcu API说明

+nodemcu_api_cn

nodeMcu API说明

English Version

-

版本 0.9.2 build 2014-11-19

+

版本 0.9.2 build 2014-11-20

变更日志:

+

2014-11-20

+修正tmr.delay,支持2s以上的延时,但是长延时可能会引起beacon timer out,导致与AP之间重新连接。

+增加tmr.wdclr(),用来重置看门狗计数器,用在长循环中,以防芯片因看门狗重启。

+修正net模块UDP无法连接问题。

+createServer(net.TCP, timeout)函数增加连接超时参数设置。

2014-11-19

增加adc模块,adc.read(0)读取adc的值。

wifi模块增加wifi.sta.getap() 函数,用于获取ap列表。

@@ -1104,8 +1174,9 @@ node模块中删除了log函数。

返回值

nil

示例

-
    node.restart();
-
+
    node.restart();
+
+

参见

-

@@ -1121,8 +1192,9 @@ node模块中删除了log函数。

返回值

nil

示例

-
    node.dsleep(us);
-
+
    node.dsleep(us);
+
+

参见

-

@@ -1137,8 +1209,9 @@ node模块中删除了log函数。

返回值

number:芯片ID

示例

-
    id = node.chipid();
-
+
    id = node.chipid();
+
+

参见

-

@@ -1153,8 +1226,9 @@ node模块中删除了log函数。

返回值

number: 系统剩余内存字节数

示例

-
    heap_size = node.heap();
-
+
    heap_size = node.heap();
+
+

参见

-

@@ -1171,8 +1245,9 @@ function(): 用户自定义的按键回调函数。 如果为nil, 则取消用

返回值

nil

示例

-
    node.key("long", function(){print('hello world')})
-
+
    node.key("long", function(){print('hello world')})
+
+

参见

-

@@ -1188,9 +1263,10 @@ High: LED打开时间,单位:毫秒,时间分辨率:80~100ms

返回值

nil

示例

-
    -- LED常亮.
-    node.led(0);
-
+
    -- LED常亮.
+    node.led(0);
+
+

参见

-

@@ -1206,9 +1282,10 @@ High: LED打开时间,单位:毫秒,时间分辨率:80~100ms

返回值

nil

示例

-
    -- 注意:该函数不支持在命令行中使用。
-    sk:on("receive", function(conn, payload) node.input(payload) end)
-
+
    -- 注意:该函数不支持在命令行中使用。
+    sk:on("receive", function(conn, payload) node.input(payload) end)
+
+

参见

-

@@ -1224,13 +1301,14 @@ serial_debug: 1:将输出送至串口; 0:输出不送至串口

返回值

nil

示例

-
    function tonet(str)
-      sk:send(str)
-      -- print(str) 错误!!! 千万不要在此函数中再使用print函数
-      -- 因为这样会导致函数的嵌套调用!!
-    end
-    node.ouput(tonet, 1)  -- serial also get the lua output.
-
+
    function tonet(str)
+      sk:send(str)
+      -- print(str) 错误!!! 千万不要在此函数中再使用print函数
+      -- 因为这样会导致函数的嵌套调用!!
+    end
+    node.ouput(tonet, 1)  -- serial also get the lua output.
+
+

参见

-

@@ -1246,9 +1324,10 @@ serial_debug: 1:将输出送至串口; 0:输出不送至串口

返回值

nil

示例

-
    -- 删除foo.lua文件
-    file.remove("foo.lua")
-
+
    -- 删除foo.lua文件
+    file.remove("foo.lua")
+
+

参见

- file.open()

@@ -1271,11 +1350,12 @@ mode:

返回值

nil

示例

-
    -- 打开'init.lua',并打印文件的第一行。
-    file.open("init.lua", "r")
-    print(file.readline())
-    file.close()
-
+
    -- 打开'init.lua',并打印文件的第一行。
+    file.open("init.lua", "r")
+    print(file.readline())
+    file.close()
+
+

参见

- file.close()

@@ -1291,11 +1371,12 @@ mode:

返回值

nil

示例

-
    -- 打开'init.lua',并打印文件的第一行,然后关闭文件。
-    file.open("init.lua", "r")
-    print(file.readline())
-    file.close()
-
+
    -- 打开'init.lua',并打印文件的第一行,然后关闭文件。
+    file.open("init.lua", "r")
+    print(file.readline())
+    file.close()
+
+

参见

- file.open()

@@ -1312,11 +1393,12 @@ mode:

逐行返回文件内容。返回值末尾包含EOL(‘\n’)

如果读到EOF返回nil。

示例

-
    -- 打开'init.lua',读取并打印文件的第一行,然后关闭文件。
-    file.open("init.lua", "r")
-    print(file.readline())
-    file.close()
-
+
    -- 打开'init.lua',读取并打印文件的第一行,然后关闭文件。
+    file.open("init.lua", "r")
+    print(file.readline())
+    file.close()
+
+

参见

- file.open()

@@ -1333,12 +1415,13 @@ mode:

true: 写入成功

nil: 写入失败

示例

-
    -- 以'a+'的模式打开'init.lua'
-    file.open("init.lua", "a+")
-    -- 将'foo bar'写到文件的末尾
-    file.writeline('foo bar')
-    file.close()
-
+
    -- 以'a+'的模式打开'init.lua'
+    file.open("init.lua", "a+")
+    -- 将'foo bar'写到文件的末尾
+    file.writeline('foo bar')
+    file.close()
+
+

参见

- file.open()

@@ -1355,12 +1438,13 @@ nil: 写入失败

true: 写入成功

nil: 写入失败

示例

-
    -- 以'a+'的模式打开'init.lua'
-    file.open("init.lua", "a+")
-    -- 将'foo bar'写到文件的末尾
-    file.writeline('foo bar')
-    file.close()
-
+
    -- 以'a+'的模式打开'init.lua'
+    file.open("init.lua", "a+")
+    -- 将'foo bar'写到文件的末尾
+    file.writeline('foo bar')
+    file.close()
+
+

参见

- file.open()

@@ -1376,13 +1460,14 @@ nil: 写入失败

返回值

nil

示例

-
    -- 以'a+'的模式打开'init.lua'
-    file.open("init.lua", "a+")
-    -- 将'foo bar'写到文件的末尾
-    file.write('foo bar')
-    file.flush()
-    file.close()
-
+
    -- 以'a+'的模式打开'init.lua'
+    file.open("init.lua", "a+")
+    -- 将'foo bar'写到文件的末尾
+    file.write('foo bar')
+    file.flush()
+    file.close()
+
+

参见

- file.open()

@@ -1403,17 +1488,18 @@ offset: default 0

成功: 返回当前的文件读写位置

失败: 返回nil

示例

-
    -- 以'a+'的模式打开'init.lua'
-    file.open("init.lua", "a+")
-    -- 将'foo bar'写到文件的末尾
-    file.write('foo bar')
-    file.flush()
-    --将文件读写位置设置在文件开始
-    file.seek("set")
-    --读取并打印文件的第一行
-    print(file.readline())
-    file.close()
-
+
    -- 以'a+'的模式打开'init.lua'
+    file.open("init.lua", "a+")
+    -- 将'foo bar'写到文件的末尾
+    file.write('foo bar')
+    file.flush()
+    --将文件读写位置设置在文件开始
+    file.seek("set")
+    --读取并打印文件的第一行
+    print(file.readline())
+    file.close()
+
+

参见

- file.open()

@@ -1429,11 +1515,12 @@ offset: default 0

返回值

返回包含{文件名:文件大小}的lua table

示例

-
    l = file.list();
-    for k,v in pairs(l) do
-      print("name:"..k..", size:"..v)
-    end
-
+
    l = file.list();
+    for k,v in pairs(l) do
+      print("name:"..k..", size:"..v)
+    end
+
+

参见

- file.remove()

@@ -1451,8 +1538,9 @@ offset: default 0

返回值

返回设置之后的mode值

示例

-
    wifi.setmode(wifi.STATION)
-
+
    wifi.setmode(wifi.STATION)
+
+

参见

- wifi.getmode()

@@ -1467,8 +1555,9 @@ offset: default 0

返回值

返回wifi的工作模式

示例

-
    print(wifi.getmode())
-
+
    print(wifi.getmode())
+
+

参见

- wifi.setmode()

@@ -1484,8 +1573,9 @@ succeed_callback: 配置成功的回调函数,配置成功并连接至AP后调

返回值

nil

示例

-
    wifi.startsmart(6, cb())
-
+
    wifi.startsmart(6, cb())
+
+

参见

- wifi.stopsmart()

@@ -1500,8 +1590,9 @@ succeed_callback: 配置成功的回调函数,配置成功并连接至AP后调

返回值

nil

示例

-
    wifi.stopsmart()
-
+
    wifi.stopsmart()
+
+

参见

- wifi.startsmart()

@@ -1518,8 +1609,9 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    wifi.sta.config("myssid","mypassword")
-
+
    wifi.sta.config("myssid","mypassword")
+
+

参见

- wifi.sta.connect()

@@ -1535,8 +1627,9 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    wifi.sta.connect()
-
+
    wifi.sta.connect()
+
+

参见

- wifi.sta.disconnect()

@@ -1552,8 +1645,9 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    wifi.sta.disconnect()
-
+
    wifi.sta.disconnect()
+
+

参见

- wifi.sta.config()

@@ -1569,8 +1663,9 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    wifi.sta.autoconnect()
-
+
    wifi.sta.autoconnect()
+
+

参见

- wifi.sta.config()

@@ -1587,9 +1682,10 @@ password: 字符串,长度小于64字节。

返回值

ip地址字符串,如:”192.168.0.111”

示例

-
    -- print current ip
-    print(wifi.sta.getip())
-
+
    -- print current ip
+    print(wifi.sta.getip())
+
+

参见

- wifi.sta.getmac()

@@ -1604,9 +1700,10 @@ password: 字符串,长度小于64字节。

返回值

mac地址字符串,如:”18-33-44-FE-55-BB”

示例

-
    -- 打印当前的mac地址
-    print(wifi.sta.getmac())
-
+
    -- 打印当前的mac地址
+    print(wifi.sta.getmac())
+
+

参见

- wifi.sta.getip()

@@ -1622,14 +1719,15 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    -- print ap list
-    function listap(t)
-      for k,v in pairs(t) do
-        print(k.." : "..v)
-      end
-    end
-    wifi.sta.getap(listap)
-
+
    -- print ap list
+    function listap(t)
+      for k,v in pairs(t) do
+        print(k.." : "..v)
+      end
+    end
+    wifi.sta.getap(listap)
+
+

参见

- wifi.sta.getip()

@@ -1643,17 +1741,19 @@ password: 字符串,长度小于64字节。

参数

cfg: 设置AP的lua table

示例:

-
     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')
+
+

参见

-

@@ -1668,8 +1768,9 @@ password: 字符串,长度小于64字节。

返回值

ip地址字符串,如:”192.168.0.111”

示例

-
    wifi.ap.getip()
-
+
    wifi.ap.getip()
+
+

参见

- wifi.ap.getmac()

@@ -1684,8 +1785,9 @@ password: 字符串,长度小于64字节。

返回值

mac地址字符串,如:”1A-33-44-FE-55-BB”

示例

-
    wifi.ap.getmac()
-
+
    wifi.ap.getmac()
+
+

参见

- wifi.ap.getip()

@@ -1701,9 +1803,10 @@ password: 字符串,长度小于64字节。

返回值

nil

示例

-
    -- delay 100us
-    tmr.delay(100)
-
+
    -- delay 100us
+    tmr.delay(100)
+
+

参见

- tmr.now()

@@ -1718,9 +1821,10 @@ password: 字符串,长度小于64字节。

返回值

uint32: value of counter

示例

-
    -- 打印计数器的当前值。
-    print(tmr.now())
-
+
    -- 打印计数器的当前值。
+    print(tmr.now())
+
+

参见

- tmr.delay()

@@ -1738,9 +1842,10 @@ function do(): 定时器到时回调函数。

返回值

nil

示例

-
    -- 每1000ms输出一个hello world
-    tmr.alarm(1000, 1, function() print("hello world") end )
-
+
    -- 每1000ms输出一个hello world
+    tmr.alarm(1000, 1, function() print("hello world") end )
+
+

参见

- tmr.now()

@@ -1755,537 +1860,573 @@ function do(): 定时器到时回调函数。

返回值

nil

示例

-
    -- 每隔1000ms打印hello world
-    tmr.alarm(1000, 1, function() print("hello world") end )
+
    -- 每隔1000ms打印hello world
+    tmr.alarm(1000, 1, function() print("hello world") end )
 
-    -- 其它代码
+    -- 其它代码
+
+    -- 停止闹钟
+    tmr.stop()
+
- -- 停止闹钟 - tmr.stop() -

参见

- tmr.now()

+

+

tmr.wdclr()

+

描述

+

清除看门狗计数器。

+

语法

+

tmr.wdclr()

+

参数

+

nil.

+

返回值

+

nil

+

示例

+
    for i=1,10000 do 
+      print(i)
+      tmr.wdclr()   -- 一个长时间的循环或者事务,需内部调用tmr.wdclr() 清楚看门狗计数器,防止重启。
+    end 
+
+ + +

参见

+

- tmr.delay()

GPIO 模块

-

常量

+

常量

gpio.OUTPUT, gpio.INPUT, gpio.INT, gpio.HIGH, gpio.LOW

gpio.mode()

-

描述

+

描述

将pin初始化为GPIO并设置输入输出模式。

-

语法

+

语法

gpio.mode(pin, mode)

-

参数

+

参数

pin: 0~11, IO编号

mode: 取值为:gpio.OUTPUT or gpio.INPUT, or gpio.INT(中断模式)

-

返回值

+

返回值

nil

-

示例

-
    -- 将GPIO0设置为输出模式
-    gpio.mode(0, gpio.OUTPUT)
+

示例

+
    -- 将GPIO0设置为输出模式
+    gpio.mode(0, gpio.OUTPUT)
+
-
-

参见

+

参见

- gpio.read()

gpio.read()

-

描述

+

描述

读取管脚电平高低。

-

语法

+

语法

gpio.read(pin)

-

参数

+

参数

pin: 0~11, IO编号

-

返回值

+

返回值

number:0:低电平, 1:高电平。

-

示例

-
    -- 读取GPIO0的电平
-    gpio.read(0)
-
+

示例

+
    -- 读取GPIO0的电平
+    gpio.read(0)
+
-

参见

+ +

参见

- gpio.mode()

gpio.write()

-

描述

+

描述

设置管脚电平

-

语法

+

语法

gpio.write(pin)

-

参数

+

参数

pin: 0~11, IO编号

level: gpio.HIGH or gpio.LOW

-

返回值

+

返回值

nil

-

示例

-
    -- 设置GPIP1为输出模式,并将输出电平设置为高
-    pin=1
-    gpio.mode(pin, gpio.OUTPUT)
-    gpio.write(pin, gpio.HIGH)
-
+

示例

+
    -- 设置GPIP1为输出模式,并将输出电平设置为高
+    pin=1
+    gpio.mode(pin, gpio.OUTPUT)
+    gpio.write(pin, gpio.HIGH)
+
-

参见

+ +

参见

- gpio.mode()

- gpio.read()

gpio.trig()

-

描述

+

描述

设置管脚中断模式的回调函数。

-

语法

+

语法

gpio.trig(pin, type, function(level))

-

参数

+

参数

pin: 0~11, IO编号

type: 取值为”up”, “down”, “both”, “low”, “high”, 分别代表上升沿、下降沿、双边沿、低电平、高电平触发方式。

function(level): 中断触发的回调函数,GPIO的电平作为输入参数。如果此处没有定义函数,则使用之前定义的回调函数。

-

返回值

+

返回值

nil

-

示例

-
    -- 使用GPIO0检测输入脉冲宽度
-    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)
+

示例

+
    -- 使用GPIO0检测输入脉冲宽度
+    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()

- gpio.write()

PWM模块

pwm.setup()

-

描述

+

描述

设置管脚为pwm模式,最多支持6个pwm。

-

语法

+

语法

pwm.setup(pin, clock, duty)

-

参数

+

参数

pin: 0~11, IO编号

clock: 1~500, pwm频率

duty: 0~100, pwm占空比,百分比表示。

-

返回值

-

nil

-

示例

-
    -- 将管脚0设置为pwm输出模式,频率100Hz,占空比50-50
-    pwm.setup(0, 100, 50)
-
- -

参见

-

- pwm.start()

-

-

pwm.close()

-

描述

-

退出pwm模式。

-

语法

-

pwm.close(pin)

-

参数

-

pin: 0~11, IO编号

返回值

nil

示例

-
    pwm.close(0)
-
+
    -- 将管脚0设置为pwm输出模式,频率100Hz,占空比50-50
+    pwm.setup(0, 100, 50)
+
+

参见

- pwm.start()

-

-

pwm.start()

+

+

pwm.close()

描述

-

pwm启动,可以在对应的GPIO检测到波形。

+

退出pwm模式。

语法

-

pwm.start(pin)

+

pwm.close(pin)

参数

pin: 0~11, IO编号

返回值

nil

示例

-
    pwm.start(0)
-
+
    pwm.close(0)
+
+

参见

-

- pwm.stop()

-

-

pwm.stop()

+

- pwm.start()

+

+

pwm.start()

描述

-

暂停pwm输出波形。

+

pwm启动,可以在对应的GPIO检测到波形。

语法

-

pwm.stop(pin)

+

pwm.start(pin)

参数

pin: 0~11, IO编号

返回值

nil

示例

-
    pwm.stop(0)
-
+
    pwm.start(0)
+
+

参见

-

- pwm.start()

-

-

pwm.setclock()

+

- pwm.stop()

+

+

pwm.stop()

描述

-

设置pwm的频率

--Note: 设置pwm频率将会同步改变其他pwm输出的频率,当前版本的所有pwm仅支持同一频率输出。

+

暂停pwm输出波形。

语法

-

pwm.setclock(pin, clock)

+

pwm.stop(pin)

参数

-

pin: 0~11, IO编号

-clock: 1~500, pwm周期

+

pin: 0~11, IO编号

返回值

nil

示例

-
    pwm.setclock(0, 100)
-
+
    pwm.stop(0)
+
+

参见

+

- pwm.start()

+

+

pwm.setclock()

+

描述

+

设置pwm的频率

+-Note: 设置pwm频率将会同步改变其他pwm输出的频率,当前版本的所有pwm仅支持同一频率输出。

+

语法

+

pwm.setclock(pin, clock)

+

参数

+

pin: 0~11, IO编号

+clock: 1~500, pwm周期

+

返回值

+

nil

+

示例

+
    pwm.setclock(0, 100)
+
+ + +

参见

- pwm.getclock()

pwm.getclock()

-

描述

+

描述

获取pin的pwm工作频率

-

语法

+

语法

pwm.getclock(pin)

-

参数

+

参数

pin: 0~11, IO编号

-

返回值

+

返回值

number:pin的pwm工作频率

-

示例

-
    print(pwm.getclock(0))
-
+

示例

+
    print(pwm.getclock(0))
+
-

参见

+ +

参见

- pwm.setclock()

pwm.setduty()

-

描述

+

描述

设置pin的占空比。

-

语法

+

语法

pwm.setduty(pin, duty)

-

参数

+

参数

pin: 0~11, IO编号

duty: 0~100, pwm的占空比,以百分数表示

-

返回值

-

nil

-

示例

-
    pwm.setduty(0, 50)
-
- -

参见

-

- pwm.getduty()

-

-

pwm.getduty()

-

描述

-

获取pin的pwm占空比。

-

语法

-

pwm.getduty(pin)

-

参数

-

pin: 0~11, IO编号

返回值

nil

示例

-
    -- D0 连接绿色led
-    -- D1 连接蓝色led
-    -- D2 连接红色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) --  led显示红色
-    led(0,0,50) -- led显示蓝色
+
    pwm.setduty(0, 50)
+
-

参见

+

- pwm.getduty()

+

+

pwm.getduty()

+

描述

+

获取pin的pwm占空比。

+

语法

+

pwm.getduty(pin)

+

参数

+

pin: 0~11, IO编号

+

返回值

+

nil

+

示例

+
    -- D0 连接绿色led
+    -- D1 连接蓝色led
+    -- D2 连接红色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) --  led显示红色
+    led(0,0,50) -- led显示蓝色
+
+ + +

参见

- pwm.setduty()

net 模块

-

常量

+

常量

net.TCP, net.UDP

net.createServer()

-

描述

-

创建一个server。

-

语法

-

net.createServer(type, secure)

-

参数

-

type: 取值为:net.TCP 或者 net.UDP

-secure: 设置为true或者false, true代表安全连接,false代表普通连接。

-

返回值

-

net.server子模块

-

示例

-
    net.createServer(net.TCP, true)
-
- -

参见

-

- net.createConnection()

-

-

net.createConnection()

描述

-

创建一个client。

+

创建一个server。

语法

-

net.createConnection(type, secure)

+

net.createServer(type, timeout)

参数

type: 取值为:net.TCP 或者 net.UDP

-secure: 设置为true或者false, true代表安全连接,false代表普通连接。

+timeout: 1~28800, 当为tcp服务器时,客户端的超时时间设置。

返回值

net.server子模块

示例

-
    net.createConnection(net.UDP, false)
-
+
    net.createServer(net.TCP, 30)
+
+

参见

+

- net.createConnection()

+

+

net.createConnection()

+

描述

+

创建一个client。

+

语法

+

net.createConnection(type, secure)

+

参数

+

type: 取值为:net.TCP 或者 net.UDP

+secure: 设置为1或者0, 1代表安全连接,0代表普通连接。

+

返回值

+

net.server子模块

+

示例

+
    net.createConnection(net.UDP, 0)
+
+ + +

参见

- net.createServer()

net.server 子模块

listen()

-

描述

+

描述

侦听指定ip地址的端口。

-

语法

+

语法

net.server.listen(port,[ip],function(net.socket))

-

参数

+

参数

port: 端口号

ip:ip地址字符串,可以省略

function(net.socket): 连接创建成功的回调函数,可以作为参数传给调用函数。

-

返回值

-

nil

-

示例

-
    -- 创建一个server
-    sv=net.createServer(net.TCP, false)
-    -- server侦听端口80,如果收到数据将数据打印至控制台,并向远端发送‘hello world’
-    sv:listen(80,function(c)
-      c:on("receive", function(sck, pl) print(pl) end)
-      c:send("hello world")
-      end)
-
- -

参见

-

- net.createServer()

-

-

close()

-

描述

-

关闭server

-

语法

-

net.server.close()

-

参数

-

nil

返回值

nil

示例

-
    -- 创建server
-    sv=net.createServer(net.TCP, false)
-    -- 关闭server
-    sv:close()
-
+
    -- 创建一个server
+    sv=net.createServer(net.TCP, 30)  -- 30s 超时
+    -- server侦听端口80,如果收到数据将数据打印至控制台,并向远端发送‘hello world’
+    sv:listen(80,function(c)
+      c:on("receive", function(sck, pl) print(pl) end)
+      c:send("hello world")
+      end)
+
+

参见

- net.createServer()

+

+

close()

+

描述

+

关闭server

+

语法

+

net.server.close()

+

参数

+

nil

+

返回值

+

nil

+

示例

+
    -- 创建server
+    sv=net.createServer(net.TCP, 5)
+    -- 关闭server
+    sv:close()
+
+ + +

参见

+

- net.createServer()

net.socket 子模块

connect()

-

描述

+

描述

连接至远端。

-

语法

+

语法

connect(port, ip)

-

参数

+

参数

port: 端口号

ip: ip地址字符串

-

返回值

+

返回值

nil

-

参见

+

参见

- net.socket:on()

send()

-

描述

+

描述

通过连接向远端发送数据。

-

语法

+

语法

send(string, function(sent))

-

参数

+

参数

string: 待发送的字符串

function(sent): 发送字符串后的回调函数。

-

返回值

+

返回值

nil

-

参见

+

参见

- net.socket:on()

on()

-

描述

+

描述

向事件注册回调函数。

-

语法

+

语法

on(event, function cb())

-

参数

+

参数

event: 字符串,取值为: “connection”, “reconnection”, “disconnection”, “receive”, “sent”

function cb(net.socket, [string]): 回调函数。第一个参数是socket.

如果事件是”receive”, 第二个参数则为接收到的字符串。

-

返回值

+

返回值

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, 0)
+    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")
+
-

参见

+ +

参见

- net.createServer()

close()

-

描述

+

描述

关闭socket。

-

语法

+

语法

close()

-

参数

+

参数

nil

-

返回值

+

返回值

nil

-

参见

+

参见

- net.createServer()

dns()

-

描述

+

描述

获取当前域的ip

-

语法

+

语法

dns(domain, function(net.socket, ip))

-

参数

+

参数

domain: 当前域的名称

function (net.socket, ip): 回调函数。第一个参数是socket,第二个参数是当前域的ip字符串。

-

返回值

+

返回值

nil

-

参见

+

参见

- net.createServer()

i2c模块

-

常量

+

常量

i2c.SLOW, i2c.TRANSMITTER, i2c. RECEIVER. FAST(400k)模式目前不支持。

i2c.setup()

-

描述

+

描述

初始化i2c。

-

语法

+

语法

i2c.setup(id, pinSDA, pinSCL, speed)

-

参数

+

参数

id = 0

pinSDA: 0~11, IO编号

pinSCL: 0~11, IO编号

speed: i2c.SLOW

-

返回值

+

返回值

nil

-

参见

+

参见

- i2c.read()

i2c.start()

-

描述

+

描述

启动i2c传输。

-

语法

+

语法

i2c.start(id)

-

参数

+

参数

id = 0

-

返回值

+

返回值

nil

-

参见

+

参见

- i2c.read()

i2c.stop()

-

描述

+

描述

停止i2c传输。

-

语法

+

语法

i2c.stop(id)

-

参数

+

参数

id = 0

-

返回值

+

返回值

nil

-

参见

+

参见

- i2c.read()

i2c.address()

-

描述

+

描述

设置i2c地址以及读写模式。

-

语法

+

语法

i2c.address(id, device_addr, direction)

-

参数

+

参数

id=0

device_addr: 设备地址。

direction: i2c.TRANSMITTER:写模式;i2c. RECEIVER:读模式。

-

返回值

+

返回值

nil

-

参见

+

参见

- i2c.read()

i2c.write()

-

描述

+

描述

向i2c写数据。数据可以是多个数字, 字符串或者lua table。

-

语法

+

语法

i2c.write(id, data1, data2,…)

-

参数

+

参数

id=0

data: 数据可以是多个数字, 字符串或者lua table。

-

返回值

+

返回值

nil

-

示例

-
    i2c.write(0, "hello", "world")
-
+

示例

+
    i2c.write(0, "hello", "world")
+
-

参见

+ +

参见

- i2c.read()

i2c.read()

-

描述

+

描述

读取len个字节的数据。

-

语法

+

语法

i2c.read(id, len)

-

参数

+

参数

id=0

len: 数据长度。

-

返回值

+

返回值

string:接收到的数据。

-

示例

-
    id=0
-    sda=1
-    scl=0
+

示例

+
    id=0
+    sda=1
+    scl=0
 
-    -- 初始化i2c, 将pin1设置为sda, 将pin0设置为scl
-    i2c.setup(id,sda,scl,i2c.SLOW)
+    -- 初始化i2c, 将pin1设置为sda, 将pin0设置为scl
+    i2c.setup(id,sda,scl,i2c.SLOW)
 
-    -- 用户定义函数:读取地址dev_addr的寄存器reg_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
+    -- 用户定义函数:读取地址dev_addr的寄存器reg_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
 
-    -- 读取0x77的寄存器0xAA中的内容。
-    reg = read_reg(0x77, 0xAA)
-    pirnt(string.byte(reg))
+    -- 读取0x77的寄存器0xAA中的内容。
+    reg = read_reg(0x77, 0xAA)
+    pirnt(string.byte(reg))
+
-
-

参见

+

参见

- i2c.write()

adc 模块

-

常量

+

常量

adc.read()

-

描述

-

读取adc的值,esp8266只有一个10bit adc,id为0,最大值1024

-

语法

+

描述

+

读取adc的值,esp8266只有一个10bit adc,id为0,引脚为TOUT,最大值1024

+

语法

adc.read(id)

-

参数

+

参数

id = 0

-

返回值

+

返回值

adc 值 10bit,最大1024.

-

参见

+

参见

-

\ No newline at end of file diff --git a/nodemcu_api_cn.md b/nodemcu_api_cn.md index a184b53b..272a39fd 100644 --- a/nodemcu_api_cn.md +++ b/nodemcu_api_cn.md @@ -1,8 +1,14 @@ # **nodeMcu API说明** # [English Version](https://github.com/funshine/nodemcu-firmware/wiki/nodemcu_api_en) -###版本 0.9.2 build 2014-11-19 +###版本 0.9.2 build 2014-11-20 ###变更日志: +2014-11-20
+修正tmr.delay,支持2s以上的延时,但是长延时可能会引起beacon timer out,导致与AP之间重新连接。
+增加tmr.wdclr(),用来重置看门狗计数器,用在长循环中,以防芯片因看门狗重启。
+修正net模块UDP无法连接问题。
+createServer(net.TCP, timeout)函数增加连接超时参数设置。 + 2014-11-19
增加adc模块,adc.read(0)读取adc的值。
wifi模块增加wifi.sta.getap() 函数,用于获取ap列表。 @@ -1032,6 +1038,31 @@ nil ####参见 **-** [tmr.now()](#tm_now) + +## tmr.wdclr() +####描述 +清除看门狗计数器。
+ +####语法 +tmr.wdclr() + +####参数 +nil. + +####返回值 +nil + +####示例 + +```lua + for i=1,10000 do + print(i) + tmr.wdclr() -- 一个长时间的循环或者事务,需内部调用tmr.wdclr() 清楚看门狗计数器,防止重启。 + end +``` + +####参见 +**-** [tmr.delay()](#tm_delay) #GPIO 模块 ##常量 @@ -1382,11 +1413,11 @@ net.TCP, net.UDP 创建一个server。 ####语法 -net.createServer(type, secure) +net.createServer(type, timeout) ####参数 type: 取值为:net.TCP 或者 net.UDP
-secure: 设置为true或者false, true代表安全连接,false代表普通连接。 +timeout: 1~28800, 当为tcp服务器时,客户端的超时时间设置。 ####返回值 net.server子模块 @@ -1394,7 +1425,7 @@ net.server子模块 ####示例 ```lua - net.createServer(net.TCP, true) + net.createServer(net.TCP, 30) ``` ####参见 @@ -1411,7 +1442,7 @@ net.createConnection(type, secure) ####参数 type: 取值为:net.TCP 或者 net.UDP
-secure: 设置为true或者false, true代表安全连接,false代表普通连接。 +secure: 设置为1或者0, 1代表安全连接,0代表普通连接。 ####返回值 net.server子模块 @@ -1419,7 +1450,7 @@ net.server子模块 ####示例 ```lua - net.createConnection(net.UDP, false) + net.createConnection(net.UDP, 0) ``` ####参见 @@ -1447,7 +1478,7 @@ nil ```lua -- 创建一个server - sv=net.createServer(net.TCP, false) + sv=net.createServer(net.TCP, 30) -- 30s 超时 -- server侦听端口80,如果收到数据将数据打印至控制台,并向远端发送‘hello world’ sv:listen(80,function(c) c:on("receive", function(sck, pl) print(pl) end) @@ -1477,7 +1508,7 @@ nil ```lua -- 创建server - sv=net.createServer(net.TCP, false) + sv=net.createServer(net.TCP, 5) -- 关闭server sv:close() ``` @@ -1544,7 +1575,7 @@ nil ####示例 ```lua - sk=net.createConnection(net.TCP, false) + sk=net.createConnection(net.TCP, 0) 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") @@ -1750,7 +1781,7 @@ string:接收到的数据。 ## adc.read() ####描述 -读取adc的值,esp8266只有一个10bit adc,id为0,最大值1024 +读取adc的值,esp8266只有一个10bit adc,id为0,引脚为TOUT,最大值1024 ####语法 adc.read(id) diff --git a/nodemcu_api_en.html b/nodemcu_api_en.html index e5c9b0c7..2293d147 100644 --- a/nodemcu_api_en.html +++ b/nodemcu_api_en.html @@ -1014,11 +1014,81 @@ body .markdown-body page-break-after: avoid; } } -nodemcu_api_en

nodeMcu API Instruction

+nodemcu_api_en

nodeMcu API Instruction

中文版本

-

version 0.9.2 build 2014-11-19

+

version 0.9.2 build 2014-11-20

change log:

+

2014-11-20

+fix tmr.delay to support more than 2s delay, may cause bacon time out, lost connection to AP.

+add tmr.wdclr to clear watchdog counter in chip, use in long time loop.

+fix UDP part of net module.

+add a timeout para to createServer(net.TCP, timeout).

2014-11-19

add adc module, use adc.read(0) to read adc value, no tests made.

add wifi.sta.getap() api to wifi.sta module, to get ap list.

@@ -1100,8 +1170,9 @@ key is triged only when key is released

Returns

nil

Example

-
    node.restart();
-
+
    node.restart();
+
+

See also

-

@@ -1117,8 +1188,9 @@ key is triged only when key is released

Returns

nil

Example

-
    node.dsleep(us);
-
+
    node.dsleep(us);
+
+

See also

-

@@ -1133,8 +1205,9 @@ key is triged only when key is released

Returns

number:chip ID

Example

-
    id = node.chipid();
-
+
    id = node.chipid();
+
+

See also

-

@@ -1149,8 +1222,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

-

@@ -1167,8 +1241,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

-

@@ -1184,9 +1259,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

-

@@ -1202,9 +1278,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

-

@@ -1220,33 +1297,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(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)
-

See also

-

@@ -1262,9 +1341,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()

@@ -1287,11 +1367,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()

@@ -1307,11 +1388,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()

@@ -1328,11 +1410,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()

@@ -1349,12 +1432,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()

@@ -1371,12 +1455,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()

@@ -1392,13 +1477,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()

@@ -1419,15 +1505,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()

@@ -1443,11 +1530,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()

@@ -1465,8 +1553,9 @@ fail: returns nil

Returns

current mode after setup

Example

-
    wifi.setmode(wifi.STATION)
-
+
    wifi.setmode(wifi.STATION)
+
+

See also

- wifi.getmode()

@@ -1481,8 +1570,9 @@ fail: returns nil

Returns

wifi operation mode

Example

-
    print(wifi.getmode())
-
+
    print(wifi.getmode())
+
+

See also

- wifi.setmode()

@@ -1498,8 +1588,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()

@@ -1514,8 +1605,9 @@ succeed_callback: callback function called after configuration, which is called

Returns

nil

Example

-
    wifi.stopsmart()
-
+
    wifi.stopsmart()
+
+

See also

- wifi.startsmart()

@@ -1532,8 +1624,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()

@@ -1549,8 +1642,9 @@ password: string which is less than 64 bytes.

Returns

nil

Example

-
    wifi.sta.connect()
-
+
    wifi.sta.connect()
+
+

See also

- wifi.sta.disconnect()

@@ -1566,8 +1660,9 @@ password: string which is less than 64 bytes.

Returns

nil

Example

-
    wifi.sta.disconnect()
-
+
    wifi.sta.disconnect()
+
+

See also

- wifi.sta.config()

@@ -1583,8 +1678,9 @@ password: string which is less than 64 bytes.

Returns

nil

Example

-
    wifi.sta.autoconnect()
-
+
    wifi.sta.autoconnect()
+
+

See also

- wifi.sta.config()

@@ -1601,9 +1697,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()

@@ -1618,9 +1715,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()

@@ -1636,14 +1734,15 @@ password: string which is less than 64 bytes.

Returns

nil

Example

-
    -- print ap list
-    function listap(t)
-      for k,v in pairs(t) do
-        print(k.." : "..v)
-      end
-    end
-    wifi.sta.getap(listap)
-
+
    -- print ap list
+    function listap(t)
+      for k,v in pairs(t) do
+        print(k.." : "..v)
+      end
+    end
+    wifi.sta.getap(listap)
+
+

See also

- wifi.sta.getip()

@@ -1657,17 +1756,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

-

@@ -1682,8 +1783,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

- wifi.ap.getmac()

@@ -1698,8 +1800,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

- wifi.ap.getip()

@@ -1715,9 +1818,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()

@@ -1732,9 +1836,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()

@@ -1751,9 +1856,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()

@@ -1769,537 +1875,573 @@ 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()

+

+

tmr.wdclr()

+

Description

+

clear system watchdog counter.

+

Syntax

+

tmr.wdclr()

+

Parameters

+

nil.

+

Returns

+

nil

+

Example

+
    for i=1,10000 do 
+      print(i)
+      tmr.wdclr()   -- should call tmr.wdclr() in a long loop to avoid hardware reset caused by watchdog.
+    end 
+
+ + +

See also

+

- tmr.delay()

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()

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.mode()

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.mode()

- gpio.read()

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

- gpio.mode()

- gpio.write()

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.start()

-

-

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.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.stop()

+

- pwm.start()

+

+

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.start()

-

-

pwm.setclock()

+

- pwm.stop()

+

+

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.start()

+

+

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()

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.setclock()

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()

-

-

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()

+

+

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

- pwm.setduty()

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()

-

-

net.createConnection()

Description

-

create a client.

+

create a server.

Syntax

-

net.createConnection(type, secure)

+

net.createServer(type, timeout)

Parameters

type: net.TCP or net.UDP

-secure: true or false, true for safe link, false for ordinary link

+timeout: for a TCP server, timeout is 1~28800 seconds, for a inactive client to disconnected.

Returns

net.server sub module

Example

-
    net.createConnection(net.UDP, false)
-
+
    net.createServer(net.TCP, 30) -- 30s timeout
+
+

See also

+

- net.createConnection()

+

+

net.createConnection()

+

Description

+

create a client.

+

Syntax

+

net.createConnection(type, secure)

+

Parameters

+

type: net.TCP or net.UDP

+secure: 1 or 0, 1 for ssl link, 0 for normal link

+

Returns

+

net.server sub module

+

Example

+
    net.createConnection(net.UDP, 0)
+
+ + +

See also

- net.createServer()

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

-

- net.createServer()

-

-

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, 30)    -- 30s time out for a inactive client
+    -- 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()

+

+

close()

+

Description

+

close server.

+

Syntax

+

net.server.close()

+

Parameters

+

nil

+

Returns

+

nil

+

Example

+
    -- create a server
+    sv=net.createServer(net.TCP, 30)
+    -- close server
+    sv:close()
+
+ + +

See also

+

- net.createServer()

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

-

- net.socket:on()

-

-

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

- net.socket:on()

+

+

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

+

- net.socket:on()

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, 0)
+    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

- net.createServer()

close()

-

Description

-

close socket.

-

Syntax

-

close()

-

Parameters

-

nil

-

Returns

-

nil

-

See also

-

- net.createServer()

-

-

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

- net.createServer()

+

+

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

+

- net.createServer()

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.read()

-

-

i2c.start()

-

Description

-

start i2c transporting.

-

Syntax

-

i2c.start(id)

-

Parameters

-

id = 0

Returns

nil

See also

- i2c.read()

-

-

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.read()

-

-

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.read()

-

-

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.

+device_addr: device address.

+direction: i2c.TRANSMITTER for writing mode , i2c. RECEIVER for reading mode

Returns

nil

-

Example

-
    i2c.write(0, "hello", "world")
-
-

See also

- i2c.read()

+

+

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()

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))
+
-
-

See also

+

See also

- i2c.write()

adc module

CONSTANT

none

adc.read()

-

Description

-

read adc value of id, esp8266 has only one 10bit adc, id=0

-

Syntax

+

Description

+

read adc value of id, esp8266 has only one 10bit adc, id=0, pin TOUT

+

Syntax

adc.read(id)

-

Parameters

+

Parameters

id = 0

-

Returns

+

Returns

adc value

-

See also

+

See also

-

\ No newline at end of file diff --git a/nodemcu_api_en.md b/nodemcu_api_en.md index 37fb253b..7837e675 100644 --- a/nodemcu_api_en.md +++ b/nodemcu_api_en.md @@ -1,8 +1,14 @@ # **nodeMcu API Instruction** # [中文版本](https://github.com/funshine/nodemcu-firmware/wiki/nodemcu_api_cn) -###version 0.9.2 build 2014-11-19 +###version 0.9.2 build 2014-11-20 ###change log: +2014-11-20
+fix tmr.delay to support more than 2s delay, may cause bacon time out, lost connection to AP.
+add tmr.wdclr to clear watchdog counter in chip, use in long time loop.
+fix UDP part of net module.
+add a timeout para to createServer(net.TCP, timeout). + 2014-11-19
add adc module, use adc.read(0) to read adc value, no tests made.
add wifi.sta.getap() api to wifi.sta module, to get ap list. @@ -1051,6 +1057,32 @@ nil **-** [tmr.now()](#tm_now) + +## tmr.wdclr() +####Description +clear system watchdog counter.
+ +####Syntax +tmr.wdclr() + +####Parameters +nil. + +####Returns +nil + +####Example + +```lua + for i=1,10000 do + print(i) + tmr.wdclr() -- should call tmr.wdclr() in a long loop to avoid hardware reset caused by watchdog. + end +``` + +####See also +**-** [tmr.delay()](#tm_delay) + #GPIO module ##CONSTANT gpio.OUTPUT, gpio.INPUT, gpio.INT, gpio.HIGH, gpio.LOW @@ -1401,11 +1433,11 @@ net.TCP, net.UDP create a server. ####Syntax -net.createServer(type, secure) +net.createServer(type, timeout) ####Parameters type: net.TCP or net.UDP
-secure: true or false, true for safe link, false for ordinary link +timeout: for a TCP server, timeout is 1~28800 seconds, for a inactive client to disconnected. ####Returns net.server sub module @@ -1413,7 +1445,7 @@ net.server sub module ####Example ```lua - net.createServer(net.TCP, true) + net.createServer(net.TCP, 30) -- 30s timeout ``` ####See also @@ -1430,7 +1462,7 @@ net.createConnection(type, secure) ####Parameters type: net.TCP or net.UDP
-secure: true or false, true for safe link, false for ordinary link +secure: 1 or 0, 1 for ssl link, 0 for normal link ####Returns net.server sub module @@ -1438,7 +1470,7 @@ net.server sub module ####Example ```lua - net.createConnection(net.UDP, false) + net.createConnection(net.UDP, 0) ``` ####See also @@ -1466,7 +1498,7 @@ nil ```lua -- create a server - sv=net.createServer(net.TCP, false) + sv=net.createServer(net.TCP, 30) -- 30s time out for a inactive client -- 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) @@ -1496,7 +1528,7 @@ nil ```lua -- create a server - sv=net.createServer(net.TCP, false) + sv=net.createServer(net.TCP, 30) -- close server sv:close() ``` @@ -1563,7 +1595,7 @@ nil ####Example ```lua - sk=net.createConnection(net.TCP, false) + sk=net.createConnection(net.TCP, 0) 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") @@ -1769,7 +1801,7 @@ none ## adc.read() ####Description -read adc value of id, esp8266 has only one 10bit adc, id=0 +read adc value of id, esp8266 has only one 10bit adc, id=0, pin TOUT ####Syntax adc.read(id)