Base Setup

Description
This page describes the base setup of most of the projects. Basically an arduino and ESP8266

Code

 * Base code for ESP8266

Test Code
m = mqtt.Client(wifi.sta.getmac, 120, "", "")

m:lwt("/lwt", wifi.sta.getmac, 0, 0)

m:on("offline", function(con)

sendToArduino("status", "reconnecting")

--print(node.heap)

tmr.alarm(1, 10000, 0, function

m:connect("10.0.0.83", 1883, 0)

end)

end)

-- on publish message receive event

m:on("message", function(conn, topic, data)

print(topic .. ":" )

if data ~= nil then

print(data)

end

if topic == "/IOT/"..node.chipid.."/Commands/GetHeap" then sendHeapInfo

elseif topic == "/IOT/"..node.chipid.."/Commands/RestartESP" then restartESP

else

sendMessage("Unknown command received: "..topic)

end

end)

tmr.alarm(0, 5000, 1, function

sendToArduino("status", "Starting ESP")

if wifi.sta.status == 5 then

tmr.stop(0)

m:connect("10.0.0.83", 1883, 0, function(conn)

sendToArduino("status", "connected")

m:subscribe("/IOT/"..node.chipid.."/Commands/#",0, function(conn)

sendMessage("Listening for commands")

end)

end)

end

end)

function restartESP

sendMessage("Restarting ESP")

m:close

node.restart

end

function sendHeapInfo

sendMessage(node.heap)

end

function sendToArduino(command, payload)

print(command.."::"..payload)

end

function sendMessage(msg)

m:publish("/IOT/"..node.chipid.."/Messages",msg,0,0, function(conn)end)

end

function sendTemp(value2)

m:publish("/IOT/"..node.chipid.."/Temperature",value2,0,0, function(conn)end)

end

function sendHumidity(value2)

m:publish("/IOT/"..node.chipid.."/Humidity",value2,0,0, function(conn)end)

end

function sendMovement(value2)

m:publish("/IOT/"..node.chipid.."/Movement",value2,0,0, function(conn)end)

end

function sendLightValue(value2)

m:publish("/IOT/"..node.chipid.."/Light",value2,0,0, function(conn)end)

end