Unverified Commit 992770b4 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Make MQTT integration optional

parent 6b94484c
Loading
Loading
Loading
Loading
+6 −10
Original line number Diff line number Diff line
@@ -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
+31 −14
Original line number Diff line number Diff line
@@ -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)
	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)
	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,10 +99,10 @@ function uart_callback(data)
	end
	json_str = json_str .. '}'

	if mqtt_host then
		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_mqtt = false
			if influx_url and influx_attr and influx_str then
@@ -100,12 +113,16 @@ function uart_callback(data)
			end
			end)
		end
	elseif influx_url and influx_attr and influx_str then
		publish_influx(influx_str)
	end
end

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()