works
This commit is contained in:
14
init.lua
14
init.lua
@ -8,34 +8,34 @@ wifiTrys = 0
|
|||||||
NUMWIFITRYS = 200
|
NUMWIFITRYS = 200
|
||||||
|
|
||||||
function launch()
|
function launch()
|
||||||
print("Connected to WIFI!")
|
print("A")
|
||||||
print("IP Address: " .. wifi.sta.getip())
|
print("B: " .. wifi.sta.getip())
|
||||||
tmr.alarm(0, 5000, 0, function() dofile(CMDFILE) end )
|
tmr.alarm(0, 5000, 0, function() dofile(CMDFILE) end )
|
||||||
end
|
end
|
||||||
|
|
||||||
function checkWIFI()
|
function checkWIFI()
|
||||||
if ( wifiTrys > NUMWIFITRYS ) then
|
if ( wifiTrys > NUMWIFITRYS ) then
|
||||||
print("Sorry. Not able to connect")
|
print("C")
|
||||||
else
|
else
|
||||||
ipAddr = wifi.sta.getip()
|
ipAddr = wifi.sta.getip()
|
||||||
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
|
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
|
||||||
tmr.alarm( 1 , 500 , 0 , launch )
|
tmr.alarm( 1 , 500 , 0 , launch )
|
||||||
else
|
else
|
||||||
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
|
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
|
||||||
print("Checking WIFI..." .. wifiTrys)
|
print("D: " .. wifiTrys)
|
||||||
wifiTrys = wifiTrys + 1
|
wifiTrys = wifiTrys + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print("-- Starting up! ")
|
print("E")
|
||||||
|
|
||||||
ipAddr = wifi.sta.getip()
|
ipAddr = wifi.sta.getip()
|
||||||
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
|
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
|
||||||
print("Configuring WIFI....")
|
print("F")
|
||||||
wifi.setmode( wifi.STATION )
|
wifi.setmode( wifi.STATION )
|
||||||
wifi.sta.config( SSID , APPWD)
|
wifi.sta.config( SSID , APPWD)
|
||||||
print("Waiting for connection")
|
print("G")
|
||||||
tmr.alarm( 0 , 2500 , 0 , checkWIFI )
|
tmr.alarm( 0 , 2500 , 0 , checkWIFI )
|
||||||
else
|
else
|
||||||
launch()
|
launch()
|
||||||
|
62
mqtt.lua
62
mqtt.lua
@ -2,47 +2,48 @@ BROKER = "192.168.87.100"
|
|||||||
BRPORT = 1883
|
BRPORT = 1883
|
||||||
BRUSER = ""
|
BRUSER = ""
|
||||||
BRPWD = ""
|
BRPWD = ""
|
||||||
CLIENTID = "ESP8266y-" .. node.chipid()
|
CLIENTID = "ESP8266-" .. node.chipid()
|
||||||
|
|
||||||
SWITCH_PIN = 0
|
SWITCH_PIN = 0
|
||||||
|
|
||||||
SWITCH_ID = 0
|
SWITCH_ID = 0
|
||||||
|
|
||||||
topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID}
|
topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID}
|
||||||
|
TOPIC_WATCHDOG = 1
|
||||||
|
TOPIC_SWITCH = 2
|
||||||
|
|
||||||
pub_sem = 0
|
pub_sem = 0
|
||||||
current_topic = 1
|
current_topic = 1
|
||||||
topicsub_delay = 50
|
topicsub_delay = 50
|
||||||
id2 = 0
|
id2 = 0
|
||||||
|
|
||||||
switch_state = 0
|
switch_state = false
|
||||||
|
old_switch_state = true
|
||||||
|
|
||||||
|
|
||||||
-- gpio.mode(SWITCH_PIN, gpio.OUTPUT)
|
function alarm()
|
||||||
-- gpio.write(SWITCH_PIN, gpio.LOW)
|
node.restart()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
gpio.mode(SWITCH_PIN, gpio.OUTPUT)
|
||||||
|
gpio.write(SWITCH_PIN, gpio.LOW)
|
||||||
|
|
||||||
print("Connecting to MQTT broker. Please wait...")
|
|
||||||
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
|
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
|
||||||
print("step 1")
|
tmr.alarm(4, 60000, 1, alarm)
|
||||||
tmr.alarm(4, 15, 1, mqtt_watchdog_expired)
|
|
||||||
print("step 2")
|
|
||||||
m:connect( BROKER , BRPORT, 0, function(conn)
|
m:connect( BROKER , BRPORT, 0, function(conn)
|
||||||
print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID )
|
print("4")
|
||||||
mqtt_sub()
|
mqtt_sub()
|
||||||
end)
|
end)
|
||||||
print("step 3")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mqtt_watchdog_expired()
|
|
||||||
print("mqtt watchdog expired")
|
|
||||||
end
|
|
||||||
|
|
||||||
function mqtt_sub()
|
function mqtt_sub()
|
||||||
if table.getn(topics) < current_topic then
|
if table.getn(topics) < current_topic then
|
||||||
run_main_prog()
|
run_main_prog()
|
||||||
else
|
else
|
||||||
m:subscribe(topics[current_topic] , 0, function(conn)
|
m:subscribe(topics[current_topic] , 0, function(conn)
|
||||||
print("Subscribing topic: " .. topics[current_topic - 1] )
|
|
||||||
end)
|
end)
|
||||||
current_topic = current_topic + 1
|
current_topic = current_topic + 1
|
||||||
tmr.alarm(5, topicsub_delay, 0, mqtt_sub )
|
tmr.alarm(5, topicsub_delay, 0, mqtt_sub )
|
||||||
@ -54,10 +55,13 @@ function publish_status()
|
|||||||
if pub_sem == 0 then
|
if pub_sem == 0 then
|
||||||
pub_sem = 1
|
pub_sem = 1
|
||||||
local uptime = tmr.time()
|
local uptime = tmr.time()
|
||||||
local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. ", \"state\":" .. switch_state .. "}}"
|
local switch_state_pres = 0
|
||||||
|
if (switch_state) then
|
||||||
|
switch_state_pres = 1
|
||||||
|
end
|
||||||
|
local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. ", \"state\":" .. switch_state_pres .. "}}"
|
||||||
m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
|
m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
|
||||||
print("Status sent: " .. id2)
|
print("8: " .. id2 .. ", " .. switch_state_pres)
|
||||||
print("State: " .. switch_state)
|
|
||||||
pub_sem = 0
|
pub_sem = 0
|
||||||
id2 = id2 +1
|
id2 = id2 +1
|
||||||
end)
|
end)
|
||||||
@ -72,12 +76,24 @@ function run_main_prog()
|
|||||||
print(topic .. ":" )
|
print(topic .. ":" )
|
||||||
if (data ~= nil ) then
|
if (data ~= nil ) then
|
||||||
print ( data )
|
print ( data )
|
||||||
if (data == "switch on") then
|
if (topic == topics[TOPIC_SWITCH] and data == "switch on") then
|
||||||
switch_state = 1
|
switch_state = true
|
||||||
gpio.write(SWITCH_PIN, gpio.HIGH)
|
elseif (topic == topics[TOPIC_SWITCH] and data == "switch off") then
|
||||||
elseif (data == "switch off") then
|
switch_state = false
|
||||||
switch_state = 0
|
elseif (topic == topics[TOPIC_SWITCH] and data == "switch toggle") then
|
||||||
gpio.write(SWITCH_PIN, gpio.LOW)
|
switch_state = not switch_state
|
||||||
|
elseif (topic == topics[TOPIC_WATCHDOG] and data == "wauwau") then
|
||||||
|
print("9")
|
||||||
|
tmr.alarm(4, 60000, 1, alarm)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (switch_state ~= old_switch_state) then
|
||||||
|
old_switch_state = switch_state
|
||||||
|
if (switch_state) then
|
||||||
|
gpio.write(SWITCH_PIN, gpio.HIGH)
|
||||||
|
else
|
||||||
|
gpio.write(SWITCH_PIN, gpio.LOW)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end )
|
end )
|
||||||
|
Reference in New Issue
Block a user