diff options
author | Daniel Friesel <derf@finalrewind.org> | 2022-05-15 21:47:18 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2022-05-15 21:47:18 +0200 |
commit | f703ae2fd1cf7f79d8f17e2526df7293de5e2514 (patch) | |
tree | 6b5fe4aa43a5ca837d69be2fb372c90a1717a780 | |
parent | f225645b78e5b09e23990c6efd76b5303ced50a4 (diff) |
Add InfluxDB support
-rw-r--r-- | README.md | 29 | ||||
-rw-r--r-- | init.lua | 43 |
2 files changed, 51 insertions, 21 deletions
@@ -56,14 +56,6 @@ function uart_callback(data) end ``` -See **init.lua** for an example. To use it, you need to create a **config.lua** file with WiFI and MQTT settings: - -```lua -station_cfg.ssid = "..." -station_cfg.pwd = "..." -mqtt_host = "..." -``` - ## SDS011 Configuration API If desired, **sds011.lua** can be used to configure the SDS011 sensor. @@ -78,3 +70,24 @@ Currently, the following commands are supported * `port:write(sds011.set_work_period(period))` * period == 0: continuous operation (about one measurement per second) * 0 < *period* ≤ 30: about one measurement every *period* minutes; fan turned off in-between + +## Application Example + +**init.lua** is an example application with HomeAssistant integration. +To use it, you need to create a **config.lua** file with WiFI and MQTT settings: + +```lua +station_cfg.ssid = "..." +station_cfg.pwd = "..." +mqtt_host = "..." +``` + +Optionally, it can also publish readings to an InfluxDB. +To do so, configure URL and attribute: + +```lua +influx_url = "..." +influx_attr = "..." +``` + +Readings will be stored as `sds011,[influx_attr] pm2_5_ugm3=...,pm10_ugm3=...` @@ -1,12 +1,15 @@ station_cfg = {} -dofile("config.lua") +publishing_mqtt = false +publishing_http = false -delayed_restart = tmr.create() +watchdog = tmr.create() chip_id = string.format("%06X", node.chipid()) device_id = "esp8266_" .. chip_id mqtt_prefix = "sensor/" .. device_id mqttclient = mqtt.Client(device_id, 120) +dofile("config.lua") + print("ESP8266 " .. chip_id) ledpin = 4 @@ -16,16 +19,15 @@ gpio.write(ledpin, 0) sds011 = require("sds011") function log_restart() - print("Network error " .. wifi.sta.status() .. ". Restarting in 20 seconds.") - delayed_restart:start() + print("Network error " .. wifi.sta.status()) end function setup_client() print("Connected") gpio.write(ledpin, 1) - publishing = true + publishing_mqtt = true mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client) - publishing = false + publishing_mqtt = false end) port = softuart.setup(9600, 2, 1) port:on("data", 10, uart_callback) @@ -63,11 +65,28 @@ function uart_callback(data) 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 + local influx_str = string.format("pm2_5_ugm3=%d.%d,pm10_ugm3=%d.%d", pm25i, pm25f, pm10i, pm10f) + if not publishing_mqtt then + watchdog:start(true) + publishing_mqtt = true gpio.write(ledpin, 0) mqttclient:publish(mqtt_prefix .. "/data", json_str, 0, 0, function(client) - publishing = false + publishing_mqtt = false + if influx_url and influx_attr and influx_str then + publish_influx(influx_str) + else + gpio.write(ledpin, 1) + collectgarbage() + end + end) + end +end + +function publish_influx(payload) + if not publishing_http then + publishing_http = true + http.post(influx_url, influx_header, "sds011" .. influx_attr .. " " .. payload, function(code, data) + publishing_http = false gpio.write(ledpin, 1) collectgarbage() end) @@ -93,8 +112,6 @@ function hass_register() local hass_rssi = string.format('{%s,"name":"RSSI","object_id":"%s_rssi","unique_id":"%s_rssi","device_class":"signal_strength","unit_of_measurement":"dBm","value_template":"{{value_json.rssi_dbm}}","entity_category":"diagnostic"}', hass_entity_base, device_id, device_id) local hass_period = string.format('{%s,"name":"Measurement Period","object_id":"%s_period","unique_id":"%s_period","icon":"mdi:clock-outline","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, device_id) - delayed_restart:stop() - 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) @@ -109,7 +126,7 @@ function hass_register() end) end -delayed_restart:register(30 * 1000, tmr.ALARM_SINGLE, node.restart) -delayed_restart:start() +watchdog:register(20 * 60 * 1000, tmr.ALARM_SEMI, node.restart) +watchdog:start() connect_wifi() |