From b67b552e393cc8e92e6d2403887df9b604037847 Mon Sep 17 00:00:00 2001 From: funshine Date: Tue, 18 Nov 2014 19:58:48 +0800 Subject: [PATCH] add a example to doc --- API.html | 579 +++++++++++++++++++++++------------------------------- API.md | 129 +++++++----- README.md | 22 +++ 3 files changed, 339 insertions(+), 391 deletions(-) diff --git a/API.html b/API.html index b9d4b5b9..72fc3975 100644 --- a/API.html +++ b/API.html @@ -1014,72 +1014,7 @@ body .markdown-body page-break-after: avoid; } } -API

nodeMcu API Instruction

+API

nodeMcu API Instruction

version 0.9.2 build 2014-11-18

change log:

@@ -1161,9 +1096,8 @@ key is triged only when key is released

Returns

nil

Example

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

See also

-

@@ -1179,9 +1113,8 @@ key is triged only when key is released

Returns

nil

Example

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

See also

-

@@ -1196,9 +1129,8 @@ key is triged only when key is released

Returns

number:chip ID

Example

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

See also

-

@@ -1213,9 +1145,8 @@ 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

-

@@ -1232,9 +1163,8 @@ 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

-

@@ -1250,10 +1180,9 @@ 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

-

@@ -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

- file.remove()

@@ -1523,9 +1461,8 @@ fail: returns nil

Returns

current mode after setup

Example

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

See also

- wifi.getmode()

@@ -1540,9 +1477,8 @@ fail: returns nil

Returns

wifi operation mode

Example

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

See also

- wifi.setmode()

@@ -1558,9 +1494,8 @@ 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()

@@ -1575,9 +1510,8 @@ succeed_callback: callback function called after configuration, which is called

Returns

nil

Example

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

See also

- wifi.startsmart()

@@ -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

- wifi.sta.getmac()

@@ -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

- wifi.sta.getip()

@@ -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

- wifi.ap.getmac()

@@ -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

- wifi.ap.getip()

@@ -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

- tmr.now()

@@ -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

- tmr.delay()

@@ -1802,10 +1724,9 @@ 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()

@@ -1821,15 +1742,14 @@ 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
-
-    -- stop alarm
-    tmr.stop()
-
+ -- something else + -- stop alarm + tmr.stop() +

See also

- tmr.now()

@@ -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

- gpio.read()

@@ -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

- gpio.mode()

@@ -1885,12 +1804,11 @@ level: gpio.HIGH or gpio.LOW

Returns

nil

Example

-
    -- set pin index 1 to GPIO mode, and set the pin to high.
-    pin=1
-    gpio.mode(pin, gpio.OUTPUT)
-    gpio.write(pin, gpio.HIGH)
-
- +
    -- set pin index 1 to GPIO mode, and set the pin to high.
+    pin=1
+    gpio.mode(pin, gpio.OUTPUT)
+    gpio.write(pin, gpio.HIGH)
+

See also

- gpio.mode()

@@ -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

- pwm.start()

@@ -1957,9 +1874,8 @@ duty: 0~100, pwm duty cycle in percentage

Returns

nil

Example

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

See also

- pwm.start()

@@ -1974,9 +1890,8 @@ duty: 0~100, pwm duty cycle in percentage

Returns

nil

Example

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

See also

- pwm.stop()

@@ -1991,9 +1906,8 @@ duty: 0~100, pwm duty cycle in percentage

Returns

nil

Example

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

See also

- pwm.start()

@@ -2010,9 +1924,8 @@ clock: 1~500, pwm frequency.

Returns

nil

Example

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

See also

- pwm.getclock()

@@ -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

- pwm.setclock()

@@ -2045,9 +1957,8 @@ duty: 0~100, pwm duty cycle in percentage

Returns

nil

Example

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

See also

- pwm.getduty()

@@ -2062,24 +1973,24 @@ duty: 0~100, pwm duty cycle in percentage

Returns

nil

Example

-
    -- D0 is connected to green led
-    -- D1 is connected to blue led
-    -- D2 is connected to red led
-    pwm.setup(0,500,50)
-    pwm.setup(1,500,50)
-    pwm.setup(2,500,50)
-    pwm.start(0)
-    pwm.start(1)
-    pwm.start(2)
-    function led(r,g,b)
-      pwm.setduty(0,g)
-      pwm.setduty(1,b)
-      pwm.setduty(2,r)
-    end
-    led(50,0,0) --  set led to red
-    led(0,0,50) -- set led to blue.
-
+
    -- D0 is connected to green led
+    -- D1 is connected to blue led
+    -- D2 is connected to red led
+    pwm.setup(0,500,50)
+    pwm.setup(1,500,50)
+    pwm.setup(2,500,50)
+    pwm.start(0)
+    pwm.start(1)
+    pwm.start(2)
+    function led(r,g,b)
+      pwm.setduty(0,g)
+      pwm.setduty(1,b)
+      pwm.setduty(2,r)
+    end
+    led(50,0,0) --  set led to red
+    led(0,0,50) -- set led to blue.
 
+

See also

- pwm.setduty()

@@ -2098,9 +2009,8 @@ secure: true or false, true for safe link, false for ordinary link

Returns

net.server sub module

Example

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

See also

- net.createConnection()

@@ -2116,9 +2026,8 @@ secure: true or false, true for safe link, false for ordinary link

Returns

net.server sub module

Example

-
    net.createConnection(net.UDP, false)
-
- +
    net.createConnection(net.UDP, false)
+

See also

- net.createServer()

@@ -2136,15 +2045,14 @@ function(net.socket): callback function, pass to Caller function as param if a c

Returns

nil

Example

-
    -- create a server
-    sv=net.createServer(net.TCP, false)
-    -- server listen on 80, if data received, print data to console, and send "hello world" to remote.
-    sv:listen(80,function(c)
-      c:on("receive", function(sck, pl) print(pl) end)
-      c:send("hello world")
-      end)
-
- +
    -- create a server
+    sv=net.createServer(net.TCP, false)
+    -- server listen on 80, if data received, print data to console, and send "hello world" to remote.
+    sv:listen(80,function(c)
+      c:on("receive", function(sck, pl) print(pl) end)
+      c:send("hello world")
+      end)
+

See also

- net.createServer()

@@ -2159,12 +2067,11 @@ function(net.socket): callback function, pass to Caller function as param if a c

Returns

nil

Example

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

See also

- net.createServer()

@@ -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

- net.createServer()

@@ -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

- i2c.read()

@@ -2328,31 +2233,31 @@ len: data length

Returns

string:data received.

Example

-
    id=0
-    sda=1
-    scl=0
+
    id=0
+    sda=1
+    scl=0
 
-    -- initialize i2c, set pin1 as sda, set pin0 as scl
-    i2c.setup(id,sda,scl,i2c.SLOW)
+    -- initialize i2c, set pin1 as sda, set pin0 as scl
+    i2c.setup(id,sda,scl,i2c.SLOW)
 
-    -- user defined function: read from reg_addr content of dev_addr
-    function read_reg(dev_addr, reg_addr)
-      i2c.start(id)
-      i2c.address(id, dev_addr ,i2c.TRANSMITTER)
-      i2c.write(id,reg_addr)
-      i2c.stop(id)
-      i2c.start(id)
-      i2c.address(id, dev_addr,i2c.RECEIVER)
-      c=i2c.read(id,1)
-      i2c.stop(id)
-      return c
-    end
+    -- user defined function: read from reg_addr content of dev_addr
+    function read_reg(dev_addr, reg_addr)
+      i2c.start(id)
+      i2c.address(id, dev_addr ,i2c.TRANSMITTER)
+      i2c.write(id,reg_addr)
+      i2c.stop(id)
+      i2c.start(id)
+      i2c.address(id, dev_addr,i2c.RECEIVER)
+      c=i2c.read(id,1)
+      i2c.stop(id)
+      return c
+    end
 
-    -- get content of register 0xAA of device 0x77
-    reg = read_reg(0x77, 0xAA)
-    pirnt(string.byte(reg))
-
+ -- get content of register 0xAA of device 0x77 + reg = read_reg(0x77, 0xAA) + pirnt(string.byte(reg)) +

See also

- i2c.write()

