Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
450b79db
Commit
450b79db
authored
Apr 06, 2015
by
funshine
Browse files
add more mqtt example, add a mqtt subfolder
parent
74a70790
Changes
3
Hide whitespace changes
Inline
Side-by-side
examples/fragment.lua
View file @
450b79db
...
@@ -515,3 +515,7 @@ end)
...
@@ -515,3 +515,7 @@ end)
m
:
on
(
"message"
,
dispatch
)
m
:
on
(
"message"
,
dispatch
)
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
tmr
.
alarm
(
0
,
10000
,
1
,
function
()
local
pl
=
"time: "
..
tmr
.
time
()
m
:
publish
(
"/topic1"
,
pl
,
0
,
0
)
end
)
lua_examples/mqtt
_test
.lua
→
lua_examples/mqtt
/mqtt2cloud
.lua
View file @
450b79db
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
-- test with cloudmqtt.com
-- test with cloudmqtt.com
m_dis
=
{}
m_dis
=
{}
function
dispatch
(
m
,
t
,
pl
)
function
dispatch
(
m
,
t
,
pl
)
if
pl
~=
nil
and
m_dis
[
t
]
then
if
pl
~=
nil
and
m_dis
[
t
]
then
m_dis
[
t
](
pl
)
m_dis
[
t
](
m
,
pl
)
end
end
end
end
function
topic1func
(
pl
)
function
topic1func
(
m
,
pl
)
print
(
"get1: "
..
pl
)
print
(
"get1: "
..
pl
)
end
end
function
topic2func
(
pl
)
function
topic2func
(
m
,
pl
)
print
(
"get2: "
..
pl
)
print
(
"get2: "
..
pl
)
end
end
m_dis
[
"/topic1"
]
=
topic1func
m_dis
[
"/topic1"
]
=
topic1func
m_dis
[
"/topic2"
]
=
topic2func
m_dis
[
"/topic2"
]
=
topic2func
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
m
=
mqtt
.
Client
(
"nodemcu1"
,
60
,
"test"
,
"test123"
)
m
=
mqtt
.
Client
(
"nodemcu1"
,
60
,
"test"
,
"test123"
)
m
:
on
(
"connect"
,
function
(
m
)
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
print
(
"connection "
..
node
.
heap
())
...
@@ -26,5 +26,8 @@ m:on("offline", function(conn)
...
@@ -26,5 +26,8 @@ m:on("offline", function(conn)
print
(
node
.
heap
())
print
(
node
.
heap
())
end
)
end
)
m
:
on
(
"message"
,
dispatch
)
m
:
on
(
"message"
,
dispatch
)
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
tmr
.
alarm
(
0
,
10000
,
1
,
function
()
local
pl
=
"time: "
..
tmr
.
time
()
m
:
publish
(
"/topic1"
,
pl
,
0
,
0
)
end
)
lua_examples/mqtt/mqtt_file.lua
0 → 100644
View file @
450b79db
-- test transfer files over mqtt.
m_dis
=
{}
function
dispatch
(
m
,
t
,
pl
)
if
pl
~=
nil
and
m_dis
[
t
]
then
m_dis
[
t
](
m
,
pl
)
end
end
function
pubfile
(
m
,
filename
)
file
.
close
()
file
.
open
(
filename
)
repeat
local
pl
=
file
.
read
(
1024
)
if
pl
then
m
:
publish
(
"/topic2"
,
pl
,
0
,
0
)
end
until
not
pl
file
.
close
()
end
-- payload(json): {"cmd":xxx,"content":xxx}
function
topic1func
(
m
,
pl
)
print
(
"get1: "
..
pl
)
local
pack
=
cjson
.
decode
(
pl
)
if
pack
.
content
then
if
pack
.
cmd
==
"open"
then
file
.
open
(
pack
.
content
,
"w+"
)
elseif
pack
.
cmd
==
"write"
then
file
.
write
(
pack
.
content
)
elseif
pack
.
cmd
==
"close"
then
file
.
close
()
elseif
pack
.
cmd
==
"remove"
then
file
.
remove
(
pack
.
content
)
elseif
pack
.
cmd
==
"run"
then
dofile
(
pack
.
content
)
elseif
pack
.
cmd
==
"read"
then
pubfile
(
m
,
pack
.
content
)
end
end
end
m_dis
[
"/topic1"
]
=
topic1func
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
end
)
m
:
on
(
"offline"
,
function
(
conn
)
print
(
"disconnect to broker..."
)
print
(
node
.
heap
())
end
)
m
:
on
(
"message"
,
dispatch
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
m
:
connect
(
192
.
168
.
18
.
88
,
1883
,
0
,
1
)
-- usage:
-- another client(pc) subscribe to /topic2, will receive the test.lua content.
-- and publish below message to /topic1
-- {"cmd":"open","content":"test.lua"}
-- {"cmd":"write","content":"print([[hello world]])\n"}
-- {"cmd":"write","content":"print(\"hello2 world2\")\n"}
-- {"cmd":"write","content":"test.lua"}
-- {"cmd":"run","content":"test.lua"}
-- {"cmd":"read","content":"test.lua"}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment