WiFiSwitch/mqtt.lua

102 lines
2.5 KiB
Lua
Raw Normal View History

2015-09-11 13:47:00 +02:00
BROKER = "172.16.2.15"
2015-09-11 09:51:54 +02:00
BRPORT = 1883
BRUSER = ""
BRPWD = ""
2015-09-11 11:32:31 +02:00
CLIENTID = "ESP8266-" .. node.chipid()
2015-09-10 15:25:03 +02:00
2015-09-13 22:05:36 +02:00
SWITCH_PIN = 3
2015-09-10 15:25:03 +02:00
2015-09-13 22:05:36 +02:00
SWITCH_ID = 1
2015-09-10 15:25:03 +02:00
2015-09-11 09:51:54 +02:00
topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID}
2015-09-11 11:32:31 +02:00
TOPIC_WATCHDOG = 1
TOPIC_SWITCH = 2
2015-09-11 09:51:54 +02:00
pub_sem = 0
current_topic = 1
topicsub_delay = 50
2015-09-10 15:25:03 +02:00
id2 = 0
2015-09-11 11:32:31 +02:00
switch_state = false
old_switch_state = true
function alarm()
node.restart()
end
2015-09-10 15:25:03 +02:00
2015-09-11 11:32:31 +02:00
gpio.mode(SWITCH_PIN, gpio.OUTPUT)
gpio.write(SWITCH_PIN, gpio.LOW)
2015-09-10 15:25:03 +02:00
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
2015-09-11 11:32:31 +02:00
tmr.alarm(4, 60000, 1, alarm)
2015-09-10 15:25:03 +02:00
m:connect( BROKER , BRPORT, 0, function(conn)
2015-09-11 11:32:31 +02:00
print("4")
2015-09-11 09:51:54 +02:00
mqtt_sub()
2015-09-10 15:25:03 +02:00
end)
2015-09-11 09:51:54 +02:00
2015-09-10 15:25:03 +02:00
function mqtt_sub()
if table.getn(topics) < current_topic then
run_main_prog()
else
m:subscribe(topics[current_topic] , 0, function(conn)
end)
2015-09-11 09:51:54 +02:00
current_topic = current_topic + 1
2015-09-10 15:25:03 +02:00
tmr.alarm(5, topicsub_delay, 0, mqtt_sub )
end
end
function publish_status()
2015-09-11 09:51:54 +02:00
if pub_sem == 0 then
pub_sem = 1
2015-09-10 15:25:03 +02:00
local uptime = tmr.time()
2015-09-11 11:32:31 +02:00
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 .. "}}"
2015-09-10 15:25:03 +02:00
m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
2015-09-11 11:32:31 +02:00
print("8: " .. id2 .. ", " .. switch_state_pres)
2015-09-11 09:51:54 +02:00
pub_sem = 0
id2 = id2 +1
2015-09-10 15:25:03 +02:00
end)
end
end
function run_main_prog()
2015-09-11 09:51:54 +02:00
tmr.alarm(3, 1000, 1, publish_status )
2015-09-10 15:25:03 +02:00
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if (data ~= nil ) then
print ( data )
2015-09-11 11:32:31 +02:00
if (topic == topics[TOPIC_SWITCH] and data == "switch on") then
switch_state = true
elseif (topic == topics[TOPIC_SWITCH] and data == "switch off") then
switch_state = false
elseif (topic == topics[TOPIC_SWITCH] and data == "switch toggle") then
switch_state = not switch_state
2015-09-11 13:47:00 +02:00
elseif (topic == topics[TOPIC_WATCHDOG] and data == "WauWau!") then
2015-09-11 11:32:31 +02:00
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.LOW)
2015-09-13 22:05:36 +02:00
else
gpio.write(SWITCH_PIN, gpio.HIGH)
2015-09-11 11:32:31 +02:00
end
2015-09-10 15:25:03 +02:00
end
end
end )
end