diff options
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | src/init.lua | 60 |
2 files changed, 44 insertions, 27 deletions
@@ -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 @@ -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 @@ -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' ``` diff --git a/src/init.lua b/src/init.lua index 00ce3b0..28fc9a2 100644 --- a/src/init.lua +++ b/src/init.lua @@ -1,10 +1,13 @@ 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") @@ -89,26 +92,36 @@ 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 - publish_count = 0 - publishing_mqtt = true - gpio.write(ledpin, 0) + 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) - 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) + 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) + 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 + publishing_http = true + http.post(influx_url, influx_header, "mh_z19" .. influx_attr .. " " .. influx_str, function(code, data) + gpio.write(ledpin, 1) + publishing_http = false + collectgarbage() + end) + else gpio.write(ledpin, 1) - publishing_http = false collectgarbage() - end) - else + 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 end end @@ -116,11 +129,14 @@ function wifi_connected() print("IP address: " .. wifi.sta.getip()) have_wifi = true no_wifi_count = 0 - 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) + + 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() |