Loading README.md +6 −5 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ It can also publish readings to InfluxDB. ## Features * Display for CO₂ and temperature * Home Assistant integration via MQTT * Optional Home Assistant integration via MQTT * Optional logging to InfluxDB * Powered via USB Loading Loading @@ -60,8 +60,9 @@ station_cfg = {ssid = "foo", pwd = "bar"} ### MQTT The only configurable entity is the hostname of the MQTT broker. The ESP8266 will register itself as `homeassistant/sensor/esp8266_XXXXXX` with the last six This setting is optional. Specify the hostname of an MQTT broker in order to enable MQTT publishing and Home Assistant integration. The ESP8266 will register itself as `homeassistant/sensor/esp8266_XXXXXX` with the last six digits representing its WiFi MAC address. ```lua Loading @@ -73,10 +74,10 @@ mqtt_host = "mqtt.example.org" These settings are optional. Specify a URL and attributes in order to enable InfluxDB publishing. For instance, if measurements should be stored as `mh_z19,location=lounge` in the `sensors` database on `https://influxdb.example.org`, the configuration is as follows. `http://influxdb.example.org:8086`, the configuration is as follows. ```lua influx_url = 'https://influxdb.example.org/write?db=sensors' influx_url = 'http://influxdb.example.org:8086/write?db=sensors' influx_attr = ',location=lounge' ``` Loading src/init.lua +38 −22 Original line number Diff line number Diff line 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 i2c.setup(0, 5, 6, i2c.SLOW) ssd1306 = require("ssd1306") fn = require("terminus16") Loading Loading @@ -89,15 +92,16 @@ function uart_callback(data) fb.init(128, 32) collectgarbage() publish_count = publish_count + 1 if have_wifi and publish_count >= 4 and not publishing_mqtt then if have_wifi and publish_count >= 4 then local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":%d,"temperature_celsius":%d}', wifi.sta.getrssi(), mh_z19.co2, mh_z19.temp) local influx_str = string.format("co2_ppm=%d,temperature_celsius=%d,abc_ticks=%d,abc_count=%d", mh_z19.co2, mh_z19.temp, mh_z19.abc_ticks, mh_z19.abc_count) publish_count = 0 if mqtt_host and not publishing_mqtt then publishing_mqtt = true gpio.write(ledpin, 0) local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":%d,"temperature_celsius":%d}', wifi.sta.getrssi(), mh_z19.co2, mh_z19.temp) mqttclient:publish(mqtt_prefix .. "/data", json_str, 0, 0, function(client) publishing_mqtt = false if have_wifi and influx_url and not publishing_http then local influx_str = string.format("co2_ppm=%d,temperature_celsius=%d,abc_ticks=%d,abc_count=%d", mh_z19.co2, mh_z19.temp, mh_z19.abc_ticks, mh_z19.abc_count) publishing_http = true http.post(influx_url, influx_header, "mh_z19" .. influx_attr .. " " .. influx_str, function(code, data) gpio.write(ledpin, 1) Loading @@ -109,6 +113,15 @@ function uart_callback(data) collectgarbage() end end) elseif influx_url and not publishing_http then publishing_http = true gpio.write(ledpin, 0) http.post(influx_url, influx_header, "mh_z19" .. influx_attr .. " " .. influx_str, function(code, data) gpio.write(ledpin, 1) publishing_http = false collectgarbage() end) end end end Loading @@ -116,12 +129,15 @@ function wifi_connected() print("IP address: " .. wifi.sta.getip()) have_wifi = true no_wifi_count = 0 if mqtt_host then print("Connecting to MQTT " .. mqtt_host) mqttclient:on("connect", hass_register) mqttclient:on("offline", wifi_err) mqttclient:lwt(mqtt_prefix .. "/state", "offline", 0, 1) mqttclient:connect(mqtt_host) end end function wifi_err() have_wifi = false Loading Loading
README.md +6 −5 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ It can also publish readings to InfluxDB. ## Features * Display for CO₂ and temperature * Home Assistant integration via MQTT * Optional Home Assistant integration via MQTT * Optional logging to InfluxDB * Powered via USB Loading Loading @@ -60,8 +60,9 @@ station_cfg = {ssid = "foo", pwd = "bar"} ### MQTT The only configurable entity is the hostname of the MQTT broker. The ESP8266 will register itself as `homeassistant/sensor/esp8266_XXXXXX` with the last six This setting is optional. Specify the hostname of an MQTT broker in order to enable MQTT publishing and Home Assistant integration. The ESP8266 will register itself as `homeassistant/sensor/esp8266_XXXXXX` with the last six digits representing its WiFi MAC address. ```lua Loading @@ -73,10 +74,10 @@ mqtt_host = "mqtt.example.org" These settings are optional. Specify a URL and attributes in order to enable InfluxDB publishing. For instance, if measurements should be stored as `mh_z19,location=lounge` in the `sensors` database on `https://influxdb.example.org`, the configuration is as follows. `http://influxdb.example.org:8086`, the configuration is as follows. ```lua influx_url = 'https://influxdb.example.org/write?db=sensors' influx_url = 'http://influxdb.example.org:8086/write?db=sensors' influx_attr = ',location=lounge' ``` Loading
src/init.lua +38 −22 Original line number Diff line number Diff line 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 i2c.setup(0, 5, 6, i2c.SLOW) ssd1306 = require("ssd1306") fn = require("terminus16") Loading Loading @@ -89,15 +92,16 @@ function uart_callback(data) fb.init(128, 32) collectgarbage() publish_count = publish_count + 1 if have_wifi and publish_count >= 4 and not publishing_mqtt then if have_wifi and publish_count >= 4 then local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":%d,"temperature_celsius":%d}', wifi.sta.getrssi(), mh_z19.co2, mh_z19.temp) local influx_str = string.format("co2_ppm=%d,temperature_celsius=%d,abc_ticks=%d,abc_count=%d", mh_z19.co2, mh_z19.temp, mh_z19.abc_ticks, mh_z19.abc_count) publish_count = 0 if mqtt_host and not publishing_mqtt then publishing_mqtt = true gpio.write(ledpin, 0) local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":%d,"temperature_celsius":%d}', wifi.sta.getrssi(), mh_z19.co2, mh_z19.temp) mqttclient:publish(mqtt_prefix .. "/data", json_str, 0, 0, function(client) publishing_mqtt = false if have_wifi and influx_url and not publishing_http then local influx_str = string.format("co2_ppm=%d,temperature_celsius=%d,abc_ticks=%d,abc_count=%d", mh_z19.co2, mh_z19.temp, mh_z19.abc_ticks, mh_z19.abc_count) publishing_http = true http.post(influx_url, influx_header, "mh_z19" .. influx_attr .. " " .. influx_str, function(code, data) gpio.write(ledpin, 1) Loading @@ -109,6 +113,15 @@ function uart_callback(data) collectgarbage() end end) elseif influx_url and not publishing_http then publishing_http = true gpio.write(ledpin, 0) http.post(influx_url, influx_header, "mh_z19" .. influx_attr .. " " .. influx_str, function(code, data) gpio.write(ledpin, 1) publishing_http = false collectgarbage() end) end end end Loading @@ -116,12 +129,15 @@ function wifi_connected() print("IP address: " .. wifi.sta.getip()) have_wifi = true no_wifi_count = 0 if mqtt_host then print("Connecting to MQTT " .. mqtt_host) mqttclient:on("connect", hass_register) mqttclient:on("offline", wifi_err) mqttclient:lwt(mqtt_prefix .. "/state", "offline", 0, 1) mqttclient:connect(mqtt_host) end end function wifi_err() have_wifi = false Loading