summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md16
-rw-r--r--init.lua45
2 files changed, 37 insertions, 24 deletions
diff --git a/README.md b/README.md
index 4fa8b73..a1c2485 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,8 @@ Most practical applications (such as the example in init.lua) also need the
following modules.
* gpio
-* mqtt
+* http (for InfluxDB integration)
+* mqtt (for HomeAssistant / MQTT integration)
* node
* softuart
* tmr
@@ -102,23 +103,18 @@ If desired, **sds011.lua** can be used to configure the SDS011 sensor.
## 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:
+**init.lua** is an example application with optional HomeAssistant and InfluxDB integration.
+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 `sds011[influx_attr] pm2_5_ugm3=%d.%01d,pm10_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 `sds011[influx_attr] pm2_5_ugm3=%d.%01d,pm10_ugm3=%d.%01d`.
So, unless `influx_attr = ''`, it must start with a comma, e.g. `influx_attr = ',device=' .. device_id`.
## Images
diff --git a/init.lua b/init.lua
index 4708feb..523ff68 100644
--- a/init.lua
+++ b/init.lua
@@ -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("SDS011 " .. chip_id)
ledpin = 4
@@ -29,11 +32,15 @@ function setup_client()
gpio.write(ledpin, 1)
port = softuart.setup(9600, 2, 1)
port:on("data", 10, uart_callback)
- publishing_mqtt = true
- mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client)
- publishing_mqtt = false
- port:write(sds011.set_work_period(nil))
- end)
+ if mqtt_host then
+ publishing_mqtt = true
+ mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client)
+ publishing_mqtt = false
+ port:write(sds011.set_work_period(nil))
+ end)
+ else
+ port:write(sds011.set_work_period(0))
+ end
end
function connect_mqtt()
@@ -49,7 +56,11 @@ end
function connect_wifi()
print("WiFi MAC: " .. wifi.sta.getmac())
print("Connecting to ESSID " .. station_cfg.ssid)
- wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, connect_mqtt)
+ 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)
@@ -67,6 +78,8 @@ function uart_callback(data)
work_period = string.format("%d min", sds011.work_period)
end
+ gpio.write(ledpin, 0)
+
if sds011.work_period == 0 and not polling then
polling = true
port:write(sds011.set_report_mode(false))
@@ -86,11 +99,11 @@ function uart_callback(data)
end
json_str = json_str .. '}'
- 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)
+ if mqtt_host then
+ if not publishing_mqtt then
+ watchdog:start(true)
+ publishing_mqtt = true
+ mqttclient:publish(mqtt_prefix .. "/data", json_str, 0, 0, function(client)
publishing_mqtt = false
if influx_url and influx_attr and influx_str then
publish_influx(influx_str)
@@ -98,7 +111,10 @@ function uart_callback(data)
gpio.write(ledpin, 1)
collectgarbage()
end
- end)
+ end)
+ end
+ elseif influx_url and influx_attr and influx_str then
+ publish_influx(influx_str)
end
end
@@ -106,6 +122,7 @@ 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)
+ print("POST " .. influx_url .. " sds011" .. influx_attr .. " " .. payload .. " returned " .. code)
publishing_http = false
gpio.write(ledpin, 1)
collectgarbage()