README: Tidy the sample code

Signed-off-by: Nick Andrew <nick@nick-andrew.net>
This commit is contained in:
Nick Andrew 2015-11-17 01:04:44 +11:00
parent c1fd5d4ed2
commit 7b1a371192
1 changed files with 44 additions and 44 deletions

View File

@ -81,7 +81,7 @@ Because Lua is a high level language and several modules are built into the firm
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,7 +95,7 @@ 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)
@ -109,19 +109,19 @@ m:on("message", function(conn, topic, 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
``` ```
@ -177,7 +177,7 @@ 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()
@ -197,12 +197,12 @@ cu:send("hello")
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)
``` ```