IoT Control Center sensor demo sensor-demo
IoT Control Center sensor demo

file:a/config.lua -> file:b/config.lua
--- a/config.lua
+++ b/config.lua
@@ -1,11 +1,11 @@
 -- network
 network = {}
-network.ip = "192.168.0.51"
-network.netmask = "255.255.255.0"
-network.gateway = "192.168.0.1"
+--network.ip = "192.168.0.51"
+--network.netmask = "255.255.255.0"
+--network.gateway = "192.168.0.1"
 
 -- MQTT
-prefix = "/IoTmanager"
+prefix = "/iotcc"
 device = "/" .. wifi.sta.getmac() -- set the device as the wifi card mac address. Is unique, also easy to trace
 
 mqttConfig = {}

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -29,8 +29,6 @@
 
     print ("\tReceived topic : " .. topic .. " / message : " .. message)
 
-    local msg = cjson.decode(message)
-
     -- catch json errors
     local json, msg = pcall(function()
         return cjson.decode(message)
@@ -43,50 +41,29 @@
         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 .. '"');
+    if topic == prefix .. "/device" then
+        print('\tNew request from IoT Control Center: clientId="' .. msg.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)
+        mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"pageId" : 100, "pageName" : "Temp sensor"}]}', 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)
+        publishMsg = {}
+        publishMsg.pageName = "Temp sensor"
+        publishMsg.pageId = 100
+        publishMsg.widget = "data"
+        publishMsg.format = "int"
+        publishMsg.title = "Temp sensor 1"
+        publishMsg.topic =  prefix .. device .. '/tempsensor'
+        publishMsg.value = "22"
+        publishMsg.valuedescription = "degrees C"
+        publishMsg.template = "template-3"
+        publishMsg.icon = "ion-ios-home"
+        publishMsg.class = "bg-green"
+        publishMsg.order = 10
 
-        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)
+        mqtt:publish(publishMsg.topic .. "/config", cjson.encode(publishMsg), 1, 0, function(conn)
         end)
     end
 end)
@@ -94,7 +71,15 @@
 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)
+    mqtt:subscribe({[prefix .. "/device"]=1}, function(conn)
     end)
+
+    tmr.alarm(3, 3000, 1, function()
+        -- read sensor temp and set it below
+        publishMsg = {}
+        publishMsg.value = math.random(10, 30)
+        mqtt:publish(prefix .. device .. '/tempsensor/data', cjson.encode(publishMsg), 1, 0, function(conn)
+        end)
+    end)
+
 end)
-

comments