MQTT auto reconnect
[nodemcu-iot-bme280-weather-station.git] / mqtt.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
-- mqtt.lua --
 
-- initiate NTP sync
sntp.sync(ntpserver,
    function(sec, usec, server, info)
        rtctime.set(sec + timezoneHours * 60 * 60, 0)
        lcdPrint('NTP sync success! ' .. getDateTime())
        --update()
    end,
    function()
        lcdPrint('NTP sync failed!')
    end,
    1
)
 
-- initiate the mqtt client and set keepalive timer to 120sec
if mqttConfig.enabled == true then
 
    function onConnect()
        lcdPrint("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
        mqttConfig.connected = true
        -- subscribe topic with qos = 1
        mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
        end)
    end
 
    mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, true)
 
    mqtt:on("connect", function(conn)
        onConnect()
    end)
    mqtt:on("offline", function(conn)
        lcdPrint("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
        mqttConfig.connected = false
        connect()
    end)
 
    -- on receive message
    mqtt:on("message", function(conn, topic, message)
        lcdPrint("\tReceived topic : " .. topic .. " / message : " .. message)
 
        -- catch json errors
        local ok, json = pcall(sjson.decode, message)
        if ok then
        else
            lcdPrint("\tError parsing JSON : " .. message)
            return
        end
 
        if topic == prefix .. "/device" then
            lcdPrint('\tNew request from IoT Control Center: clientId="' .. json.clientId .. '"')
            if json.time then
                rtctime.set(json.time + timezoneHours * 60 * 60, 0)
                --update()
            end
            mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"id" : 50, "name" : "Weather stations", "icon": "fa fa-thermometer-quarter", "order": "1000"}]}', 1, 0, function(conn)
            end)
            update()
        end
    end)
 
    function connect()
        lcdPrint("\tConnecting to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
        mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, function(conn)
                onConnect()
            end,
            function(conn, reason)
                print("\tCould not connect to MQTT server")
                print("\tClient ", conn)
                print("\tReason ", reason)
                tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, connect())
        end)
    end
    connect()
end
 
comments