summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-05-26 07:52:52 +0200
committerDaniel Friesel <derf@finalrewind.org>2022-05-26 07:53:05 +0200
commit102dd0072d34fa081f5861148be61c0c3739827c (patch)
tree301fe5c2bb61d06da0db3e45cf573509a85e5fd3
parentee6a1033c293f3d3b327568fe9554d4651c4c964 (diff)
verify checksum, parse automatic baseline correction data
-rw-r--r--README.md8
-rw-r--r--init.lua10
-rw-r--r--mh-z19.lua37
3 files changed, 28 insertions, 27 deletions
diff --git a/README.md b/README.md
index 84bd62c..f6fed93 100644
--- a/README.md
+++ b/README.md
@@ -41,17 +41,17 @@ levels at boot time and may therefore be unsuitable for MH-Z19 connection.
Copy **mh-z19.lua** to your NodeMCU board and set it up as follows.
```lua
-mhz19 = require("mh-z19")
+mh_z19 = require("mh-z19")
port = softuart.setup(9600, 1, 2)
port:on("data", 9, uart_callback)
function uart_callback(data)
- if sds011.parse_frame(data) then
- -- mhz19.co2 contains the CO₂ concentration in ppm
+ if mh_z19.parse_frame(data) then
+ -- mh_z19.co2 contains the CO₂ concentration in ppm
end
end
-port:write(mhz19.query())
+port:write(mhz19.c_query)
```
## Application Example
diff --git a/init.lua b/init.lua
index 76b8225..b6d7175 100644
--- a/init.lua
+++ b/init.lua
@@ -16,7 +16,7 @@ ledpin = 4
gpio.mode(ledpin, gpio.OUTPUT)
gpio.write(ledpin, 0)
-mhz19 = require("mh-z19")
+mh_z19 = require("mh-z19")
poll = tmr.create()
@@ -58,13 +58,13 @@ function connect_wifi()
end
function uart_callback(data)
- if not mhz19.parse_frame(data) then
+ if not mh_z19.parse_frame(data) then
print("Invalid MH-Z19 frame")
return
end
- local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":"%d"}', wifi.sta.getrssi(), mhz19.co2)
- local influx_str = string.format("co2_ppm=%d", mhz19.co2)
+ local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":"%d"}', wifi.sta.getrssi(), mh_z19.co2)
+ local influx_str = string.format("co2_ppm=%d", mh_z19.co2)
if not publishing_mqtt then
watchdog:start(true)
@@ -94,7 +94,7 @@ function publish_influx(payload)
end
function query_data()
- port:write(mhz19.query())
+ port:write(mh_z19.c_query)
end
function hass_register()
diff --git a/mh-z19.lua b/mh-z19.lua
index fe269b8..0b0a53e 100644
--- a/mh-z19.lua
+++ b/mh-z19.lua
@@ -1,28 +1,29 @@
-local mhz19 = {}
+local mh_z19 = {}
-local c_read = string.char(0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79)
+mh_z19.c_query = string.char(0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79)
-mhz19.co2 = nil
-mhz19.temp = nil
-mhz19.tt = nil
-mhz19.ss = nil
-mhz19.uu = nil
+mh_z19.co2 = nil
+mh_z19.temp = nil
+mh_z19.abc_count = nil
+mh_z19.abc_ticks = nil
-function mhz19.query()
- return c_read
+function checksum(b1, b2, b3, b4, b5, b6, b7)
+ return 0xff - ((b1 + b2 + b3 + b4 + b5 + b6 + b7) % 0x100) + 1
end
-function mhz19.parse_frame(data)
- local head, cmd, co2h, co2l, temp, tt, ss, uh, ul = struct.unpack("BBBBBBBBB", data)
- if head ~= 0xff or cmd ~= 0x86 then
+function mh_z19.parse_frame(data)
+ local start, cmd, co2h, co2l, temp, u1, abc_ticks, abc_count, cksum = struct.unpack("BBBBBBBBB", data)
+ if start ~= 0xff or cmd ~= 0x86 then
return false
end
- mhz19.co2 = co2h * 256 + co2l
- mhz19.temp = temp - 40
- mhz19.tt = tt
- mhz19.ss = ss
- mhz19.uu = uh * 256 + ul
+ if checksum(cmd, co2h, co2l, temp, u1, abc_ticks, abc_count) ~= cksum then
+ return false
+ end
+ mh_z19.co2 = co2h * 256 + co2l
+ mh_z19.temp = temp - 40
+ mh_z19.abc_ticks = abc_ticks
+ mh_z19.abc_count = abc_count
return true
end
-return mhz19
+return mh_z19