README: Tidy the sample code
Signed-off-by: Nick Andrew <nick@nick-andrew.net>
This commit is contained in:
parent
c1fd5d4ed2
commit
7b1a371192
88
README.md
88
README.md
|
@ -47,7 +47,7 @@ Because Lua is a high level language and several modules are built into the firm
|
||||||
print(ip)
|
print(ip)
|
||||||
--nil
|
--nil
|
||||||
wifi.setmode(wifi.STATION)
|
wifi.setmode(wifi.STATION)
|
||||||
wifi.sta.config("SSID","password")
|
wifi.sta.config("SSID", "password")
|
||||||
ip = wifi.sta.getip()
|
ip = wifi.sta.getip()
|
||||||
print(ip)
|
print(ip)
|
||||||
--192.168.18.110
|
--192.168.18.110
|
||||||
|
@ -57,8 +57,8 @@ Because Lua is a high level language and several modules are built into the firm
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
pin = 1
|
pin = 1
|
||||||
gpio.mode(pin,gpio.OUTPUT)
|
gpio.mode(pin, gpio.OUTPUT)
|
||||||
gpio.write(pin,gpio.HIGH)
|
gpio.write(pin, gpio.HIGH)
|
||||||
print(gpio.read(pin))
|
print(gpio.read(pin))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -67,10 +67,10 @@ Because Lua is a high level language and several modules are built into the firm
|
||||||
```lua
|
```lua
|
||||||
-- A simple http client
|
-- A simple http client
|
||||||
conn=net.createConnection(net.TCP, 0)
|
conn=net.createConnection(net.TCP, 0)
|
||||||
conn:on("receive", function(conn, payload) print(payload) end )
|
conn:on("receive", function(conn, payload) print(payload) end)
|
||||||
conn:connect(80,"115.239.210.27")
|
conn:connect(80, "115.239.210.27")
|
||||||
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
|
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
|
||||||
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
|
.. "Connection: keep-alive\r\nAccept: */*\r\n\r\n")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Or a simple HTTP server
|
## Or a simple HTTP server
|
||||||
|
@ -78,12 +78,12 @@ Because Lua is a high level language and several modules are built into the firm
|
||||||
```lua
|
```lua
|
||||||
-- A simple http server
|
-- A simple http server
|
||||||
srv=net.createServer(net.TCP)
|
srv=net.createServer(net.TCP)
|
||||||
srv:listen(80,function(conn)
|
srv:listen(80, function(conn)
|
||||||
conn:on("receive",function(conn,payload)
|
conn:on("receive", function(conn,payload)
|
||||||
print(payload)
|
print(payload)
|
||||||
conn:send("<h1> Hello, NodeMcu.</h1>")
|
conn:send("<h1> Hello, NodeMCU.</h1>")
|
||||||
end)
|
end)
|
||||||
conn:on("sent",function(conn) conn:close() end)
|
conn:on("sent", function(conn) conn:close() end)
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -95,33 +95,33 @@ m = mqtt.Client("clientid", 120, "user", "password")
|
||||||
|
|
||||||
-- setup Last Will and Testament (optional)
|
-- setup Last Will and Testament (optional)
|
||||||
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
|
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
|
||||||
-- to topic "/lwt" if client don't send keepalive packet
|
-- to topic "/lwt" if client doesn't send keepalive packet
|
||||||
m:lwt("/lwt", "offline", 0, 0)
|
m:lwt("/lwt", "offline", 0, 0)
|
||||||
|
|
||||||
m:on("connect", function(con) print ("connected") end)
|
m:on("connect", function(con) print("connected") end)
|
||||||
m:on("offline", function(con) print ("offline") end)
|
m:on("offline", function(con) print("offline") end)
|
||||||
|
|
||||||
-- on publish message receive event
|
-- on publish message receive event
|
||||||
m:on("message", function(conn, topic, data)
|
m:on("message", function(conn, topic, data)
|
||||||
print(topic .. ":" )
|
print(topic .. ":")
|
||||||
if data ~= nil then
|
if data ~= nil then
|
||||||
print(data)
|
print(data)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- m:connect( host, port, secure, auto_reconnect, function(client) )
|
-- m:connect(host, port, secure, auto_reconnect, function(client) end)
|
||||||
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
|
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
|
||||||
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
|
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
|
||||||
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
|
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
|
||||||
|
|
||||||
-- subscribe topic with qos = 0
|
-- subscribe to topic with qos = 0
|
||||||
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
|
m:subscribe("/topic", 0, function(conn) print("subscribe success") end)
|
||||||
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
|
-- or subscribe multiple topics (topic/0, qos = 0; topic/1, qos = 1; topic2, qos = 2)
|
||||||
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
|
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
|
||||||
-- publish a message with data = hello, QoS = 0, retain = 0
|
-- publish a message with data = hello, QoS = 0, retain = 0
|
||||||
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
|
m:publish("/topic", "hello", 0, 0, function(conn) print("sent") end)
|
||||||
|
|
||||||
m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disconnect from host.
|
m:close(); -- if auto-reconnect == 1, it will disable auto-reconnect and then disconnect from host.
|
||||||
-- you can call m:connect again
|
-- you can call m:connect again
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -131,45 +131,45 @@ m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disc
|
||||||
```lua
|
```lua
|
||||||
-- a udp server
|
-- a udp server
|
||||||
s=net.createServer(net.UDP)
|
s=net.createServer(net.UDP)
|
||||||
s:on("receive",function(s,c) print(c) end)
|
s:on("receive", function(s, c) print(c) end)
|
||||||
s:listen(5683)
|
s:listen(5683)
|
||||||
|
|
||||||
-- a udp client
|
-- a udp client
|
||||||
cu=net.createConnection(net.UDP)
|
cu=net.createConnection(net.UDP)
|
||||||
cu:on("receive",function(cu,c) print(c) end)
|
cu:on("receive", function(cu, c) print(c) end)
|
||||||
cu:connect(5683,"192.168.18.101")
|
cu:connect(5683, "192.168.18.101")
|
||||||
cu:send("hello")
|
cu:send("hello")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Do something shiny with an RGB LED
|
## Do something shiny with an RGB LED
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
function led(r,g,b)
|
function led(r, g, b)
|
||||||
pwm.setduty(1,r)
|
pwm.setduty(1, r)
|
||||||
pwm.setduty(2,g)
|
pwm.setduty(2, g)
|
||||||
pwm.setduty(3,b)
|
pwm.setduty(3, b)
|
||||||
end
|
end
|
||||||
pwm.setup(1,500,512)
|
pwm.setup(1, 500, 512)
|
||||||
pwm.setup(2,500,512)
|
pwm.setup(2, 500, 512)
|
||||||
pwm.setup(3,500,512)
|
pwm.setup(3, 500, 512)
|
||||||
pwm.start(1)
|
pwm.start(1)
|
||||||
pwm.start(2)
|
pwm.start(2)
|
||||||
pwm.start(3)
|
pwm.start(3)
|
||||||
led(512,0,0) -- red
|
led(512, 0, 0) -- red
|
||||||
led(0,0,512) -- blue
|
led(0, 0, 512) -- blue
|
||||||
```
|
```
|
||||||
|
|
||||||
## And blink it
|
## And blink it
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
lighton=0
|
lighton=0
|
||||||
tmr.alarm(1,1000,1,function()
|
tmr.alarm(1, 1000, 1, function()
|
||||||
if lighton==0 then
|
if lighton==0 then
|
||||||
lighton=1
|
lighton=1
|
||||||
led(512,512,512)
|
led(512, 512, 512)
|
||||||
else
|
else
|
||||||
lighton=0
|
lighton=0
|
||||||
led(0,0,0)
|
led(0, 0, 0)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
@ -177,8 +177,8 @@ cu:send("hello")
|
||||||
## If you want to run something when the system boots
|
## If you want to run something when the system boots
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
--init.lua will be excuted
|
--init.lua will be executed
|
||||||
file.open("init.lua","w")
|
file.open("init.lua", "w")
|
||||||
file.writeline([[print("Hello, do this at the beginning.")]])
|
file.writeline([[print("Hello, do this at the beginning.")]])
|
||||||
file.close()
|
file.close()
|
||||||
node.restart() -- this will restart the module.
|
node.restart() -- this will restart the module.
|
||||||
|
@ -188,21 +188,21 @@ cu:send("hello")
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- a simple telnet server
|
-- a simple telnet server
|
||||||
s=net.createServer(net.TCP,180)
|
s=net.createServer(net.TCP, 180)
|
||||||
s:listen(2323,function(c)
|
s:listen(2323, function(c)
|
||||||
function s_output(str)
|
function s_output(str)
|
||||||
if(c~=nil)
|
if(c~=nil)
|
||||||
then c:send(str)
|
then c:send(str)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
node.output(s_output, 0) -- re-direct output to function s_ouput.
|
node.output(s_output, 0) -- re-direct output to function s_ouput.
|
||||||
c:on("receive",function(c,l)
|
c:on("receive", function(c, l)
|
||||||
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
|
node.input(l) -- works like pcall(loadstring(l)) but support multiples separate lines
|
||||||
end)
|
end)
|
||||||
c:on("disconnection",function(c)
|
c:on("disconnection", function(c)
|
||||||
node.output(nil) -- un-regist the redirect output function, output goes to serial
|
node.output(nil) -- un-register the redirect output function, output goes to serial
|
||||||
end)
|
end)
|
||||||
print("Welcome to NodeMcu world.")
|
print("Welcome to NodeMCU world.")
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue