summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.mailmap1
-rw-r--r--.reuse/dep59
-rw-r--r--LICENSES/BSD-2-Clause.txt9
-rw-r--r--LICENSES/CC0-1.0.txt121
-rw-r--r--README.md35
-rw-r--r--firmware/nodemcu-release-13-modules-2022-04-17-19-03-07-integer.binbin0 -> 471040 bytes
-rw-r--r--init.lua41
-rw-r--r--sen5x.lua42
8 files changed, 239 insertions, 19 deletions
diff --git a/.mailmap b/.mailmap
new file mode 100644
index 0000000..c69b0d4
--- /dev/null
+++ b/.mailmap
@@ -0,0 +1 @@
+Birte Kristina Friesel <derf@finalrewind.org>
diff --git a/.reuse/dep5 b/.reuse/dep5
new file mode 100644
index 0000000..1949303
--- /dev/null
+++ b/.reuse/dep5
@@ -0,0 +1,9 @@
+Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+
+Files: init.lua sen5x.lua
+Copyright: 2022-2023 Birte Kristina Friesel
+License: BSD-2-Clause
+
+Files: README.md .gitignore
+Copyright: 2022-2023 Birte Kristina Friesel
+License: CC0-1.0
diff --git a/LICENSES/BSD-2-Clause.txt b/LICENSES/BSD-2-Clause.txt
new file mode 100644
index 0000000..5f662b3
--- /dev/null
+++ b/LICENSES/BSD-2-Clause.txt
@@ -0,0 +1,9 @@
+Copyright (c) <year> <owner>
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/LICENSES/CC0-1.0.txt b/LICENSES/CC0-1.0.txt
new file mode 100644
index 0000000..0e259d4
--- /dev/null
+++ b/LICENSES/CC0-1.0.txt
@@ -0,0 +1,121 @@
+Creative Commons Legal Code
+
+CC0 1.0 Universal
+
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
+ HEREUNDER.
+
+Statement of Purpose
+
+The laws of most jurisdictions throughout the world automatically confer
+exclusive Copyright and Related Rights (defined below) upon the creator
+and subsequent owner(s) (each and all, an "owner") of an original work of
+authorship and/or a database (each, a "Work").
+
+Certain owners wish to permanently relinquish those rights to a Work for
+the purpose of contributing to a commons of creative, cultural and
+scientific works ("Commons") that the public can reliably and without fear
+of later claims of infringement build upon, modify, incorporate in other
+works, reuse and redistribute as freely as possible in any form whatsoever
+and for any purposes, including without limitation commercial purposes.
+These owners may contribute to the Commons to promote the ideal of a free
+culture and the further production of creative, cultural and scientific
+works, or to gain reputation or greater distribution for their Work in
+part through the use and efforts of others.
+
+For these and/or other purposes and motivations, and without any
+expectation of additional consideration or compensation, the person
+associating CC0 with a Work (the "Affirmer"), to the extent that he or she
+is an owner of Copyright and Related Rights in the Work, voluntarily
+elects to apply CC0 to the Work and publicly distribute the Work under its
+terms, with knowledge of his or her Copyright and Related Rights in the
+Work and the meaning and intended legal effect of CC0 on those rights.
+
+1. Copyright and Related Rights. A Work made available under CC0 may be
+protected by copyright and related or neighboring rights ("Copyright and
+Related Rights"). Copyright and Related Rights include, but are not
+limited to, the following:
+
+ i. the right to reproduce, adapt, distribute, perform, display,
+ communicate, and translate a Work;
+ ii. moral rights retained by the original author(s) and/or performer(s);
+iii. publicity and privacy rights pertaining to a person's image or
+ likeness depicted in a Work;
+ iv. rights protecting against unfair competition in regards to a Work,
+ subject to the limitations in paragraph 4(a), below;
+ v. rights protecting the extraction, dissemination, use and reuse of data
+ in a Work;
+ vi. database rights (such as those arising under Directive 96/9/EC of the
+ European Parliament and of the Council of 11 March 1996 on the legal
+ protection of databases, and under any national implementation
+ thereof, including any amended or successor version of such
+ directive); and
+vii. other similar, equivalent or corresponding rights throughout the
+ world based on applicable law or treaty, and any national
+ implementations thereof.
+
+2. Waiver. To the greatest extent permitted by, but not in contravention
+of, applicable law, Affirmer hereby overtly, fully, permanently,
+irrevocably and unconditionally waives, abandons, and surrenders all of
+Affirmer's Copyright and Related Rights and associated claims and causes
+of action, whether now known or unknown (including existing as well as
+future claims and causes of action), in the Work (i) in all territories
+worldwide, (ii) for the maximum duration provided by applicable law or
+treaty (including future time extensions), (iii) in any current or future
+medium and for any number of copies, and (iv) for any purpose whatsoever,
+including without limitation commercial, advertising or promotional
+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
+member of the public at large and to the detriment of Affirmer's heirs and
+successors, fully intending that such Waiver shall not be subject to
+revocation, rescission, cancellation, termination, or any other legal or
+equitable action to disrupt the quiet enjoyment of the Work by the public
+as contemplated by Affirmer's express Statement of Purpose.
+
+3. Public License Fallback. Should any part of the Waiver for any reason
+be judged legally invalid or ineffective under applicable law, then the
+Waiver shall be preserved to the maximum extent permitted taking into
+account Affirmer's express Statement of Purpose. In addition, to the
+extent the Waiver is so judged Affirmer hereby grants to each affected
+person a royalty-free, non transferable, non sublicensable, non exclusive,
+irrevocable and unconditional license to exercise Affirmer's Copyright and
+Related Rights in the Work (i) in all territories worldwide, (ii) for the
+maximum duration provided by applicable law or treaty (including future
+time extensions), (iii) in any current or future medium and for any number
+of copies, and (iv) for any purpose whatsoever, including without
+limitation commercial, advertising or promotional purposes (the
+"License"). The License shall be deemed effective as of the date CC0 was
+applied by Affirmer to the Work. Should any part of the License for any
+reason be judged legally invalid or ineffective under applicable law, such
+partial invalidity or ineffectiveness shall not invalidate the remainder
+of the License, and in such case Affirmer hereby affirms that he or she
+will not (i) exercise any of his or her remaining Copyright and Related
+Rights in the Work or (ii) assert any associated claims and causes of
+action with respect to the Work, in either case contrary to Affirmer's
+express Statement of Purpose.
+
+4. Limitations and Disclaimers.
+
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
+ surrendered, licensed or otherwise affected by this document.
+ b. Affirmer offers the Work as-is and makes no representations or
+ warranties of any kind concerning the Work, express, implied,
+ statutory or otherwise, including without limitation warranties of
+ title, merchantability, fitness for a particular purpose, non
+ infringement, or the absence of latent or other defects, accuracy, or
+ the present or absence of errors, whether or not discoverable, all to
+ the greatest extent permissible under applicable law.
+ c. Affirmer disclaims responsibility for clearing rights of other persons
+ that may apply to the Work or any use thereof, including without
+ limitation any person's Copyright and Related Rights in the Work.
+ Further, Affirmer disclaims responsibility for obtaining any necessary
+ consents, permissions or other rights required for any use of the
+ Work.
+ d. Affirmer understands and acknowledges that Creative Commons is not a
+ party to this document and has no duty or obligation with respect to
+ this CC0 or use of the Work.
diff --git a/README.md b/README.md
index ccfcfed..443a452 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,17 @@
# ESP8266 Lua/NodeMCU module for Sensirion SEN5x PM sensors
-This repository contains a Lua module (`sen5x.lua`) as well as
-MQTT-based HomeAssistant integration (`init.lua`) for **Sensirion SEN5x**
-particulate matter, VOC, and NOx sensors.
+This repository contains an ESP8266 NodeMCU Lua module (`sen5x.lua`) as well as
+MQTT / HomeAssistant / InfluxDB integration example (`init.lua`) for
+**Sensirion SEN5x** I²C particulate matter, VOC, and NOx sensors connected via
+I²C.
## Dependencies
sen5x.lua has been tested with Lua 5.1 on NodeMCU firmware 3.0.1 (Release
202112300746, integer build). It requires the following modules.
-* i2c
* bit
+* i2c
The MQTT HomeAssistant integration in init.lua additionally needs the following
modules.
@@ -19,7 +20,6 @@ modules.
* mqtt
* node
* tmr
-* uart
* wifi
## Setup
@@ -34,7 +34,7 @@ Connect the SEN5x board to your ESP8266/NodeMCU board as follows.
SDA and SCL must have external pull-up resistors to 3V3.
If you use different pins for SDA and SCL, you need to adjust the
-softuart.setup call in the examples provided in this repository to reflect
+i2c.setup call in the examples provided in this repository to reflect
those changes. Keep in mind that some ESP8266 pins must have well-defined logic
levels at boot time and may therefore be unsuitable for SEN5x connection.
@@ -58,7 +58,16 @@ end
function read_data()
if sen5x.read() then
- print(string.format("PM1: %d.%01d µg/m³", sen5x.pm1/10, sen5x.pm1%10))
+ -- Available values depend on sensor type (SEN50/SEN54/SEN55)
+ -- Unsupported readings are nil
+ -- sen5x.pm1 : pm1/10 == PM1.0 concentration [µg/m³]
+ -- sen5x.pm2_5 : pm2_5/10 == PM2.5 concentration [µg/m³]
+ -- sen5x.pm4 : pm4/10 == PM4.0 concentration [µg/m³]
+ -- sen5x.pm10 : pm10/10 == PM10 concentration [µg/m³]
+ -- sen5x.humidity : humidity/100 == Humidity [%]
+ -- sen5x.temperature : temperature/200 == Temperature [°c]
+ -- sen5x.voc : voc/10 == VOC [?]
+ -- sen5x.nox : nox/10 == NOx [?]
end
end
```
@@ -69,8 +78,7 @@ end
To use it, you need to create a **config.lua** file with WiFi and MQTT settings:
```lua
-station_cfg.ssid = "..."
-station_cfg.pwd = "..."
+station_cfg = {ssid = "...", pwd = "..."}
mqtt_host = "..."
```
@@ -82,4 +90,11 @@ influx_url = "..."
influx_attr = "..."
```
-Readings will be stored as `sen5x,[influx_attr] pm1_ugm3=...,pm2_5_ugm3=...,...`
+Readings will be published as `sen5x[influx_attr] pm1_ugm3=%d.%01d,pm2_5_ugm3=%d.%01d,pm4_ugm3=%d.%01d,pm10_ugm3=%d.%01d,humidity_relpercent=%d.%01d,temperature_celsius=%d.%01d,voc=%d.%01d,nox=%d.%01d,`
+(or a subset thereof, depending on whether a SEN50/SEN54/SEN55 is connected).
+So, unless `influx_attr = ''`, it must start with a comma, e.g. `influx_attr = ',device=' .. device_id`.
+
+## Images
+
+![](https://finalrewind.org/projects/esp8266-nodemcu-sen5x/media/preview.jpg)
+![](https://finalrewind.org/projects/esp8266-nodemcu-sen5x/media/hass.png)
diff --git a/firmware/nodemcu-release-13-modules-2022-04-17-19-03-07-integer.bin b/firmware/nodemcu-release-13-modules-2022-04-17-19-03-07-integer.bin
new file mode 100644
index 0000000..25539fe
--- /dev/null
+++ b/firmware/nodemcu-release-13-modules-2022-04-17-19-03-07-integer.bin
Binary files differ
diff --git a/init.lua b/init.lua
index 267e1c9..fa1b53b 100644
--- a/init.lua
+++ b/init.lua
@@ -1,4 +1,3 @@
-station_cfg = {}
publishing_mqtt = false
publishing_http = false
@@ -11,7 +10,7 @@ mqttclient = mqtt.Client(device_id, 120)
dofile("config.lua")
-print("ESP8266 " .. chip_id)
+print("SEN5x " .. chip_id)
ledpin = 4
gpio.mode(ledpin, gpio.OUTPUT)
@@ -27,14 +26,17 @@ end
function setup_client()
print("Connected")
gpio.write(ledpin, 1)
+ local json_str = '{"status":"Initializing"}'
if not sen5x.start() then
- print("SEN5x initialization error")
+ json_str = '{"status":"Initialization failed"}'
end
publishing_mqtt = true
mqttclient:publish(mqtt_prefix .. "/state", "online", 0, 1, function(client)
- publishing_mqtt = false
- push_timer:start()
- prepare_push_data()
+ mqttclient:publish(mqtt_prefix .. "/data", json_str, 0, 0, function(client)
+ publishing_mqtt = false
+ push_timer:start()
+ prepare_push_data()
+ end)
end)
end
@@ -58,6 +60,22 @@ function connect_wifi()
wifi.sta.connect()
end
+function prepare_get_status()
+ if sen5x.prepare_read_status() == false then
+ print("SEN5x error")
+ else
+ local delayed_read_status = tmr.create()
+ delayed_read_status:register(20, tmr.ALARM_SINGLE, get_status)
+ delayed_read_status:start()
+ end
+end
+
+function get_status()
+ if sen5x.read_status() == false then
+ print("SEN5x error")
+ end
+end
+
function prepare_push_data()
if sen5x.prepare_read() == false then
print("SEN5x error")
@@ -106,7 +124,7 @@ function push_data()
json_str = string.format('%s"nox":%d.%01d,', json_str, sen5x.nox/10, sen5x.nox%10)
influx_str = string.format("%snox=%d.%01d,", influx_str, sen5x.nox/10, sen5x.nox%10)
end
- json_str = string.format('%s"rssi_dbm":%d}', json_str, wifi.sta.getrssi())
+ json_str = string.format('%s"status":"%s","rssi_dbm":%d}', json_str, sen5x.status, wifi.sta.getrssi())
influx_str = string.format("%srssi_dbm=%d", influx_str, wifi.sta.getrssi())
if not publishing_mqtt then
@@ -119,6 +137,7 @@ function push_data()
publish_influx(influx_str)
else
gpio.write(ledpin, 1)
+ prepare_get_status()
collectgarbage()
end
end)
@@ -132,6 +151,7 @@ function publish_influx(payload)
http.post(influx_url, influx_header, "sen5x" .. influx_attr .. " " .. payload, function(code, data)
publishing_http = false
gpio.write(ledpin, 1)
+ prepare_get_status()
collectgarbage()
end)
end
@@ -175,14 +195,17 @@ function hass_register()
local hass_humi = string.format('{%s,"name":"Humidity","object_id":"%s_humidity","unique_id":"%s_humidity","device_class":"humidity","unit_of_measurement":"%%","value_template":"{{value_json.humidity_relpercent}}"}', hass_entity_base, device_id, device_id)
table.insert(publish_queue, {"homeassistant/sensor/" .. device_id .. "/humidity/config", hass_humi})
- local hass_voc = string.format('{%s,"name":"VOC","object_id":"%s_voc","unique_id":"%s_voc","unit_of_measurement":"VOC","icon":"mdi:air-filter","value_template":"{{value_json.voc}}"}', hass_entity_base, device_id, device_id)
+ local hass_voc = string.format('{%s,"name":"VOC","object_id":"%s_voc","unique_id":"%s_voc","unit_of_measurement":"counts","icon":"mdi:air-filter","value_template":"{{value_json.voc}}"}', hass_entity_base, device_id, device_id)
table.insert(publish_queue, {"homeassistant/sensor/" .. device_id .. "/voc/config", hass_voc})
end
if product_name == "SEN55" then
- local hass_nox = string.format('{%s,"name":"NOx","object_id":"%s_nox","unique_id":"%s_nox","unit_of_measurement":"NOx","icon":"mdi:molecule","value_template":"{{value_json.nox}}"}', hass_entity_base, device_id, device_id)
+ local hass_nox = string.format('{%s,"name":"NOx","object_id":"%s_nox","unique_id":"%s_nox","unit_of_measurement":"counts","icon":"mdi:molecule","value_template":"{{value_json.nox}}"}', hass_entity_base, device_id, device_id)
table.insert(publish_queue, {"homeassistant/sensor/" .. device_id .. "/nox/config", hass_nox})
end
+
+ local hass_status = string.format('{%s,"name":"Status","object_id":"%s_status","unique_id":"%s_status","icon":"mdi:information","value_template":"{{value_json.status}}","entity_category":"diagnostic"}', hass_entity_base, device_id, device_id)
+ table.insert(publish_queue, {"homeassistant/sensor/" .. device_id .. "/status/config", hass_status})
local hass_rssi = string.format('{%s,"name":"RSSI","object_id":"%s_rssi","unique_id":"%s_rssi","device_class":"signal_strength","unit_of_measurement":"dBm","value_template":"{{value_json.rssi_dbm}}","entity_category":"diagnostic"}', hass_entity_base, device_id, device_id)
table.insert(publish_queue, {"homeassistant/sensor/" .. device_id .. "/rssi/config", hass_rssi})
diff --git a/sen5x.lua b/sen5x.lua
index 13bd972..3e6c7b9 100644
--- a/sen5x.lua
+++ b/sen5x.lua
@@ -2,6 +2,7 @@ local sen5x = {}
local device_address = 0x69
sen5x.bus_id = 0
+sen5x.status = "Initializing"
function sen5x.start()
i2c.start(sen5x.bus_id)
@@ -62,6 +63,47 @@ function sen5x.read()
return true
end
+function sen5x.prepare_read_status()
+ i2c.start(sen5x.bus_id)
+ if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then
+ return false
+ end
+ i2c.write(sen5x.bus_id, {0xd2, 0x06})
+ i2c.stop(sen5x.bus_id)
+ return true
+end
+
+function sen5x.read_status()
+ i2c.start(sen5x.bus_id)
+ if not i2c.address(sen5x.bus_id, device_address, i2c.RECEIVER) then
+ return false
+ end
+ local data = i2c.read(sen5x.bus_id, 6)
+ i2c.stop(sen5x.bus_id)
+ if not sen5x.crc_valid(data, 6) then
+ return false
+ end
+ local status1 = string.byte(data, 1)
+ local status2 = string.byte(data, 5)
+
+ if bit.isset(status1, 5) then
+ sen5x.status = "Fan speed out of range"
+ elseif bit.isset(status1, 3) then
+ sen5x.status = "Fan cleaning active"
+ elseif bit.isset(status2, 7) then
+ sen5x.status = "Gas sensor error"
+ elseif bit.isset(status2, 6) then
+ sen5x.status = "RHT sensor error"
+ elseif bit.isset(status2, 5) then
+ sen5x.status = "Laser failure"
+ elseif bit.isset(status2, 4) then
+ sen5x.status = "Fan failure"
+ else
+ sen5x.status = "OK"
+ end
+ return true
+end
+
function sen5x.prepare_get_product()
i2c.start(sen5x.bus_id)
if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then