moved config to config.lua, static IP option, optimizations
moved config to config.lua, static IP option, optimizations

file:b/config.lua (new)
  -- network
  network = {}
  network.ip = "192.168.0.51"
  network.netmask = "255.255.255.0"
  network.gateway = "192.168.0.1"
 
  -- MQTT
  prefix = "/IoTmanager"
  device = "/" .. wifi.sta.getmac() -- set the device as the wifi card mac address. Is unique, also easy to trace
 
  mqttConfig = {}
  mqttConfig.host = '192.168.0.50'
  mqttConfig.port = '9443'
  mqttConfig.user = 'iot'
  mqttConfig.pass = 'iot'
  mqttConfig.secure = 1
file:a/init.lua -> file:b/init.lua
-- init.lua -- -- init.lua --
   
  dofile("config.lua")
   
local def_sta_config=wifi.sta.getdefaultconfig(true) local def_sta_config=wifi.sta.getdefaultconfig(true)
if def_sta_config.ssid ~= "" then if def_sta_config.ssid ~= "" then
print(string.format("Found wifi config data in flash\n\tssid:\"%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(string.format("Found wifi config data in flash\n\tssid:\"%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 "")))
  wifi.sta.config(def_sta_config)
  if network.ip ~= "" then
  --wifi.sta.setip(network)
  end
else else
print("No wifi config found on flash. Turning on IoT Setup") print("No wifi config found on flash. Turning on IoT Setup")
--wifi.setmode(wifi.STATIONAP)  
--wifi.ap.config({ssid="IoTSetup_" .. wifi.sta.getmac(), auth=wifi.OPEN})  
--enduser_setup.manual(true)  
end end
   
-- configure wifi via enduser setup -- configure wifi via enduser setup
  --wifi.setmode(wifi.STATIONAP)
  --wifi.ap.config({ssid="IoTSetup_" .. wifi.sta.getmac(), auth=wifi.OPEN})
  --enduser_setup.manual(true)
enduser_setup.start( enduser_setup.start(
function() function()
do do
  if network.ip ~= "" then
  --wifi.sta.setip(network)
  end
tmr.stop(0) tmr.stop(0)
tmr.alarm(1, 3000, 1, function() tmr.alarm(1, 3000, 1, function()
if wifi.sta.getip()==nil then if wifi.sta.getip()==nil then
print("\tConnected to access point, obtaining IP address ...") print("\tConnected to access point, obtaining IP address ...")
else else
def_sta_config=wifi.sta.getdefaultconfig(true) def_sta_config=wifi.sta.getdefaultconfig(true)
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(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()) print('\tip: ', wifi.sta.getip())
--enduser_setup.stop() --enduser_setup.stop()
-- run the main file -- run the main file
if file.exists("main.lc") then if file.exists("main.lc") then
dofile("main.lc") dofile("main.lc")
else else
dofile("main.lua") dofile("main.lua")
end end
tmr.stop(1) tmr.stop(1)
end end
end) end)
end end
end, end,
function(err, str) function(err, str)
print("enduser_setup: Err #" .. err .. ": " .. str) print("enduser_setup: Err #" .. err .. ": " .. str)
end end
); );
   
tmr.alarm(0, 3000, 1, function() tmr.alarm(0, 3000, 1, function()
if wifi.sta.getip()==nil then if wifi.sta.getip()==nil then
def_sta_config=wifi.sta.getdefaultconfig(true) def_sta_config=wifi.sta.getdefaultconfig(true)
print("Connecting to access point " .. def_sta_config.ssid) print("Connecting to access point " .. def_sta_config.ssid)
else else
tmr.stop(0) tmr.stop(0)
end end
end) end)
   
-- configure wifi reset button -- configure wifi reset button
pin = 3 pin = 3
function pin3cb() function pin3cb()
print("Resetting wireless configuration and restarting") print("Resetting wireless configuration and restarting")
node.restore() node.restore()
node.restart() node.restart()
end end
gpio.trig(pin, "down", pin3cb) gpio.trig(pin, "down", pin3cb)
   
file:a/mqtt-config.lua (deleted)
-- mqtt-config.lua --  
 
prefix = "/IoTmanager"  
device = "/" .. wifi.sta.getmac() -- set the device as the wifi card mac address. Is unique, also easy to trace  
 
mqttConfig = {}  
mqttConfig.host = '192.168.0.50'  
mqttConfig.port = '9443'  
mqttConfig.user = 'iot'  
mqttConfig.pass = 'iot'  
mqttConfig.secure = 1  
 
file:a/mqtt.lua -> file:b/mqtt.lua
-- mqtt.lua -- -- mqtt.lua --
   
-- include MQTT config file  
dofile("mqtt-config.lua")  
   
print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port) print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port)
   
