|
|
|
@ -3,14 +3,14 @@ __main = coroutine.create(function(__run) |
|
|
|
|
--Packages Used: file, timer, node, wifi, net, mqtt, gpio, uart |
|
|
|
|
--Global Constant |
|
|
|
|
----------------- |
|
|
|
|
VERSION = 'v1.0'; |
|
|
|
|
VERSION = 'v1.1'; |
|
|
|
|
HOSTNAME = file.getcontents('cfg/HOSTNAME') or 'default'; |
|
|
|
|
WIFI_SSID = file.getcontents('cfg/WIFI_SSID') or 'sola'; |
|
|
|
|
WIFI_PASSWD = file.getcontents('cfg/WIFI_PASSWD') or 'c5ce4dfbbd7f'; |
|
|
|
|
MQTT_SERVER = file.getcontents('cfg/MQTT_SERVER') or '192.168.3.6'; |
|
|
|
|
MQTT_PORT = file.getcontents('cfg/MQTT_PORT') or '1883'; |
|
|
|
|
SCRIPT = 'script.lua'; |
|
|
|
|
MQTT_KEEP_ALIVE = 60; |
|
|
|
|
MQTT_KEEP_ALIVE = 10; |
|
|
|
|
MQTT_TOPIC_PREFIX = '/hass/'..HOSTNAME..'/'; |
|
|
|
|
MQTT_RECONNECT_DELAY = 10 * 1000; |
|
|
|
|
MQTT_PUBLISH_INTERVAL = 100; |
|
|
|
@ -26,7 +26,12 @@ __main = coroutine.create(function(__run) |
|
|
|
|
REG = function(topic, payload) |
|
|
|
|
mqtt_publish_queue[topic] = payload; |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--Error Log |
|
|
|
|
node.setonerror(function(s) |
|
|
|
|
file.putcontents('err_msg', s); |
|
|
|
|
node.restart(); |
|
|
|
|
end); |
|
|
|
|
|
|
|
|
|
--Load User-defined Script |
|
|
|
|
----------------- |
|
|
|
@ -80,10 +85,13 @@ __main = coroutine.create(function(__run) |
|
|
|
|
m:close(); |
|
|
|
|
m:connect(MQTT_SERVER, MQTT_PORT, false); |
|
|
|
|
end |
|
|
|
|
--lwt |
|
|
|
|
m:lwt(MQTT_TOPIC_PREFIX..'online', '0', 0, 1); |
|
|
|
|
--MQTT events |
|
|
|
|
m:on("connect", function(m) |
|
|
|
|
FLAG_mqtt_status = true; |
|
|
|
|
m:subscribe({[MQTT_TOPIC_PREFIX..'ctl/#']=0, [MQTT_TOPIC_PREFIX..'debug/#']=0}); |
|
|
|
|
m:publish(MQTT_TOPIC_PREFIX..'online', '1', 0, 1); |
|
|
|
|
end); |
|
|
|
|
m:on("connfail", function(m, errCode) |
|
|
|
|
FLAG_mqtt_status = false; |
|
|
|
@ -120,7 +128,7 @@ __main = coroutine.create(function(__run) |
|
|
|
|
if FLAG_mqtt_status then |
|
|
|
|
local topic, payload = next(mqtt_publish_queue); |
|
|
|
|
if topic then |
|
|
|
|
m:publish(topic, payload, 0, 0); |
|
|
|
|
m:publish(topic, payload, 0, string.find(topic, MQTT_TOPIC_PREFIX..'snsr') and 1 or 0); |
|
|
|
|
mqtt_publish_queue[topic] = nil; |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|