initial commit
initial commit

file:b/init.lua (new)
--- /dev/null
+++ b/init.lua
@@ -1,1 +1,34 @@
+-- init.lua --
 
+-- Configure wifi via enduser setup
+enduser_setup.start(
+    function()
+        do
+        local def_sta_config=wifi.sta.getdefaultconfig(true)
+            tmr.alarm(1, 3000, 1, function()
+                if wifi.sta.getip()==nil then
+                    print("\tconnected to access point, obtaining IP address ...")
+                else
+                    -- TODO: remove password
+                    print(string.format("\tconnected to access point ssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
+                    print('\tip: ', wifi.sta.getip())
+                    -- Run the main file
+                    dofile("main.lua")
+                    tmr.stop(1)
+                end
+            end)
+        end
+    end,
+    function(err, str)
+        print("enduser_setup: Err #" .. err .. ": " .. str)
+    end
+);
+
+tmr.alarm(0, 3000, 1, function() 
+    if wifi.sta.getip()==nil then
+        print("connecting to access point ...")
+    else
+        tmr.stop(0)
+    end
+end)
+

file:b/main.lua (new)
--- /dev/null
+++ b/main.lua
@@ -1,1 +1,5 @@
+-- main.lua --
 
+-- Run the mqtt file
+dofile("mqtt.lua")
+

file:b/mqtt.lua (new)
--- /dev/null
+++ b/mqtt.lua
@@ -1,1 +1,87 @@
+-- mqtt.lua --
 
+prefix = "/IoTmanager"
+device = "/relay01"
+
+local status1 = 1
+gpio12 = 6
+gpio.mode(gpio12, gpio.OUTPUT)
+--gpio.write(gpio12, gpio.LOW)
+gpio.write(gpio12, gpio.HIGH)
+
+local status2 = 1
+gpio13 = 7
+gpio.mode(gpio13, gpio.OUTPUT)
+--gpio.write(gpio13, gpio.LOW)
+gpio.write(gpio13, gpio.HIGH)
+
+-- initiate the mqtt client and set keepalive timer to 120sec
+
+mqtt = mqtt.Client("nodemcu", 120, "qdcaxgqc", "eQurLLKpesr9", 1)
+
+mqtt:on("connect", function(con)
+    print ("connected to MQTT broker\n")
+end)
+mqtt:on("offline", function(con)
+    print ("\tdisconected from MQTT broker, reconnecting\n")
+end)
+
+-- on receive message
+mqtt:on("message", function(conn, topic, data)
+
+    print ("\tReceived topic : " .. topic .. " / data : " .. data)
+
+    if (topic == prefix .. device .. "/toggle1/control") then
+        if data == "1" then
+            print "\t\tReceived message ON@Relay1"
+            gpio.write(gpio12, gpio.HIGH)
+            status1 = 1
+        elseif data == "0" then
+            print "\t\tReceived message OFF@Relay1"
+            gpio.write(gpio12, gpio.LOW)
+            status1 = 0
+        end
+    elseif topic == prefix .. device .. "/toggle2/control" then
+        if data == "1" then
+            print "Received message ON@Relay2"
+            gpio.write(gpio13, gpio.HIGH)
+            status2 = 1
+        elseif data == "0" then
+            print "Received message OFF@Relay2"
+            gpio.write(gpio13, gpio.LOW)
+            status2 = 0
+        end
+    else
+        local id = 1
+        msg = cjson.decode(data)
+        print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"');
+        
+        if msg.command == "getPages" then
+            mqtt:publish(prefix .. device .. "/response", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 1, function(conn)    
+            end)
+        elseif msg.command == "getPageById" then
+            --if (msg.param == 10 or msg.param == 0) then
+                msg = '{"id":"1", "page": "Page 1", "pageId": 10, "widget":"toggle", "descr":"Relay 1", "topic":"' .. prefix .. device ..'/toggle1"}'
+                mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
+                end)
+                mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn)
+                end)
+            --end
+            --if (msg.param == 20 or msg.param == 0) then
+                msg = '{"id":"2", "page": "Page 2", "pageId": 20, "widget":"toggle", "descr":"Relay 2", "topic":"' .. prefix .. device ..'/toggle2"}'
+                mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
+                end)
+                mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 1, function(conn)
+                end)
+            --end
+        end
+    end
+end)
+
+mqtt:connect("m20.cloudmqtt.com", 29338, 1, 1, function(conn) 
+    print ("connected to MQTT broker\n")
+    -- subscribe topic with qos = 2
+    mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
+    end)
+end)
+

comments