local status1 = 1 local status1 = 1
local gpio12 = 6 local gpio12 = 6
gpio.mode(gpio12, gpio.OUTPUT) gpio.mode(gpio12, gpio.OUTPUT)
--gpio.write(gpio12, gpio.LOW) -- OFF --gpio.write(gpio12, gpio.LOW) -- OFF
gpio.write(gpio12, gpio.HIGH) -- ON gpio.write(gpio12, gpio.HIGH) -- ON
   
local status2 = 1 local status2 = 1
local gpio13 = 7 local gpio13 = 7
gpio.mode(gpio13, gpio.OUTPUT) gpio.mode(gpio13, gpio.OUTPUT)
--gpio.write(gpio13, gpio.LOW) -- OFF --gpio.write(gpio13, gpio.LOW) -- OFF
gpio.write(gpio13, gpio.HIGH) -- ON gpio.write(gpio13, gpio.HIGH) -- ON
   
-- initiate the mqtt client and set keepalive timer to 120sec -- initiate the mqtt client and set keepalive timer to 120sec
local mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1) local mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
   
mqtt:on("connect", function(con) mqtt:on("connect", function(con)
print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n") print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
end) end)
mqtt:on("offline", function(con) mqtt:on("offline", function(con)
print ("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n") print ("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
end) end)
   
-- on receive message -- on receive message
mqtt:on("message", function(conn, topic, data) mqtt:on("message", function(conn, topic, data)
   
print ("\tReceived topic : " .. topic .. " / data : " .. data) print ("\tReceived topic : " .. topic .. " / data : " .. data)
   
if (topic == prefix .. device .. "/toggle1/control") then if (topic == prefix .. device .. "/toggle1/control") then
if data == "1" then if data == "1" then
print "\t\tReceived message ON@Relay1" print "\t\tReceived message ON@Relay1"
gpio.write(gpio12, gpio.HIGH) gpio.write(gpio12, gpio.HIGH)
status1 = 1 status1 = 1
elseif data == "0" then elseif data == "0" then
print "\t\tReceived message OFF@Relay1" print "\t\tReceived message OFF@Relay1"
gpio.write(gpio12, gpio.LOW) gpio.write(gpio12, gpio.LOW)
status1 = 0 status1 = 0
end end
-- publish status to be in sync with all devices connected -- publish status to be in sync with all devices connected
mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn) mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
end) end)
elseif topic == prefix .. device .. "/toggle2/control" then elseif topic == prefix .. device .. "/toggle2/control" then
if data == "1" then if data == "1" then
print "\t\tReceived message ON@Relay2" print "\t\tReceived message ON@Relay2"
gpio.write(gpio13, gpio.HIGH) gpio.write(gpio13, gpio.HIGH)
status2 = 1 status2 = 1
elseif data == "0" then elseif data == "0" then
print "\t\tReceived message OFF@Relay2" print "\t\tReceived message OFF@Relay2"
gpio.write(gpio13, gpio.LOW) gpio.write(gpio13, gpio.LOW)
status2 = 0 status2 = 0
end end
-- publish status to be in sync with all devices connected -- publish status to be in sync with all devices connected
mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 1, function(conn) mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
end) end)
else else
local list = {} local list = {}
local k = 0 local k = 0
for v in string.gmatch(topic, "([^/]+)") do for v in string.gmatch(topic, "([^/]+)") do
list[k] = v list[k] = v
k = k + 1 k = k + 1
end end
local id = list[1] local id = list[1]
local msg = cjson.decode(data) local msg = cjson.decode(data)
print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"'); print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"');
   
if msg.command == "getPages" then 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) mqtt:publish(prefix .. device .. "/response", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 0, function(conn)
end) end)
elseif msg.command == "getPageById" then elseif msg.command == "getPageById" then
--if (msg.param == 10 or msg.param == 0) 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"}' 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) mqtt:publish(prefix .. device .. "/config", msg, 1, 0, function(conn)
end) end)
mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn) mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
end) end)
--end end
--if (msg.param == 20 or msg.param == 0) then 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"}' 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) mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
end) end)
mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 1, function(conn) mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
end) end)
--end end
end end
end end
end) end)
   
mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn) mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn)
print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n") print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
-- subscribe topic with qos = 1 -- subscribe topic with qos = 1
mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn) mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
end) end)
end) end)
   
comments