demo for electrodragon.com esp relay master
demo for electrodragon.com esp relay

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
@@ -28,8 +28,6 @@
 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()
@@ -70,24 +68,44 @@
         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 .. '"');
+        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" : 10, "pageName" : "Dogs heating"}]}', 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)
+        local m = {}
+        m.pageName = "Dogs heating"
+        m.pageId = 10
+        m.widget = "toggle"
+        m.title = "Mara"
+        m.topic = prefix .. device ..'/toggle1'
+        m.checked = status1
+        m.template = "template-1"
+        m.icon = "ion-ios-paw"
+        m.class4 = "bg-green"
+        m.order = 10
+        mqtt:publish(m.topic .. "/config", cjson.encode(m), 1, 0, function(conn)
         end)
-        mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
+        --mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
+        --end)
+
+        local m = {}
+        m.pageName = "Dogs heating"
+        m.pageId = 10
+        m.widget = "toggle"
+        m.title = "Linda"
+        m.topic = prefix .. device ..'/toggle2'
+        m.checked = status1
+        m.template = "template-1"
+        m.icon = "ion-ios-paw"
+        m.class4 = "bg-green"
+        m.order = 20
+        mqtt:publish(m.topic .. "/config", cjson.encode(m), 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)
+        --mqtt:publish(prefix .. device .. "/toggle2/confirm", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
+        --end)
     end
 end)
 

comments