summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-05-15 21:34:47 +0200
committerDaniel Friesel <derf@finalrewind.org>2022-05-15 21:34:47 +0200
commit060063f2d428903c073036876dad6dc6195576aa (patch)
treed04ea17b28706d671c9df3ea9c9d096154dd861a
parentf6d4dcb42da4901944da1bc5d96c9ec6fbfbe134 (diff)
Add InfluxDB support
-rw-r--r--README.md14
-rw-r--r--init.lua48
2 files changed, 46 insertions, 16 deletions
diff --git a/README.md b/README.md
index e592ccb..e37a46e 100644
--- a/README.md
+++ b/README.md
@@ -45,11 +45,23 @@ function uart_callback(data)
end
end
```
+## Application Example
-See **init.lua** for an example. To use it, you need to create a **config.lua** file with WiFI and MQTT settings:
+**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:
+
+```
+influx_url = "..."
+influx_attr = "..."
+```
+
+Readings will be stored as `vindriktning,[influx_attr] pm2_5_ugm3=...`
diff --git a/init.lua b/init.lua
index f5b19ee..35d87e9 100644
--- a/init.lua
+++ b/init.lua
@@ -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
@@ -18,16 +21,15 @@ pm1006 = require("pm1006")
pm_values = {}
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, nil, 2)
port:on("data", 20, uart_callback)
@@ -37,6 +39,7 @@ function connect_mqtt()
print("IP address: " .. wifi.sta.getip())
print("Connecting to MQTT " .. mqtt_host)
mqttclient:on("connect", hass_register)
+ mqttclient:on("connfail", log_restart)
mqttclient:on("offline", log_restart)
mqttclient:lwt(mqtt_prefix .. "/state", "offline", 0, 1)
mqttclient:connect(mqtt_host)
@@ -68,28 +71,43 @@ function uart_callback(data)
local pm_int = pm_sum / 10
local pm_frac = pm_sum % 10
local json_str = string.format('{"pm2_5_ugm3": %d.%d, "rssi_dbm": %d}', pm_int, pm_frac, wifi.sta.getrssi())
+ local influx_str = string.format("pm2_5_ugm3=%d.%d", pm_int, pm_frac)
pm_values = {}
- if not publishing then
- publishing = true
+ 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
- gpio.write(ledpin, 1)
- collectgarbage()
+ 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
end
end
+function publish_influx(payload)
+ if not publishing_http then
+ publishing_http = true
+ http.post(influx_url, influx_header, "vindriktning" .. influx_attr .. " " .. payload, function(code, data)
+ publishing_http = false
+ gpio.write(ledpin, 1)
+ collectgarbage()
+ end)
+ end
+end
+
function hass_register()
local hass_device = string.format('{"connections":[["mac","%s"]],"identifiers":["%s"],"model":"ESP8266 + PM1006","name":"Vindriktning %s","manufacturer":"derf"}', wifi.sta.getmac(), device_id, chip_id)
local hass_entity_base = string.format('"device":%s,"state_topic":"%s/data","expire_after":90', hass_device, mqtt_prefix)
local hass_pm2_5 = string.format('{%s,"name":"PM2.5","object_id":"%s_pm2_5","unique_id":"%s_pm2_5","device_class":"pm25","unit_of_measurement":"µg/m³","value_template":"{{value_json.pm2_5_ugm3}}"}', hass_entity_base, device_id, device_id)
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)
- 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 .. "/rssi/config", hass_rssi, 0, 1, function(client)
collectgarbage()
@@ -98,7 +116,7 @@ function hass_register()
end)
end
-delayed_restart:register(30 * 1000, tmr.ALARM_SINGLE, node.restart)
-delayed_restart:start()
+watchdog:register(120 * 1000, tmr.ALARM_SEMI, node.restart)
+watchdog:start()
connect_wifi()