From a07247be87a77b6cc2f90c71b371bbf60766ef26 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 18 Apr 2022 18:11:25 +0200 Subject: initial commit --- pm1006.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 pm1006.lua (limited to 'pm1006.lua') diff --git a/pm1006.lua b/pm1006.lua new file mode 100644 index 0000000..1aeed17 --- /dev/null +++ b/pm1006.lua @@ -0,0 +1,20 @@ +local pm1006 = {} + +function pm1006.parse_frame(data) + if string.byte(data, 1) ~= 0x16 or string.byte(data, 2) ~= 0x11 or string.byte(data, 3) ~= 0x0b then + -- invalid header + return nil + end + local checksum = 0 + for i = 1, 20 do + checksum = (checksum + string.byte(data, i)) % 256 + end + if checksum ~= 0 then + -- invalid checksum + return nil + end + local pm2_5 = string.byte(data, 6) * 256 + string.byte(data, 7) + return pm2_5 +end + +return pm1006 -- cgit v1.2.3