Skip to content

Commit dca0518

Browse files
author
Andy Gelme
committed
Version: 0.0 2011-07-28
0 parents  commit dca0518

17 files changed

+2040
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
readme.html

images/aiko_gateway.jpg

16.9 KB
Loading

images/lua_mqtt_overview.dia

1.48 KB
Binary file not shown.

images/lua_mqtt_overview.jpg

31.2 KB
Loading

images/pebble.jpg

22.7 KB
Loading

images/playstation_portable.jpg

35.4 KB
Loading

lua/example/example_00.lua

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/lua
2+
-- ------------------------------------------------------------------------- --
3+
-- example_00.lua
4+
-- ~~~~~~~~~~~~~~
5+
-- Please do not remove the following notices.
6+
-- Copyright (c) 2011 by Geekscape Pty. Ltd.
7+
-- Documentation: http://http://geekscape.github.com/lua_mqtt_client
8+
-- License: GPLv3 http://geekscape.org/static/aiko_license.html
9+
-- Version: 0.0
10+
--
11+
-- Description
12+
-- ~~~~~~~~~~~
13+
-- Subscribe to a topic and publish all received messages on another topic.
14+
--
15+
-- ToDo
16+
-- ~~~~
17+
-- - On failure, automatically reconnect to MQTT server.
18+
-- ------------------------------------------------------------------------- --
19+
20+
function callback(
21+
topic, -- string
22+
message) -- string
23+
24+
print("Topic: " .. topic .. ", message: '" .. message .. "'")
25+
26+
mqtt_client:publish(args.topic2, message)
27+
end
28+
29+
-- ------------------------------------------------------------------------- --
30+
31+
function is_openwrt()
32+
return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT
33+
end
34+
35+
-- ------------------------------------------------------------------------- --
36+
37+
if (not is_openwrt()) then require("luarocks.require") end
38+
require("lapp")
39+
40+
args = lapp [[
41+
Subscribe to topic1 and publish all messages on topic2
42+
-h,--host (default localhost) MQTT server hostname
43+
-i,--id (default example_00) MQTT client identifier
44+
-p,--port (default 1883) MQTT server port number
45+
-s,--topic1 (default test/1) Subscribe topic
46+
-t,--topic2 (default test/2) Publish topic
47+
]]
48+
49+
local MQTT = require("mqtt_library")
50+
51+
mqtt_client = MQTT.client.create(args.host, args.port, callback)
52+
53+
mqtt_client:connect(args.id)
54+
55+
mqtt_client:subscribe({ args.topic1 })
56+
57+
while (true) do
58+
mqtt_client:handler()
59+
socket.sleep(1.0) -- seconds
60+
end
61+
62+
mqtt_client:unsubscribe({ args.topic1 })
63+
64+
mqtt_client:destroy()
65+
66+
-- ------------------------------------------------------------------------- --

