Created
December 3, 2019 19:34
-
-
Save kolen/14aea70411d0dff6a96d3f52967b652d to your computer and use it in GitHub Desktop.
Read MH-Z14 CO2 sensor readings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Read MH-Z14 CO2 sensor readings connected via serial port | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem "rubyserial", "0.6.0" | |
end | |
require "rubyserial" | |
s = Serial.new "/dev/tty.wchusbserialfd130" | |
loop do | |
s.write([0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79].pack("c*")) | |
response_b = s.read(9) | |
response = response_b.unpack "CC S> cccc C" | |
pp response | |
concentration = response[2] | |
pp concentration | |
sleep 5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment