Hatena::ブログ(Diary)

toshi_hirasawaの日記

2013-05-02

温度センサ(TMP102)と気圧センサ(MPL115A2)をつかってみた

| 18:36

root@raspi-82:~/raspi/dledFromBlogs# i2cdetect -y 1  
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
root@raspi-82:~/raspi/dledFromBlogs#

こちらを参考にさせていただきました。

気圧の計算などめんどくさー、な部分はこちらからスクリプとをDL.(ありがたや)

http://machide.blog98.fc2.com/blog-date-20120923.html

MPL115A2仕様の翻訳(ありがたや)

http://www.ne.jp/asahi/o-family/extdisk/MPL115A2cnt/MPL115A2m_jp.pdf

TMP102によれば ただいま21.3度

root@raspi-82:~/raspi/TempAndPressure# cat ./tmp102.sh
#!/bin/bash

hexraw=$(sudo i2cget -y 1 0x48 0x00 w)
while [ "$hexraw" == "" ]; do
    hexraw=$(sudo i2cget -y 1 0x48 0x00 w)
done
#echo "$hexraw"

msb=$(echo ${hexraw:4:2})
lsb=$(echo ${hexraw:2:1})
#echo "0x$msb$lsb"

dec=$(printf "%d\n" "0x$msb$lsb")

echo "scale=4; $dec*0.0625"| bc
root@raspi-82:~/raspi/TempAndPressure# ./tmp102.sh 
21.3125
root@raspi-82:~/raspi/TempAndPressure# 

MPL115A2によるとただいま998ヘクトパスカル


root@raspi-82:~/raspi/dledFromBlogs# cat ./MPL115A2.sh 
#!/bin/bash

sudo i2cset -y 1 0x60 0x12 0x01

perl -e 'select(undef, undef, undef, 0.1);'

#----- padc -----#

address_0=$(sudo i2cget -y 1 0x60 0x00 w)
while [ "$address_0" == "" ]; do
    address_0=$(sudo i2cget -y 1 0x60 0x00 w)
done

msb_0=$(echo ${address_0:4:2})
msb_1=$(echo ${address_0:2:2})
#echo "0x$msb_0$msb_1"

dec_padc=$(printf "%d\n" "0x$msb_0$msb_1")
padc=$(echo "scale=0; $dec_padc/64"| bc)
echo "Padc = $padc"


#----- tadc -----#

address_2=$(sudo i2cget -y 1 0x60 0x02 w)
while [ "$address_2" == "" ]; do
    address_2=$(sudo i2cget -y 1 0x60 0x02 w)
done

msb_2=$(echo ${address_2:4:2})
msb_3=$(echo ${address_2:2:2})
#echo "0x$msb_2$msb_3"

dec_tadc=$(printf "%d\n" "0x$msb_2$msb_3")
tadc=$(echo "scale=0; $dec_tadc/64"| bc)
echo "Tadc = $tadc"


#----- a0 -----#

address_4=$(sudo i2cget -y 1 0x60 0x04 w)
while [ "$address_4" == "" ]; do
    address_4=$(sudo i2cget -y 1 0x60 0x04 w)
done

msb_4=$(echo ${address_4:4:2})
msb_5=$(echo ${address_4:2:2})
#echo "0x$msb_4$msb_5"

a0_dec=$(printf "%d\n" "0x$msb_4$msb_5")
a0_bin=$(echo "obase=2; ibase=10; $a0_dec"| bc)
a0_bin16=$(printf "%016d\n" "$a0_bin")
#echo "$a0_bin16"

a0_sign=$(echo ${a0_bin16:0:1})
a0_integer=$(echo ${a0_bin16:1:12})
a0_fractional=$(echo ${a0_bin16:13:3})
#echo "$a0_sign"
#echo "$a0_integer"
#echo "$a0_fractional"

a0_sign_dec=$(echo ${a0_sign/1/-1})
a0_sign_dec2=$(echo ${a0_sign_dec/0/1})  
a0_integer_dec=$(echo "obase=10; ibase=2; $a0_integer"| bc)
a0_1=$(echo ${a0_fractional:0:1})
a0_2=$(echo ${a0_fractional:1:1})
a0_3=$(echo ${a0_fractional:2:1})
a0_fractional_dec=$(echo "scale=3; $a0_1/2+$a0_2/4+$a0_3/8"| bc)
a0=$(echo "scale=3; $a0_sign_dec2*($a0_integer_dec+$a0_fractional_dec)"| bc)
echo "a0 = $a0"


#----- b1 -----#

address_6=$(sudo i2cget -y 1 0x60 0x06 w)
while [ "$address_6" == "" ]; do
    address_6=$(sudo i2cget -y 1 0x60 0x06 w)
done

msb_6=$(echo ${address_6:4:2})
msb_7=$(echo ${address_6:2:2})
#echo "0x$msb_6$msb_7"

b1_dec=$(printf "%d\n" "0x$msb_6$msb_7")
b1_bin=$(echo "obase=2; ibase=10; $b1_dec"| bc)
b1_bin16=$(printf "%016d\n" "$b1_bin")
#echo "$b1_bin16"

b1_bin16_1=$(echo ${b1_bin16//0/a})
b1_bin16_2=$(echo ${b1_bin16_1//1/0}) 
b1_bin16_3=$(echo ${b1_bin16_2//a/1})
#echo "$b1_bin16_3"

b1_bin16_4=$(echo "obase=2; ibase=2; $b1_bin16_3+1"| bc)
b1_bin16_5=$(printf "%016d\n" "$b1_bin16_4")
#echo "$b1_bin16_5"

b1_sign=$(echo ${b1_bin16:0:1})
b1_integer=$(echo ${b1_bin16_5:1:2})
b1_fractional=$(echo ${b1_bin16_5:3:13})
#echo "$b1_sign"
#echo "$b1_integer"
#echo "$b1_fractional"

b1_sign_dec=$(echo ${b1_sign/1/-1})
b1_sign_dec2=$(echo ${b1_sign_dec/0/1})  
b1_integer_dec=$(echo "obase=10; ibase=2; $b1_integer"| bc)
b1_1=$(echo ${b1_fractional:0:1})
b1_2=$(echo ${b1_fractional:1:1})
b1_3=$(echo ${b1_fractional:2:1})
b1_4=$(echo ${b1_fractional:3:1})
b1_5=$(echo ${b1_fractional:4:1})
b1_6=$(echo ${b1_fractional:5:1})
b1_7=$(echo ${b1_fractional:6:1})
b1_8=$(echo ${b1_fractional:7:1})
b1_9=$(echo ${b1_fractional:8:1})
b1_10=$(echo ${b1_fractional:9:1})
b1_11=$(echo ${b1_fractional:10:1})
b1_12=$(echo ${b1_fractional:11:1})
b1_13=$(echo ${b1_fractional:12:1})
b1_fractional_dec=$(echo "scale=8; $b1_1/2+$b1_2/4+$b1_3/8+$b1_4/16+$b1_5/32+$b1_6/64+$b1_7/128+$b1_8/256+$b1_9/512+$b1_10/1024+$b1_11/2048+$b1_12/4096+$b1_13/8192"| bc)
b1=$(echo "scale=8; $b1_sign_dec2*($b1_integer_dec+$b1_fractional_dec)"| bc)
echo "b1 = $b1"


#----- b2 -----#

address_8=$(sudo i2cget -y 1 0x60 0x08 w)
while [ "$address_8" == "" ]; do
    address_8=$(sudo i2cget -y 1 0x60 0x08 w)
done

msb_8=$(echo ${address_8:4:2})
msb_9=$(echo ${address_8:2:2})
#echo "0x$msb_8$msb_9"

b2_dec=$(printf "%d\n" "0x$msb_8$msb_9")
b2_bin=$(echo "obase=2; ibase=10; $b2_dec"| bc)
b2_bin16=$(printf "%016d\n" "$b2_bin")
#echo "$b2_bin16"

b2_bin16_1=$(echo ${b2_bin16//0/a})
b2_bin16_2=$(echo ${b2_bin16_1//1/0}) 
b2_bin16_3=$(echo ${b2_bin16_2//a/1})
#echo "$b2_bin16_3"

b2_bin16_4=$(echo "obase=2; ibase=2; $b2_bin16_3+1"| bc)
b2_bin16_5=$(printf "%016d\n" "$b2_bin16_4")
#echo "$b2_bin16_5"

b2_sign=$(echo ${b2_bin16:0:1})
b2_integer=$(echo ${b2_bin16_5:1:1})
b2_fractional=$(echo ${b2_bin16_5:2:14})
#echo "$b2_sign"
#echo "$b2_integer"
#echo "$b2_fractional"

b2_sign_dec=$(echo ${b2_sign/1/-1})
b2_sign_dec2=$(echo ${b2_sign_dec/0/1})  
b2_integer_dec=$(echo "obase=10; ibase=2; $b2_integer"| bc)
b2_1=$(echo ${b2_fractional:0:1})
b2_2=$(echo ${b2_fractional:1:1})
b2_3=$(echo ${b2_fractional:2:1})
b2_4=$(echo ${b2_fractional:3:1})
b2_5=$(echo ${b2_fractional:4:1})
b2_6=$(echo ${b2_fractional:5:1})
b2_7=$(echo ${b2_fractional:6:1})
b2_8=$(echo ${b2_fractional:7:1})
b2_9=$(echo ${b2_fractional:8:1})
b2_10=$(echo ${b2_fractional:9:1})
b2_11=$(echo ${b2_fractional:10:1})
b2_12=$(echo ${b2_fractional:11:1})
b2_13=$(echo ${b2_fractional:12:1})
b2_14=$(echo ${b2_fractional:13:1})
b2_fractional_dec=$(echo "scale=9; $b2_1/2+$b2_2/4+$b2_3/8+$b2_4/16+$b2_5/32+$b2_6/64+$b2_7/128+$b2_8/256+$b2_9/512+$b2_10/1024+$b2_11/2048+$b2_12/4096+$b2_13/8192+$b2_14/16384"| bc)
b2=$(echo "scale=9; $b2_sign_dec2*($b2_integer_dec+$b2_fractional_dec)"| bc)
echo "b2 = $b2"


#----- c12 -----#

address_a=$(sudo i2cget -y 1 0x60 0x0a w)
while [ "$address_a" == "" ]; do
    address_a=$(sudo i2cget -y 1 0x60 0x0a w)
done

msb_a=$(echo ${address_a:4:2})
msb_b=$(echo ${address_a:2:2})
#echo "0x$msb_a$msb_b"

c12_dec=$(printf "%d\n" "0x$msb_a$msb_b")
c12_bin=$(echo "obase=2; ibase=10; $c12_dec"| bc)
c12_bin16=$(printf "%016d\n" "$c12_bin")
#echo "$c12_bin16"

c12_sign=$(echo ${c12_bin16:0:1})
c12_fractional=$(echo ${c12_bin16:1:13})
#echo "$c12_sign"
#echo "$c12_fractional"

c12_sign_dec=$(echo ${c12_sign/1/-1})
c12_sign_dec2=$(echo ${c12_sign_dec/0/1})  
c12_10=$(echo ${c12_fractional:0:1})
c12_11=$(echo ${c12_fractional:1:1})
c12_12=$(echo ${c12_fractional:2:1})
c12_13=$(echo ${c12_fractional:3:1})
c12_14=$(echo ${c12_fractional:4:1})
c12_15=$(echo ${c12_fractional:5:1})
c12_16=$(echo ${c12_fractional:6:1})
c12_17=$(echo ${c12_fractional:7:1})
c12_18=$(echo ${c12_fractional:8:1})
c12_19=$(echo ${c12_fractional:9:1})
c12_20=$(echo ${c12_fractional:10:1})
c12_21=$(echo ${c12_fractional:11:1})
c12_22=$(echo ${c12_fractional:12:1})
c12_fractional_dec=$(echo "scale=9; $c12_10/1024+$c12_11/2048+$c12_12/4096+$c12_13/8192+$c12_14/16384+$c12_15/32768+$c12_16/65536+$c12_17/131072+$c12_18/262144+$c12_19/524288+$c12_20/1048576+$c12_21/2097152+$c12_22/4194304"| bc)
c12=$(echo "scale=9; $c12_sign_dec2*$c12_fractional_dec"| bc)
echo "c12 = $c12"


#----- c11 -----#

address_c=$(sudo i2cget -y 1 0x60 0x0c w)
while [ "$address_c" == "" ]; do
    address_c=$(sudo i2cget -y 1 0x60 0x0c w)
done

msb_c=$(echo ${address_c:4:2})
msb_d=$(echo ${address_c:2:2})
#echo "0x$msb_c$msb_d"

c11_dec=$(printf "%d\n" "0x$msb_c$msb_d")
c11_bin=$(echo "obase=2; ibase=10; $c11_dec"| bc)
c11_bin16=$(printf "%016d\n" "$c11_bin")
#echo "$c11_bin16"

c11_sign=$(echo ${c11_bin16:0:1})
c11_fractional=$(echo ${c11_bin16:1:10})
#echo "$c11_sign"
#echo "$c11_fractional"

c11_sign_dec=$(echo ${c11_sign/1/-1})
c11_sign_dec2=$(echo ${c11_sign_dec/0/1})  
c11_12=$(echo ${c11_fractional:0:1})
c11_13=$(echo ${c11_fractional:1:1})
c11_14=$(echo ${c11_fractional:2:1})
c11_15=$(echo ${c11_fractional:3:1})
c11_16=$(echo ${c11_fractional:4:1})
c11_17=$(echo ${c11_fractional:5:1})
c11_18=$(echo ${c11_fractional:6:1})
c11_19=$(echo ${c11_fractional:7:1})
c11_20=$(echo ${c11_fractional:8:1})
c11_21=$(echo ${c11_fractional:9:1})
c11_fractional_dec=$(echo "scale=9; $c11_12/4096+$c11_13/8192+$c11_14/16384+$c11_15/32768+$c11_16/65536+$c11_17/131072+$c11_18/262144+$c11_19/524288+$c11_20/1048576+$c11_21/2097152"| bc)
c11=$(echo "scale=9; $c11_sign_dec2*$c11_fractional_dec"| bc)
echo "c11 = $c11"


#----- c22 -----#

address_e=$(sudo i2cget -y 1 0x60 0x0e w)
while [ "$address_e" == "" ]; do
    address_e=$(sudo i2cget -y 1 0x60 0x0e w)
done

msb_e=$(echo ${address_e:4:2})
msb_f=$(echo ${address_e:2:2})
#echo "0x$msb_e$msb_f"

c22_dec=$(printf "%d\n" "0x$msb_e$msb_f")
c22_bin=$(echo "obase=2; ibase=10; $c22_dec"| bc)
c22_bin16=$(printf "%016d\n" "$c22_bin")
#echo "$c22_bin16"

c22_sign=$(echo ${c22_bin16:0:1})
c22_fractional=$(echo ${c22_bin16:1:10})
#echo "$c22_sign"
#echo "$c22_fractional"

c22_sign_dec=$(echo ${c22_sign/1/-1})
c22_sign_dec2=$(echo ${c22_sign_dec/0/1})  
c22_16=$(echo ${c22_fractional:0:1})
c22_17=$(echo ${c22_fractional:1:1})
c22_18=$(echo ${c22_fractional:2:1})
c22_19=$(echo ${c22_fractional:3:1})
c22_20=$(echo ${c22_fractional:4:1})
c22_21=$(echo ${c22_fractional:5:1})
c22_22=$(echo ${c22_fractional:6:1})
c22_23=$(echo ${c22_fractional:7:1})
c22_24=$(echo ${c22_fractional:8:1})
c22_25=$(echo ${c22_fractional:9:1})
c22_fractional_dec=$(echo "scale=9; $c22_16/65536+$c22_17/131072+$c22_18/262144+$c22_19/524288+$c22_20/1048576+$c22_21/2097152+$c22_22/4194304+$c22_23/8388608+$c22_24/16777216+$c22_25/33554432"| bc)
c22=$(echo "scale=9; $c22_sign_dec2*$c22_fractional_dec"| bc)
echo "c22 = $c22"


#----- pcomp -----#

c11_padc=$(echo "$c11*$padc"| bc)
c12_tadc=$(echo "$c12*$tadc"| bc)
pcomp_2=$(echo "($b1+$c11_padc+$c12_tadc)*$padc"| bc)
c22_tadc=$(echo "$c22*$tadc"| bc)
pcomp_3=$(echo "($b2+$c22_tadc)*$tadc"| bc)
pcomp=$(echo "$a0+$pcomp_2+$pcomp_3"| bc)
#echo "$pcomp"


#----- decpcomp -----#

Pa=$(echo "($pcomp*65000+51150000)/1023"| bc)
hPa=$(echo "$Pa/100"| bc)
echo "$hPa [hPa]"



root@raspi-82:~/raspi/dledFromBlogs# ./MPL115A2.sh 
Padc = 312
Tadc = 529
a0 = 1772.625
b1 = -2.06408691
b2 = -.871032713
c12 = .000705001
c11 = 0
c22 = 0
998 [hPa]
root@raspi-82:~/raspi/dledFromBlogs# 

湿度センサーがほしくなってきたぞw

トラックバック - http://d.hatena.ne.jp/toshi_hirasawa/20130502/1367487406
リンク元