-
Notifications
You must be signed in to change notification settings - Fork 3
/
application.lua
78 lines (66 loc) · 2.18 KB
/
application.lua
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
71
72
73
74
75
76
77
78
-- file : application.lua
local module = {}
m = nil
-- Sends a simple ping to the broker
local function send_ping()
m:publish(config.ENDPOINT .. "ping","id=" .. config.ID,0,0)
end
-- Sends my id to the broker for registration
local function register_myself()
m:subscribe(config.ENDPOINT .. config.ID .. "/+/set",0,function(conn)
print("Successfully subscribed to data endpoint")
end)
end
local function mqtt_handle_error(client, reason)
-- In reality, the connected function should do something useful!
-- https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/#connection-failure-callback-reason-codes
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, mqtt_connect)
end
local function mqtt_connect()
-- Connect to broker
m:connect(config.HOST, config.PORT, 0, 0, function(con)
register_myself()
-- And then pings each 60*1000 milliseconds
tmr.stop(6)
tmr.alarm(6, 60*1000, 1, send_ping)
end, mqtt_handle_error)
end
local function mqtt_start()
m = mqtt.Client(config.ID, 120)
-- register message callback beforehand
m:on("message", function(conn, topic, data)
if data ~= nil then
print(topic .. ": " .. data)
local saddr = string.match(topic, "/" .. config.ID .. "/(%x+)/set")
local addr = tonumber(saddr, 16)
local dimlevel = tonumber(data, 10)
if dimlevel > 74 then
radio.SendCommand(addr, 3) -- 100%
elseif dimlevel > 24 then
radio.SendCommand(addr, 2) -- 50%
elseif dimlevel > -1 then
radio.SendCommand(addr, 1) -- OFF
else
radio.SendCommand(addr, 0xFF) -- Learn
end
end
end)
mqtt_connect()
end
function module.start()
mqtt_start()
radio.start()
radio.on_command = function(addr, cmd)
local dimlevel = 0
if (cmd == 2) then
dimlevel = 50
elseif (cmd == 3) then
dimlevel = 100
elseif (cmd == 0xFF) then
return
end
m:publish(config.ENDPOINT .. config.ID .. string.format('/%04X/current', addr),
string.format('%d', dimlevel), 0, 0)
end
end
return module