Unverified Commit 102dd007 authored by Daniel Friesel's avatar Daniel Friesel
Browse files

verify checksum, parse automatic baseline correction data

parent ee6a1033
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -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
+5 −5
Original line number Diff line number Diff line
@@ -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()
+19 −18
Original line number Diff line number Diff line
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