ありゃりゃ、MPL3115は湿度は測定できない・・・。プログラム段階で知るとは間抜けな!
まあいいかこれでやることに。結線の心配をしたが一発で繋がった。
例題がPython2.7だったので3に修正した。
コマンドラインからpython3 xxx.py でなく
$>./xxx.pyで実行するには以下のソースのヘッダー部分を記述。
chmod 755 にしないと実行できないね。久々にlinuxやってる(-_-;
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from smbus import SMBus
import time
# Special Chars
deg = u'\N{DEGREE SIGN}'
# I2C Constants
ADDR = 0x60
CTRL_REG1 = 0x26
PT_DATA_CFG = 0x13
bus = SMBus(1)
who_am_i = bus.read_byte_data(ADDR, 0x0C)
print( hex(who_am_i))
if who_am_i != 0xc4:
print("Device not active.")
exit(1)
# Set oversample rate to 128
setting = bus.read_byte_data(ADDR, CTRL_REG1)
newSetting = setting | 0x38
bus.write_byte_data(ADDR, CTRL_REG1, newSetting)
# Enable event flags
bus.write_byte_data(ADDR, PT_DATA_CFG, 0x07)
# Toggel One Shot
setting = bus.read_byte_data(ADDR, CTRL_REG1)
if (setting & 0x02) == 0:
bus.write_byte_data(ADDR, CTRL_REG1, (setting | 0x02))
try:
while True:
print("--------------")
# Enable event flags
bus.write_byte_data(ADDR, PT_DATA_CFG, 0x07)
# Toggel One Shot
setting = bus.read_byte_data(ADDR, CTRL_REG1)
if (setting & 0x02) == 0:
bus.write_byte_data(ADDR, CTRL_REG1, (setting | 0x02))
# Read sensor data
#print("Waiting for data...")
status = bus.read_byte_data(ADDR,0x00)
while (status & 0x08) == 0:
#print bin(status)
status = bus.read_byte_data(ADDR,0x00)
time.sleep(0.5)
#print ("Reading sensor data...")
p_data = bus.read_i2c_block_data(ADDR,0x01,3)
t_data = bus.read_i2c_block_data(ADDR,0x04,2)
status = bus.read_byte_data(ADDR,0x00)
#print("status: "+bin(status))
p_msb = p_data[0]
p_csb = p_data[1]
p_lsb = p_data[2]
t_msb = t_data[0]
t_lsb = t_data[1]
pressure = (p_msb << 10) | (p_csb << 2) | (p_lsb >> 6)
p_decimal = ((p_lsb & 0x30) >> 4)/4.0
celsius = t_msb + (t_lsb >> 4)/16.0
fahrenheit = (celsius * 9)/5 + 32
#print ("Pressure and Temperature at "+time.strftime('%m/%d/%Y %H:%M:%S%z'))
print ('{:.0f}'.format((pressure+p_decimal)/100.0)+" Pa")
print ('{:.1f}'.format(celsius)+deg+"C")
#print (str(fahrenheit)+deg+"F")
except KeyboardInterrupt:
print("Key Stop")
pass
【結果】
————–
10156 Pa
25.6°C
————–
10156 Pa
25.6°C
————–
10156 Pa
25.6°C