\ No newline at end of file diff --git a/API.md b/API.md index 5edf7c76..93a90116 100644 --- a/API.md +++ b/API.md @@ -95,7 +95,7 @@ nil ####Example -``` +```lua node.restart(); ``` @@ -121,7 +121,7 @@ nil ####Example -``` +```lua node.dsleep(us); ``` @@ -144,7 +144,7 @@ number:chip ID ####Example -``` +```lua id = node.chipid(); ``` @@ -167,7 +167,7 @@ number: system heap size left in bytes ####Example -``` +```lua heap_size = node.heap(); ``` @@ -191,7 +191,7 @@ Default function: long: change LED blinking rate, short: reset chip nil ####Example -``` +```lua node.key("long", function(){print('hello world')}) ``` @@ -215,7 +215,7 @@ nil ####Example -``` +```lua -- turn led on forever. node.led(0); ``` @@ -240,7 +240,7 @@ nil ####Example -``` +```lua -- never use node.input() in console. no effect. sk:on("receive", function(conn, payload) node.input(payload) end) ``` @@ -265,7 +265,7 @@ nil ####Example -``` +```lua function tonet(str) sk:send(str) -- print(str) WRONG!!! never ever print something in this function @@ -274,6 +274,27 @@ nil node.ouput(tonet, 1) -- serial also get the lua output. ``` +```lua + -- 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 **-** []() @@ -294,7 +315,7 @@ nil ####Example -``` +```lua -- remove "foo.lua" from file system. file.remove("foo.lua") ``` @@ -326,7 +347,7 @@ nil ####Example -``` +```lua -- open 'init.lua', print the first line. file.open("init.lua", "r") print(file.readline()) @@ -353,7 +374,7 @@ nil ####Example -``` +```lua -- open 'init.lua', print the first line. file.open("init.lua", "r") print(file.readline()) @@ -381,7 +402,7 @@ return nil when EOF. ####Example -``` +```lua -- print the first line of 'init.lua' file.open("init.lua", "r") print(file.readline()) @@ -409,7 +430,7 @@ nil: there is error ####Example -``` +```lua -- open 'init.lua' in 'a+' mode file.open("init.lua", "a+") -- write 'foo bar' to the end of the file @@ -438,7 +459,7 @@ nil: there is error ####Example -``` +```lua -- open 'init.lua' in 'a+' mode file.open("init.lua", "a+") -- write 'foo bar' to the end of the file @@ -466,7 +487,7 @@ nil ####Example -``` +```lua -- open 'init.lua' in 'a+' mode file.open("init.lua", "a+") -- write 'foo bar' to the end of the file @@ -500,7 +521,7 @@ fail: returns nil ####Example -``` +```lua -- open 'init.lua' in 'a+' mode file.open("init.lua", "a+") -- write 'foo bar' to the end of the file @@ -531,7 +552,7 @@ a lua table which contains the {file name: file size} pairs ####Example -``` +```lua l = file.list(); for k,v in l do print("name:"..k..", size:"..v) @@ -561,7 +582,7 @@ current mode after setup ####Example -``` +```lua wifi.setmode(wifi.STATION) ``` @@ -585,7 +606,7 @@ wifi operation mode ####Example -``` +```lua print(wifi.getmode()) ``` @@ -611,7 +632,7 @@ nil ####Example -``` +```lua wifi.startsmart(6, cb()) ``` @@ -635,7 +656,7 @@ nil ####Example -``` +```lua wifi.stopsmart() ``` @@ -663,7 +684,7 @@ nil ####Example -``` +```lua wifi.sta.config("myssid","mypassword") ``` @@ -689,7 +710,7 @@ nil ####Example -``` +```lua wifi.sta.connect() ``` @@ -715,7 +736,7 @@ nil ####Example -``` +```lua wifi.sta.disconnect() ``` @@ -741,7 +762,7 @@ nil ####Example -``` +```lua wifi.sta.autoconnect() ``` @@ -768,7 +789,7 @@ ip address in string, for example:"192.168.0.111" ####Example -``` +```lua -- print current ip print(wifi.sta.getip()) ``` @@ -794,7 +815,7 @@ mac address in string, for example:"18-33-44-FE-55-BB" ####Example -``` +```lua -- print current mac address print(wifi.sta.getmac()) ``` @@ -818,7 +839,7 @@ cfg: lua table to setup ap. ####Example: -``` +```lua cfg={} cfg.ssid="myssid" cfg.pwd="mypwd" @@ -830,7 +851,7 @@ nil ####Example -``` +```lua wifi.ap.config(ssid, 'password') ``` @@ -853,7 +874,7 @@ ip address in string, for example:"192.168.0.111" ####Example -``` +```lua wifi.ap.getip() ``` @@ -877,7 +898,7 @@ mac address in string, for example:"1A-33-44-FE-55-BB" ####Example -``` +```lua wifi.ap.getmac() ``` @@ -902,7 +923,7 @@ nil ####Example -``` +```lua -- delay 100us tmr.delay(100) ``` @@ -927,7 +948,7 @@ uint32: value of counter ####Example -``` +```lua -- print current value of counter print(tmr.now()) ``` @@ -954,7 +975,7 @@ nil ####Example -``` +```lua -- print "hello world" every 1000ms tmr.alarm(1000, 1, function() print("hello world") end ) ``` @@ -981,7 +1002,7 @@ nil ####Example -``` +```lua -- print "hello world" every 1000ms tmr.alarm(1000, 1, function() print("hello world") end ) @@ -1017,7 +1038,7 @@ nil ####Example -``` +```lua -- set gpio 0 as output. gpio.mode(0, gpio.OUTPUT) @@ -1043,7 +1064,7 @@ number:0 - low, 1 - high ####Example -``` +```lua -- read value of gpio 0. gpio.read(0) ``` @@ -1069,7 +1090,7 @@ nil ####Example -``` +```lua -- set pin index 1 to GPIO mode, and set the pin to high. pin=1 gpio.mode(pin, gpio.OUTPUT) @@ -1099,7 +1120,7 @@ nil ####Example -``` +```lua -- use pin 0 as the input pulse width counter pulse0 = 0 du = 0 @@ -1138,7 +1159,7 @@ nil ####Example -``` +```lua -- set pin index 0 as pwm output, frequency is 100Hz, duty cycle is 50-50. pwm.setup(0, 100, 50) ``` @@ -1163,7 +1184,7 @@ nil ####Example -``` +```lua pwm.close(0) ``` @@ -1187,7 +1208,7 @@ nil ####Example -``` +```lua pwm.start(0) ``` @@ -1211,7 +1232,7 @@ nil ####Example -``` +```lua pwm.stop(0) ``` @@ -1238,7 +1259,7 @@ nil ####Example -``` +```lua pwm.setclock(0, 100) ``` @@ -1262,7 +1283,7 @@ number:pwm frequency of pin ####Example -``` +```lua print(pwm.getclock(0)) ``` @@ -1287,7 +1308,7 @@ nil ####Example -``` +```lua pwm.setduty(0, 50) ``` @@ -1311,7 +1332,7 @@ nil ####Example -``` +```lua -- D0 is connected to green led -- D1 is connected to blue led -- D2 is connected to red led @@ -1356,7 +1377,7 @@ net.server sub module ####Example -``` +```lua net.createServer(net.TCP, true) ``` @@ -1381,7 +1402,7 @@ net.server sub module ####Example -``` +```lua net.createConnection(net.UDP, false) ``` @@ -1408,7 +1429,7 @@ nil ####Example -``` +```lua -- 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. @@ -1438,7 +1459,7 @@ nil ####Example -``` +```lua -- create a server sv=net.createServer(net.TCP, false) -- close server @@ -1506,7 +1527,7 @@ nil ####Example -``` +```lua sk=net.createConnection(net.TCP, false) sk:on("receive", function(sck, c) print(c) end ) sk:connect(80,"192.168.0.66") @@ -1651,7 +1672,7 @@ nil ####Example -``` +```lua i2c.write(0, "hello", "world") ``` @@ -1676,7 +1697,7 @@ string:data received. ####Example -``` +```lua id=0 sda=1 scl=0 diff --git a/README.md b/README.md index f5f15c25..cea207c2 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,28 @@ braudrate:9600 file.close() node.restart() -- this will restart the module. ``` + +####With below code, you can telnet to your esp8266 now +```lua + -- 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
[nodemcu wiki](https://github.com/funshine/nodemcu-firmware/wiki/nodeMcu:-lua-based-interactive-firmware-for-mcu)