-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessaging.lua
More file actions
51 lines (43 loc) · 1.53 KB
/
messaging.lua
File metadata and controls
51 lines (43 loc) · 1.53 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
require "configuration"
function connect_mqtt(callback)
-- Start a watchdog timer to reset if no response from server in 100s
local WDT_TIMEOUT = 100
local timeout = WDT_TIMEOUT
local wdt = tmr.create()
wdt:register(1000, tmr.ALARM_AUTO, function()
timeout = timeout - 1
if ( timeout <= 0 )
then
print("No keepalive from server in 100s, restarting")
node.restart()
end
end)
print("Starting watchdog (watches server-sent keepalives)")
wdt:start()
m = mqtt.Client(node.chipid(), 120)
m:on("message", function(client, topic, data)
print("Received data on topic " .. topic)
if (topic == "/keepalive")
then
print("Received keepalive, resetting WDT")
timeout = WDT_TIMEOUT
end
end)
net.dns.setdnsserver("1.1.1.1", 0)
net.dns.setdnsserver("8.8.8.8", 1)
print("Looking up the MQTT broker in DNS")
net.dns.resolve(CONF_MQTT, function(sk, ip)
if (ip == nil)
then
print("DNS resolution error")
node.reset()
end
print("MQTT broker resolved to:" .. ip)
m:connect(ip, 1883, 0, function(client)
print("MQTT Connection succesful")
client:publish("/iot/cave/hello/" .. node.chipid(), "Hello, I'm a motion sensor", 0, 0, function(client) print("sent") end)
client:subscribe("/keepalive", 0, function(client) print("Subscribed to keepalives") end)
callback(client)
end)
end)
end