-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_rgb.lua
More file actions
70 lines (63 loc) · 1.89 KB
/
Copy pathmqtt_rgb.lua
File metadata and controls
70 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "cblgywch", "PhsKtj4F8GnB")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("node/lwt", "offline", 0, 0)
m:on("offline", function(con)
print ("MQTT offline")
led_start_blink(1000,100)
end)
m:on("connect", function(con)
print ("MQTT connected")
end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print("Received: " .. topic .. ": " .. data)
if topic == "node/led" then
if data == "on" then
led_on()
elseif data == "off" then
led_off()
elseif data == "toggle" then
led_toggle()
end
elseif topic == "node/rgb" then
if data == "cycle" then
print("Starting RGB cycle")
rgb_startCycle()
elseif data == "stop" then
print("Stopping RGB cycle")
rgb_stopCycle()
elseif data == "on" then
print("Setting RGB on")
rgb_on()
elseif data == "off" then
print("Setting RGB off")
rgb_off()
else
r, g, b = data:match("([^,]+),([^,]+),([^,]+)")
r = tonumber(r)
g = tonumber(g)
b = tonumber(b)
if (r ~= nil and g ~= nil and b ~= nil) then
rgb_set(r,g,b)
else
print("Unrecognized message: " .. data)
end
end
end
end)
-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("m20.cloudmqtt.com", 19767, 0,
function(conn)
print ("MQTT connected. Subscribing...")
-- subscribe topic with qos = 0
m:subscribe("node/#",0, function(conn)
print("Subscription successful")
led_start_blink(10000,100)
end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("node/hello","mac = " .. wifi.sta.getmac(),0,0, function(conn) print("Hello sent") end)
end
)