lua/example/example_01.lua

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/lua
2+
-- ------------------------------------------------------------------------- --
3+
-- example_01.lua
4+
-- ~~~~~~~~~~~~~~
5+
-- Please do not remove the following notices.
6+
-- Copyright (c) 2011 by Geekscape Pty. Ltd.
7+
-- Documentation: http://http://geekscape.github.com/lua_mqtt_client
8+
-- License: GPLv3 http://geekscape.org/static/aiko_license.html
9+
-- Version: 0.0
10+
--
11+
-- Description
12+
-- ~~~~~~~~~~~
13+
-- Subscribe to a topic on one MQTT server and publish all received messages
14+
-- to a topic on another MQTT server.
15+
--
16+
-- ToDo
17+
-- ~~~~
18+
-- - On failure, automatically reconnect to MQTT server(s).
19+
-- ------------------------------------------------------------------------- --
20+
21+
function callback(
22+
topic, -- string
23+
message) -- string
24+
25+
print("Topic: " .. topic .. ", message: '" .. message .. "'")
26+
27+
mqtt_client2:publish(args.topic2, message)
28+
end
29+
30+
-- ------------------------------------------------------------------------- --
31+
32+
function is_openwrt()
33+
return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT
34+
end
35+
36+
-- ------------------------------------------------------------------------- --
37+
38+
if (not is_openwrt()) then require("luarocks.require") end
39+
require("lapp")
40+
41+
args = lapp [[
42+
Subscribe to topic1 and publish all messages on topic2
43+
-g,--host1 (default localhost) Subscribe MQTT server hostname
44+
-h,--host2 (default localhost) Publish MQTT server hostname
45+
-i,--id (default example_01) MQTT client identifier
46+
-p,--port1 (default 1883) Subscribe MQTT server port number
47+
-q,--port2 (default 1883) Publish MQTT server port number
48+
-s,--topic1 (default test/1) Subscribe topic
49+
-t,--topic2 (default test/2) Publish topic
50+
]]
51+
52+
local MQTT = require("mqtt_library")
53+
54+
mqtt_client1 = MQTT.client.create(args.host1, args.port1, callback)
55+
mqtt_client2 = MQTT.client.create(args.host2, args.port2)
56+
57+
mqtt_client1:connect(args.id .. "a")
58+
mqtt_client2:connect(args.id .. "b")
59+
60+
mqtt_client1:subscribe({ args.topic1 })
61+
62+
while (true) do
63+
mqtt_client1:handler()
64+
mqtt_client2:handler()
65+
socket.sleep(1.0) -- seconds
66+
end
67+
68+
mqtt_client1:unsubscribe({ args.topic1 })
69+
70+
mqtt_client1:destroy()
71+
mqtt_client2:destroy()
72+
73+
-- ------------------------------------------------------------------------- --

lua/example/example_02.lua

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/lua
2+
-- ------------------------------------------------------------------------- --
3+
-- example_02.lua
4+
-- ~~~~~~~~~~~~~~
5+
-- Please do not remove the following notices.
6+
-- Copyright (c) 2011 by Geekscape Pty. Ltd.
7+
-- Documentation: http://http://geekscape.github.com/lua_mqtt_client
8+
-- License: GPLv3 http://geekscape.org/static/aiko_license.html
9+
-- Version: 0.0
10+
--
11+
-- Description
12+
-- ~~~~~~~~~~~
13+
-- Publish a sequence of messages to a specified topic.
14+
-- Used to control some coloured RGB LEDs.
15+
--
16+
-- ToDo
17+
-- ~~~~
18+
-- - On failure, automatically reconnect to MQTT server.
19+
-- ------------------------------------------------------------------------- --
20+
21+
function is_openwrt()
22+
return(os.getenv("USER") == "root") -- Assume logged in as "root" on OpenWRT
23+
end
24+
25+
-- ------------------------------------------------------------------------- --
26+
27+
if (not is_openwrt()) then require("luarocks.require") end
28+
require("lapp")
29+
30+
local args = lapp [[
31+
Subscribe to topic1 and publish all messages on topic2
32+
-h,--host (default localhost) MQTT server hostname
33+
-i,--id (default example_02) MQTT client identifier
34+
-p,--port (default 1883) MQTT server port number
35+
-s,--sleep (default 5.0) Sleep time between commands
36+
-t,--topic (default test/2) Topic on which to publish
37+
]]
38+
39+
local MQTT = require("mqtt_library")
40+
41+
local mqtt_client = MQTT.client.create(args.host, args.port)
42+
43+
mqtt_client:connect(args.id)
44+
45+
local index = 1
46+
local messages = { "c010000", "c000100", "c000001" }
47+
48+
while (true) do
49+
mqtt_client:handler()
50+
socket.sleep(0.1) -- seconds
51+
52+
socket.sleep(args.sleep) -- seconds
53+
mqtt_client:publish(args.topic, messages[index]);
54+
55+
index = index + 1
56+
if (index > #messages) then index = 1 end
57+
end
58+
59+
mqtt_client:destroy()
60+
61+
-- ------------------------------------------------------------------------- --

0 commit comments

Comments
 (0)