-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmosquitto.lua
More file actions
70 lines (57 loc) · 2.05 KB
/
mosquitto.lua
File metadata and controls
70 lines (57 loc) · 2.05 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
local module = {}
m = nil
local function register_device()
m:subscribe("BedRoom/rgb/set",0)
end
local function push_availablitity()
m:publish("BedRoomRGBAvailability","online",1,0)
end
local function mqtt_start()
m = mqtt.Client(config.ID, 120, config.USER,config.PWD)
m:on("message",
function(client, topic, data)
t = sjson.decode(data)
if t["state"] == "ON" and t["color"] == nil and t["brightness"] == nil and t["effect"] == nil then
rgb.white()
m:publish("BedRoom/rgb","{\"state\":\"ON\"}",0,1)
elseif t["state"] == "OFF" and t["color"] == nil and t["brightness"] == nil and t["effect"] == nil then
rgb.close()
m:publish("BedRoom/rgb","{\"state\":\"OFF\"}",0,1)
elseif t["color"] ~= nil and t["brightness"] == nil and t["effect"] == nil then
g = t["color"]["g"]
r = t["color"]["r"]
b = t["color"]["b"]
rgb.change_color(g, r, b)
elseif t["color"] == nil and t["brightness"] == nil and t["effect"] ~= nil then
print(t["effect"])
rgb.change_effects(t["effect"])
elseif t["effect"] ~= nil and t["effect"] == "static" then
rgb.close_effect()
elseif t["color"] == nil and t["brightness"] ~= nil and t["effect"] == nil then
print(t["brightness"])
brightness = t["brightness"]
rgb.set_brightness(brightness)
end
end
)
m:on("offline",
function(client)
mqtt_start()
end
)
m:lwt("BedRoomRGBAvailability", "offline")
m:connect(config.HOST, config.PORT, 0, 0,
function(client)
register_device()
tmr.alarm(4, config.DELAY, tmr.ALARM_AUTO, push_availablitity)
end,
function(client, reason)
print("MQTT Connect Failed" .. reason)
node.restart()
end
)
end
function module.start()
mqtt_start()
end
return module