From 9bc9955e2c8436dfee063b0c1106c6a07838a961 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 8 Jul 2024 10:49:56 +0100 Subject: Add sds.rb --- README | 2 ++ sds.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 sds.rb diff --git a/README b/README index 7b963dc..4eb937a 100644 --- a/README +++ b/README @@ -27,6 +27,8 @@ pdfbook4.rb: Page order to make (or pdfjam command to turn a pdf file into) 4-up double-sided brochure ordered pages (to make two A6 folia per A4 page) scan.sh: Batch scanning script +sds.rb: + Read streaming data from an SDS011 section.c: Like the section command on IOS slide.rb: diff --git a/sds.rb b/sds.rb new file mode 100755 index 0000000..c28d508 --- /dev/null +++ b/sds.rb @@ -0,0 +1,66 @@ +#!/usr/bin/env ruby + +class Rdr + def initialize(f) + @f = f + reset + end + + def valid? + @len == 10# && @csok + end + + def read! + v = @f.read(1) + @len += 1 + + case @len + when 1 + reset if v != 0xAA + when 2 + reset if v != 0xC0 + when 3 + @pm25 = v + when 4 + @pm25 += v * 256 + when 5 + @pm10 = v + when 6 + @pm10 += v * 256 + when 7 + @id = v + when 8 + @id += v * 256 + when 9 + if checksum == v + @csok = true + else + reset + end + when 10 + reset if v != 0xAB + else + reset + end + end + + def checksum + ((@pm25 / 256) + (@pm25 % 256) + + (@pm10 / 256) + (@pm10 % 256) + + (@id / 256) + (@id % 256)) % 256 + end + + def reset + @len = 0 + @pm25 = 0 + @pm10 = 0 + @id = 0 + @csok = false + end +end + +File.open("/dev/ttyUSB0", "rb") do |f| + r = Rdr.new(f) + r.read! until r.valid? + puts "Hooray" +end -- cgit v1.2.1