IoT Control Center compatibility
[nodemcu-iot-mqtt-relay.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- mqtt.lua --
 
print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port)
 
local status1 = 1
local gpio12 = 6
gpio.mode(gpio12, gpio.OUTPUT)
--gpio.write(gpio12, gpio.LOW) -- OFF
gpio.write(gpio12, gpio.HIGH) -- ON
 
local status2 = 1
local gpio13 = 7
gpio.mode(gpio13, gpio.OUTPUT)
--gpio.write(gpio13, gpio.LOW) -- OFF
gpio.write(gpio13, gpio.HIGH) -- ON
 
-- initiate the mqtt client and set keepalive timer to 120sec
local mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
 
mqtt:on("connect", function(con)
    print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
end)
mqtt:on("offline", function(con)
    print ("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
end)
 
-- on receive message
mqtt:on("message", function(conn, topic, message)
 
    print ("\tReceived topic : " .. topic .. " / message : " .. message)
 
    local msg = cjson.decode(message)
 
    -- catch json errors
    local json, msg = pcall(function()
        return cjson.decode(message)
    end)
 
    if json then
 
    else
        print("\tError parsing JSON : " .. message)
        return
    end
 
    if (topic == prefix .. device .. "/toggle1/data") then
        if msg.status == "1" then
            print "\t\tReceived message ON@Relay1"
            gpio.write(gpio12, gpio.HIGH)
            status1 = 1
        elseif msg.status == "0" then
            print "\t\tReceived message OFF@Relay1"
            gpio.write(gpio12, gpio.LOW)
            status1 = 0
        end
        -- publish status confirmation to be in sync with all devices connected
        mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
        end)
    elseif topic == prefix .. device .. "/toggle2/data" then
        if msg.status == "1" then
            print "\t\tReceived message ON@Relay2"
            gpio.write(gpio13, gpio.HIGH)
            status2 = 1
        elseif msg.status == "0" then
            print "\t\tReceived message OFF@Relay2"
            gpio.write(gpio13, gpio.LOW)
            status2 = 0
        end
        -- publish status confirmation to be in sync with all devices connected
        mqtt:publish(prefix .. device .. "/toggle2/confirm", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
        end)
    elseif topic == prefix .. "/device" then
        print('\tNew request from IoT Control Center: clientId="' .. clientId .. '"');
 
        -- check clientId's privileges here
 
        mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 0, function(conn)
        end)
 
        msg = '{"pageName": "Dogs heating", "pageId": 10, "widget":"toggle", "title":"Mara", "topic":"' .. prefix .. device ..'/toggle1", "checked":' .. status1 ..', "template": "template-1", "icon": "ion-ios-paw", "class4": "bg-green", "order" : 10}'
        mqtt:publish(prefix .. device .. "/toggle1/config", msg, 1, 0, function(conn)
        end)
        mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
        end)
 
        msg = '{"pageName": "Dogs heating", "pageId": 10, "widget":"toggle", "title":"Linda", "topic":"' .. prefix .. device ..'/toggle2", "checked":' .. status2 ..', "template": "template-1", "icon": "ion-ios-paw", "class4": "bg-green", "order" : 20}'
        mqtt:publish(prefix .. device .. "/toggle2/config", msg, 1, 1, function(conn)
        end)
        mqtt:publish(prefix .. device .. "/toggle2/confirm", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
        end)
    end
end)
 
mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn)
    print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
    -- subscribe topic with qos = 1
    mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
    end)
end)
 
comments