summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua19
1 files changed, 19 insertions, 0 deletions
diff --git a/init.lua b/init.lua
index 6dc32d0..4c38550 100644
--- a/init.lua
+++ b/init.lua
@@ -18,6 +18,9 @@ gpio.write(ledpin, 0)
sds011 = require("sds011")
+poll = tmr.create()
+polling = false
+
function log_restart()
print("Network error " .. wifi.sta.status())
end
@@ -65,6 +68,17 @@ function uart_callback(data)
work_period = string.format("%d min", sds011.work_period)
end
+ if sds011.work_period == 0 and not polling then
+ polling = true
+ port:write(sds011.set_report_mode(false))
+ poll:start()
+ end
+ if sds011.work_period > 0 and polling then
+ polling = false
+ port:write(sds011.set_report_mode(true))
+ poll:stop()
+ end
+
local json_str = string.format('{"rssi_dbm":%d,"period":"%s"', wifi.sta.getrssi(), work_period)
if sds011.pm2_5i ~= nil then
json_str = string.format('%s,"pm2_5_ugm3":%d.%d,"pm10_ugm3":%d.%d', json_str, sds011.pm2_5i, sds011.pm2_5f, sds011.pm10i, sds011.pm10f)
@@ -99,6 +113,10 @@ function publish_influx(payload)
end
end
+function query_data()
+ port:write(sds011.query())
+end
+
function hass_config(client, topic, message)
if topic == "config/" .. device_id .. "/set/work_period" then
local work_period = 0
@@ -133,6 +151,7 @@ function hass_register()
end
watchdog:register(20 * 60 * 1000, tmr.ALARM_SEMI, node.restart)
+poll:register(10 * 1000, tmr.ALARM_AUTO, query_data)
watchdog:start()
connect_wifi()