summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-04-19 17:46:19 +0200
committerDaniel Friesel <derf@finalrewind.org>2022-04-19 17:46:19 +0200
commit14c0657c42169d1953f3a7947a51a59c346616b9 (patch)
treeff5f70a9bb60ee2ef93e28adebc46f8a4d0fa67f
parentbcc02461025f89c0a74c93ccde88ef7384008f16 (diff)
add homeassistant integration, set measurement period via MQTT
-rw-r--r--init.lua52
-rw-r--r--sds011.lua3
2 files changed, 49 insertions, 6 deletions
diff --git a/init.lua b/init.lua
index e9abef9..2f51e0e 100644
--- a/init.lua
+++ b/init.lua
@@ -2,11 +2,13 @@ station_cfg = {}
dofile("config.lua")
delayed_restart = tmr.create()
-chipid = node.chipid()
-mqtt_prefix = "sensor/esp8266_" .. chipid
-mqttclient = mqtt.Client("esp8266_" .. chipid, 120)
+chip_id = node.chipid()
+device_id = "esp8266_" .. chip_id
+mqtt_prefix = "sensor/" .. device_id
+mqttclient = mqtt.Client(device_id, 120)
-print("ESP8266 " .. chipid)
+
+print("ESP8266 " .. chip_id)
ledpin = 4
gpio.mode(ledpin, gpio.OUTPUT)
@@ -33,7 +35,8 @@ function connect_mqtt()
print("IP address: " .. wifi.sta.getip())
print("Connecting to MQTT " .. mqtt_host)
delayed_restart:stop()
- mqttclient:on("connect", setup_client)
+ mqttclient:on("connect", hass_register)
+ mqttclient:on("message", hass_config)
mqttclient:on("offline", log_restart)
mqttclient:lwt(mqtt_prefix .. "/state", "offline", 0, 1)
mqttclient:connect(mqtt_host)
@@ -56,7 +59,11 @@ function uart_callback(data)
print("Invalid or data-less SDS011 frame")
return
end
- local json_str = string.format('{"pm25_ugm3": %d.%d, "pm10_ugm3": %d.%d, "rssi_dbm": %d}', pm25i, pm25f, pm10i, pm10f, wifi.sta.getrssi())
+ local work_period = "continuous"
+ if sds011.work_period > 0 then
+ work_period = string.format("%d min", sds011.work_period)
+ end
+ local json_str = string.format('{"pm2_5_ugm3": %d.%d, "pm10_ugm3": %d.%d, "rssi_dbm": %d, "period": "%s"}', pm25i, pm25f, pm10i, pm10f, wifi.sta.getrssi(), work_period)
if not publishing then
publishing = true
gpio.write(ledpin, 0)
@@ -68,6 +75,39 @@ function uart_callback(data)
end
end
+function hass_config(client, topic, message)
+ if topic == "config/" .. device_id .. "/set/work_period" then
+ local work_period = 0
+ local _, _, minutes = string.find(message, "([0-9]+) min")
+ if minutes ~= nil then
+ work_period = tonumber(minutes)
+ end
+ port:write(sds011.set_work_period(work_period))
+ end
+end
+
+function hass_register()
+ local hass_device = string.format('{"connections":[["mac","%s"]],"identifiers":["%s"],"model":"ESP8266","name":"ESP8266 SDS011","manufacturer":"DIY"}', wifi.sta.getmac(), device_id)
+ local hass_entity_base = string.format('"device":%s,"state_topic":"%s/data","expire_after":1800', hass_device, mqtt_prefix)
+ local hass_pm2_5 = string.format('{%s,"name":"PM2.5","unique_id":"%s_pm2_5","icon":"mdi:air-filter","unit_of_measurement":"µg/m³","value_template":"{{value_json.pm2_5_ugm3}}"}', hass_entity_base, device_id)
+ local hass_pm10 = string.format('{%s,"name":"PM10","unique_id":"%s_pm10","icon":"mdi:air-filter","unit_of_measurement":"µg/m³","value_template":"{{value_json.pm10_ugm3}}"}', hass_entity_base, device_id)
+ local hass_rssi = string.format('{%s,"name":"RSSI","unique_id":"%s_rssi","icon":"mdi:signal","unit_of_measurement":"dBm","value_template":"{{value_json.rssi_dbm}}","entity_category":"diagnostic"}', hass_entity_base, device_id)
+ local hass_period = string.format('{%s,"name":"Measurement Period","unique_id":"%s_period","command_topic":"config/%s/set/work_period","options":["continuous","1 min","2 min","3 min","4 min","5 min","6 min","7 min","8 min","9 min","10 min"],"value_template":"{{value_json.period}}","entity_category":"config"}', hass_entity_base, device_id, device_id)
+
+ mqttclient:publish("homeassistant/sensor/" .. device_id .. "/pm2_5/config", hass_pm2_5, 0, 1, function(client)
+ mqttclient:publish("homeassistant/sensor/" .. device_id .. "/pm10/config", hass_pm10, 0, 1, function(client)
+ mqttclient:publish("homeassistant/sensor/" .. device_id .. "/rssi/config", hass_rssi, 0, 1, function(client)
+ mqttclient:publish("homeassistant/select/" .. device_id .. "/work_period/config", hass_period, 0, 1, function(client)
+ client:subscribe("config/" .. device_id .. "/set/work_period", 0, function(client)
+ collectgarbage()
+ setup_client()
+ end)
+ end)
+ end)
+ end)
+ end)
+end
+
delayed_restart:register(20 * 1000, tmr.ALARM_SINGLE, node.restart)
connect_wifi()
diff --git a/sds011.lua b/sds011.lua
index a528720..dc0004f 100644
--- a/sds011.lua
+++ b/sds011.lua
@@ -18,6 +18,8 @@ local c_sleep = 0x00
local c_work = 0x01
local c_workperiod = 0x08
+sds011.work_period = 0
+
function sds011.finish_cmd(cmd)
cmd = cmd .. string.char(0xff, 0xff)
local checksum = 0
@@ -56,6 +58,7 @@ function sds011.set_work_period(period)
if period < 0 or period > 30 then
return
end
+ sds011.work_period = period
local cmd = string.char(c_head, c_id, c_workperiod, c_write, period)
cmd = cmd .. string.char(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
return sds011.finish_cmd(cmd)