Skip to content

Instantly share code, notes, and snippets.

@kolen
Created December 3, 2019 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolen/14aea70411d0dff6a96d3f52967b652d to your computer and use it in GitHub Desktop.
Save kolen/14aea70411d0dff6a96d3f52967b652d to your computer and use it in GitHub Desktop.
Read MH-Z14 CO2 sensor readings
#!/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