2015/02/27

Raspberry Piのによる気圧お知らせ

I2Cで気圧センサ(MPL115A2)と接続できたので、気圧の変動によってtwitterで
つぶやくようにしました。

Rubyによるtwitter接続登録

ここに詳しくプログラムによる接続手続きが書かれているので、そちらを見てください。(^^;
簡単に書くと、twitterの開発者向けサイトにログインして、トークンを得るために
アプリケーション名とかを記入します。すると、トークンが表示されますので、コピー
してプログラム中に挿入します。
サンプルプログラムでHello worldが表示できます。

Arduinoプログラムの移植

Arduino + MPL115A2の組み合わせの時のプログラムがありましたので、それをRuby用に
移植します。
そこにtwitterにつぶやく一文も追加しました。

#!/usr/bin/env ruby
#coding: utf-8

require "date"
require "twitter"

class I2CDevice
  # ioctl command
  # Ref. https://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/linux/i2c.h
  I2C_RETRIES = 0x0701
  I2C_TIMEOUT = 0x0702
  I2C_SLAVE = 0x0703
  I2C_SLAVE_FORCE = 0x0706
  I2C_TENBIT = 0x0704
  I2C_FUNCS = 0x0705
  I2C_RDWR = 0x0707
  I2C_SMBUS = 0x0720
  I2C_UDELAY = 0x0705
  I2C_MDELAY = 0x0706

  attr_accessor :address
  def initialize(address)
    @address = address
  end

  def i2cget(address, length=1)
    i2c = File.open("/dev/i2c-1", "r+")
    i2c.ioctl(I2C_SLAVE, @address)
    i2c.write(address.chr)
    ret = i2c.read(length)
    i2c.close
    ret
  end

  def i2cset(*data)
    i2c = File.open("/dev/i2c-1", "r+")
    i2c.ioctl(I2C_SLAVE, @address)
    i2c.write(data.pack("C*"))
    i2c.close
  end
end

class MPL115A2 < I2CDevice
  def initialize
    super(0x60)

    coefficient = i2cget(0x04, 8).unpack("n*")

    @a0 = fixed_point(coefficient[0], 12)
    @b1 = fixed_point(coefficient[1], 2)
    @b2 = fixed_point(coefficient[2], 1)
    @c12 = fixed_point(coefficient[3], 0) / (1<<9)
    p [@a0, @b1, @b2, @c12]
  end

  def fixed_point(fixed, int_bits)
    msb = 15
    deno = (1<<(msb-int_bits)).to_f
    if (fixed & (1<<15)).zero?
      fixed / deno
    else
      -( ( (~fixed & 0xffff) + 1) / deno )
    end
  end

  def calculate_hPa
    i2cset(0x12, 0x01) # CONVERT

    sleep 0.003

    data = i2cget(0x00, 4).unpack("n*")

    p_adc = (data[0]) >> 6
    t_adc = (data[1]) >> 6

    p_comp = @a0 + (@b1 + @c12 * t_adc) * p_adc + @b2 * t_adc
    hPa = p_comp * ( (1150 - 500) / 1023.0) + 500;
  end
end

mpl = MPL115A2.new
loopflag = 0 #周回フラグ
press = [0,0,0,0,0,0,0,0,0,0,0,0,0] #気圧データ保存配列
tcount = 0 #3時間計

# Twitter setup
client = Twitter::REST::Client.new do |config|
  config.consumer_key = "******"
  config.consumer_secret = "******"
  config.access_token        = "******"
  config.access_token_secret = "******"
end

loop do
  sec_total = 0
  min_total = 0

  for min_j in 0..14 do
    for sec_i in 0..14 do
      sec_total = sec_total + mpl.calculate_hPa
      sleep 1
    end
    onemin_ave = sec_total / 15
    min_total = min_total + onemin_ave
    sec_total = 0
    sleep 45
  end
  press[tcount] = min_total / 15
  print("tcount = ", tcount, "pressure = ", min_total/15, "\n")

  if tcount > 0  then
    day = Time.now
    daystr = day.strftime("%Y/%m/%d %H:%M:%S ")

    dpdt = (press[tcount] - press[0]) / (tcount * (loopflag + 0.25) * 0.635)
    printf("pressure = %f, dP/dt = %f ,", press[tcount], dpdt)
    if 2.5 <= dpdt then
      printf("急速に気圧が上昇しています \n")
      mess = "急速に気圧が上昇しています。 @横浜南"
      message = daystr + mess
      client.update(message)
    end
    if 0.5 < dpdt && dpdt < 2.5 then
      printf("気圧が緩やかに上昇しています。 \n")
      mess = "気圧が緩やかに上昇しています。 @横浜南"
      message = daystr + mess
      client.update(message)
    end
    if -0.5 <= dpdt && dpdt <= 0.5 then
      printf("気圧の変化はありません。 \n")
    end
    if -2.5 < dpdt && dpdt < -0.5 then
      printf("気圧が緩やかに下降しています。 \n")
      mess = "気圧が緩やかに下降しています。 @横浜南"
      message = daystr + mess
      client.update(message)
    end
    if dpdt <= -2.5 then
      printf("急速に気圧が落ちています。 \n")
      mess = "急速に気圧が落ちています。突然の雨に気をつけてください。 @横浜南"
      message = daystr + mess
      client.update(message)
    end

    if tcount == 8 then
      press[0] = press[4]
    end

    if tcount >= 12 then
      loopflag = 1
      tcount = 1
    else
      tcount += 1
    end

  else
    tcount += 1
  end
end

で、動作させますと、このようにつぶやきます。

今回はつぶやきまで、ということで終わりにしますが、もう少しネタがあるので
頃合いをみて続きます。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。