Loading README.md +5 −11 Original line number Diff line number Diff line Loading @@ -55,25 +55,19 @@ end ## Application Example **init.lua** is an example application with HomeAssistant integration. It uses oversampling to smoothen readings, and only reports the average of every group of ten readings. To use it, you need to create a **config.lua** file with WiFI and MQTT settings: **init.lua** is an example application with optional HomeAssistant and InfluxDB integration. It uses oversampling to smoothen readings, and only reports the average of every group of ten readings. To use it, you need to create a **config.lua** file with WiFI and MQTT/InfluxDB settings: ```lua station_cfg = {ssid = "...", pwd = "..."} mqtt_host = "..." ``` Optionally, it can also publish readings to InfluxDB. To do so, configure URL and attribute: ```lua influx_url = "..." influx_attr = "..." ``` Readings will be published as `vindriktning[influx_attr] pm2_5_ugm3=%d.%01d`. Both `mqtt_host` and `influx_url` are optional, though it does not make much sense to specify neither. InfluxDB readings will be published as `vindriktning[influx_attr] pm2_5_ugm3=%d.%01d`. So, unless `influx_attr = ''`, it must start with a comma, e.g. `influx_attr = ',device=' .. device_id`. ## Resources Loading init.lua +33 −20 Original line number Diff line number Diff line Loading @@ -4,11 +4,14 @@ publishing_http = false 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") if mqtt_host then mqtt_prefix = "sensor/" .. device_id mqttclient = mqtt.Client(device_id, 120) end print("Vindriktning " .. chip_id) ledpin = 4 Loading @@ -26,10 +29,12 @@ end function setup_client() print("Connected") gpio.write(ledpin, 1) if mqtt_host then publishing_mqtt = true mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client) publishing_mqtt = false end) end port = softuart.setup(9600, nil, 2) port:on("data", 20, uart_callback) end Loading @@ -47,7 +52,11 @@ end function connect_wifi() print("WiFi MAC: " .. wifi.sta.getmac()) print("Connecting to ESSID " .. station_cfg.ssid) if mqtt_host then wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, connect_mqtt) else wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, setup_client) end wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, log_restart) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, log_restart) wifi.setmode(wifi.STATION) Loading @@ -72,6 +81,7 @@ function uart_callback(data) 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 mqtt_host then if not publishing_mqtt then watchdog:start(true) publishing_mqtt = true Loading @@ -86,6 +96,9 @@ function uart_callback(data) end end) end elseif influx_url and influx_attr and influx_str then publish_influx(influx_str) end end end end Loading Loading
README.md +5 −11 Original line number Diff line number Diff line Loading @@ -55,25 +55,19 @@ end ## Application Example **init.lua** is an example application with HomeAssistant integration. It uses oversampling to smoothen readings, and only reports the average of every group of ten readings. To use it, you need to create a **config.lua** file with WiFI and MQTT settings: **init.lua** is an example application with optional HomeAssistant and InfluxDB integration. It uses oversampling to smoothen readings, and only reports the average of every group of ten readings. To use it, you need to create a **config.lua** file with WiFI and MQTT/InfluxDB settings: ```lua station_cfg = {ssid = "...", pwd = "..."} mqtt_host = "..." ``` Optionally, it can also publish readings to InfluxDB. To do so, configure URL and attribute: ```lua influx_url = "..." influx_attr = "..." ``` Readings will be published as `vindriktning[influx_attr] pm2_5_ugm3=%d.%01d`. Both `mqtt_host` and `influx_url` are optional, though it does not make much sense to specify neither. InfluxDB readings will be published as `vindriktning[influx_attr] pm2_5_ugm3=%d.%01d`. So, unless `influx_attr = ''`, it must start with a comma, e.g. `influx_attr = ',device=' .. device_id`. ## Resources Loading
init.lua +33 −20 Original line number Diff line number Diff line Loading @@ -4,11 +4,14 @@ publishing_http = false 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") if mqtt_host then mqtt_prefix = "sensor/" .. device_id mqttclient = mqtt.Client(device_id, 120) end print("Vindriktning " .. chip_id) ledpin = 4 Loading @@ -26,10 +29,12 @@ end function setup_client() print("Connected") gpio.write(ledpin, 1) if mqtt_host then publishing_mqtt = true mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client) publishing_mqtt = false end) end port = softuart.setup(9600, nil, 2) port:on("data", 20, uart_callback) end Loading @@ -47,7 +52,11 @@ end function connect_wifi() print("WiFi MAC: " .. wifi.sta.getmac()) print("Connecting to ESSID " .. station_cfg.ssid) if mqtt_host then wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, connect_mqtt) else wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, setup_client) end wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, log_restart) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, log_restart) wifi.setmode(wifi.STATION) Loading @@ -72,6 +81,7 @@ function uart_callback(data) 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 mqtt_host then if not publishing_mqtt then watchdog:start(true) publishing_mqtt = true Loading @@ -86,6 +96,9 @@ function uart_callback(data) end end) end elseif influx_url and influx_attr and influx_str then publish_influx(influx_str) end end end end Loading