-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.lua
181 lines (159 loc) · 5.54 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local module = {}
m = nil
setStatus = nil
-- Toggling LED
local pinOnOff = 2 --> GPIO4 -->D2
local state = gpio.LOW
gpio.mode(pinOnOff, gpio.OUTPUT)
local isMqttUnavailable = 0
-- Set relay from last known status in status file
if init_status ~= nil then
if init_status == "ON" then
print("Init status from file: ON")
gpio.write(pinOnOff, gpio.HIGH)
elseif init_status == "OFF" then
print("Init status from file: OFF")
gpio.write(pinOnOff, gpio.LOW)
else
print("Can't read init status from file. Set to OFF")
gpio.write(pinOnOff, gpio.LOW)
end
end
local is_started = 0
function setStatus(pSt)
file.open("status.lua", "w")
file.write('status = "' .. pSt .. '"')
file.close()
if pSt ~= nil then
init_status = pSt
end
end
local function push(pState)
if pState == "ON" then
print("Switch On")
gpio.write(pinOnOff, gpio.HIGH)
end
if pState == "OFF" then
print("Switch Off")
gpio.write(pinOnOff, gpio.LOW)
end
-- Publishing status
m:publish(config.STATUS, pState, config.QOS, config.RETAIN_TRUE, function(conn) end)
if pState ~= nil then
setStatus(pState)
end
end
-- Sends my id to the broker for registration
local function register_myself()
print("Subscribing...")
m:subscribe({[config.SUBPOINT]=config.QOS, [config.PING]=config.QOS, [config.PRINTTIME]=config.QOS, nil})
print("Subscribed on " .. config.SUBPOINT)
end
local function connack_string(connack_code)
if connack_code == 0 then
print("Connection Accepted.")
elseif connack_code == -1 then
print("Connection Refused: Timeout trying to send the Connect message.")
elseif connack_code == -2 then
print("Connection Refused: Timeout waiting for a CONNACK from the broker.")
elseif connack_code == -3 then
print("Connection Refused: DNS Lookup failed.")
elseif connack_code == -4 then
print("Connection Refused: The response from the broker was not a CONNACK as required by the protocol.")
elseif connack_code == -5 then
print("Connection Refused: There is no broker listening at the specified IP Address and Port.")
elseif connack_code == 1 then
print("Connection Refused: The broker is not a 3.1.1 MQTT broker.")
elseif connack_code == 2 then
print("Connection Refused: The specified ClientID was rejected by the broker. (See mqtt.Client()).")
elseif connack_code == 3 then
print("Connection Refused: The server is unavailable.")
elseif connack_code == 4 then
print("Connection Refused: The broker refused the specified username or password.")
elseif connack_code == 5 then
print("Connection Refused: The username is not authorized.")
else
print("Connection Refused: unknown reason.")
end
end
function reconnect()
print ("Reconnecting MQTT broker...")
m:connect(config.BROKER, config.PORT, config.QOS, function(client) print("Connected to MQTT")
register_myself() --run the subscripion function
end)
end
local function mqtt_start()
print("Connecting to broker...")
is_started = 1
-- Connect to broker
m:connect(config.BROKER, config.PORT, config.QOS, config.RECONNECT,
function(con)
print("Connected!")
if isMqttUnavailable > 0 then
isMqttUnavailable = 0
print("Sending information about MQTT Broket was down!")
m:publish(config.STATUS, "Was Offline, now is Online", config.QOS, config.RETAIN, function(conn) end)
tmr.stop(5)
-- Print status of relay
if init_status ~= nil then
print("Init status: " .. init_status)
end
end
register_myself()
print("Publishing AVBL status on: " .. config.STATUS)
m:publish(config.STATUS, "AVLB", config.QOS, config.RETAIN, function(conn) end)
print("Done!")
m:publish(config.STATUS, "ONLINE", config.QOS, config.RETAIN_TRUE, function(conn) end)
-- Set switch to last known state
if init_status ~= nil then
if init_status == "ON" then
m:publish(config.STATUS, init_status, config.QOS, config.RETAIN_TRUE, function(conn) end)
elseif init_status == "OFF" then
m:publish(config.STATUS, init_status, config.QOS, config.RETAIN_TRUE, function(conn) end)
else
m:publish(config.STATUS, "OFF", config.QOS, config.RETAIN_TRUE, function(conn) end)
end
end
end,
function(client, reason)
print("failed reason: " .. reason)
print(connack_string(reason))
register_myself()
m:publish(config.STATUS, "OFF", config.QOS, config.RETAIN_TRUE, function(conn) end)
end)
-- on receive message
m:on("message", function(conn, topic, data)
--print(topic .. ":" ..data)
if data ~= nil then
if is_started == 0 then
if topic == node.chipid() .."/SWITCH" then
if data == "ON" or data == "OFF" then
push(data)
end
end
end
is_started = 0
if topic == config.PING and data == "AVLB" then
m:publish(config.STATUS, "AVLB", config.QOS, config.RETAIN, function(conn) end)
end
end
end)
m:on("offline", function(client)
print ("Offline " .. config.ID)
isMqttUnavailable = isMqttUnavailable + 1
print("Restarting MQTT module...")
m:lwt(config.STATUS, "OFFLINE", 0, 0)
tmr.alarm(5, 10000, 1, mqtt_start)
register_myself()
end)
end
module.timeout_restart = 0;
reset_pin = 5 --> GPIO14 -->D5
gpio.mode(reset_pin, gpio.INT, gpio.PULLUP)
function module.start()
print("Starting MQTT module...")
m = mqtt.Client(config.ID, config.KEEPALIVE, config.UN, config.PS)
m:lwt(config.STATUS, "OFFLINE", 0, config.RETAIN_TRUE)
mqtt_start()
end
return module