LoginSignup
1
0

More than 1 year has passed since last update.

rubyでRaspberry piに繋いだIO dataのCO2センサから値を読み出す。

Last updated at Posted at 2023-06-24

IO dataのUSB接続CO2センサが安売りされているので買ってデータを取ってみた。
IO data CO2センサ UD-CO2S
USBで接続するとシリアルポートに繋がっているようにみえ、ポートに接続するとデータが流れてくるものなので、そうくろうなく値を読み出すことができる。
どこに繋がったか調べてみると、/dev/ttyACM0 に繋がっているので、

ruby ioDataCo2.ruby
#! /usr/bin/env ruby
require 'serialport'

class IOdata_CO2
  attr_reader :co2
  def initialize (port = '/dev/ttyACM0')
    @port = port
    #puts @port
  end
  def co2
    #puts @port
    sp = SerialPort.open(@port, 9600,8,1,0)
    sp.write "STA\r\n"
    sp.read_timeout=2500
    data = sp.read
    @co2 = String.new
    data.each_line {|line|
      #puts line
      if line[0..2] == "CO2" then
        @co2 = line[4.. line.index(",")-1]
        break
      end
      }
    sp.write "STP" +"\r\n"
    sp.close
    #puts co2
    return @co2
  end
end

data = IOdata_CO2.new('/dev/ttyACM0')
puts data.co2

温度や湿度も得られるけど、これはセンサの測定値補正用のものなので室温より高め。
室温が欲しければ別途室温センサ使ったほうがいいですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0