らずぱいでIoT 第9回(python版 気温・気圧・湿度計算)
今回は、いよいよpython版のレジスタの読取り・計算・表示の全プログラムです。
このプログラムでは、Dict(連想配列)を使う事でチップ説明書にあるレジスタアドレスと一致するように工夫してあります。
プログラムの内容自体は、前回のチップ説明書の中にある保障式を今まで書いてきたプログラムに合うように書き直したものです。
————————————- 以下ソース
#!/usr/bin/python
import smbus
import time
SMBUS_NUMBER = 1
SMBUS_ADDR = 0x77
SMBUS_REG_CONF = 0xF5
SMBUS_REG_CTRL_MEAS = 0xF4
SMBUS_REG_CTRL_HUM = 0xF2
dig_T = {}
dig_P = {}
dig_H = {}
calib = {}
n = 0x88
while n <= 0xA1:
__ calib[n] = 0
__ n = n + 1
n = 0xE1
while n <= 0xF0:
__ calib[n] = 0
__ n = n + 1
n = 0xF7
while n <= 0xFE:
__ calib[n] = 0
__ n = n + 1
bus = smbus.SMBus(SMBUS_NUMBER)
bus.write_byte_data(SMBUS_ADDR,SMBUS_REG_CONF,0xA0)
bus.write_byte_data(SMBUS_ADDR,SMBUS_REG_CTRL_HUM,0x01)
bus.write_byte_data(SMBUS_ADDR,SMBUS_REG_CTRL_MEAS,0x25)
time.sleep(0.1)
for k in calib.keys():
__ calib[k] = bus.read_byte_data(SMBUS_ADDR, k)
dig_T[1] = (calib[0x89] << 8 | calib[0x88])
dig_T[2] = (calib[0x8B] << 8 | calib[0x8A])
dig_T[3] = (calib[0x8D] << 8 | calib[0x8C])
dig_P[1] = (calib[0x8F] << 8 | calib[0x8E])
dig_P[2] = (calib[0x91] << 8 | calib[0x90])
dig_P[3] = (calib[0x93] << 8 | calib[0x92])
dig_P[4] = (calib[0x95] << 8 | calib[0x94])
dig_P[5] = (calib[0x97] << 8 | calib[0x96])
dig_P[6] = (calib[0x99] << 8 | calib[0x98])
dig_P[7] = (calib[0x9B] << 8 | calib[0x9A])
dig_P[8] = (calib[0x9D] << 8 | calib[0x9C])
dig_P[9] = (calib[0x9F] << 8 | calib[0x9E])
dig_H[1] = calib[0xA1]
dig_H[2] = (calib[0xE2] << 8 | calib[0xE1])
dig_H[3] = (calib[0xE3])
dig_H[4] = (calib[0xE4] << 4 | calib[0xE5] & 0xf)
dig_H[5] = (calib[0xE6] << 4 | calib[0xE5] >> 4)
dig_H[6] = (calib[0xE7])
for i in [2,3]:
__ if (dig_T[i] & 0x8000):
____ dig_T[i] = (-dig_T[i] ^ 0xffff)+1
for i in [2,3,4,5,6,7,8,9]:
__ if (dig_P[i] & 0x8000):
____ dig_P[i] = (-dig_P[i] ^ 0xffff)+1
for i in [2,4,5]:
__ if (dig_H[i] & 0x8000):
____ dig_H[i] = (-dig_H[i] ^ 0xffff)+1
if (dig_H[6] & 0x80):
__ dig_H[6] = (-dig_H[6] ^ 0xff)+1
raw_T = calib[0xFA] << 12 | calib[0xFB] << 4 | calib[0xFC] >> 4
raw_P = calib[0xF7] << 12 | calib[0xF8] << 4 | calib[0xF9] >> 4
raw_H = calib[0xFD] << 8 | calib[0xFE]
tv1 = ((raw_T >> 3) – (dig_T[1] << 1)) * (dig_T[2] >> 11)
tv2 = (((raw_T >> 4) – dig_T[1]) * ((raw_T >> 4) – dig_T[1])) >> 12
tv2 = (tv2 * dig_T[3]) >> 14
t_fine = tv1 + tv2
Temp = ((t_fine * 5 + 128) >> 8) / 100.0
pv1 = (t_fine / 2.0) – 64000
pv2 = ((pv1 / 4.0) * (pv1 / 4.0) / 2048) * dig_P[6]
pv2 = pv2 + ((pv1 * dig_P[5]) * 2.0)
pv2 = (pv2 / 4.0) + (dig_P[4] * 65536.0)
pv1 = (((dig_P[3] * (((pv1 /4.0) * (pv1 * 4.0)) /8192)) / 8) + ((dig_P[2] * pv1) / 2.0)) / 262144
pv1 = ((32768 + pv1) * dig_P[1]) / 32768
if pv1 != 0:
__ p = ((1048576 – raw_P) – (pv2 / 4096)) * 3125
__ if p < 0x80000000:
____ p = (p * 2.0) / pv1
__ else:
____ p = (p / pv1) * 2
__ pv1 = (dig_P[9] * (((p / 8.0) * (p / 8.0)) / 8192.0)) / 4096
__ pv2 = ((p / 4.0) * dig_P[8]) / 8192.0
__ p = p + ((pv1+ pv2 + dig_P[7]) /16.0)
else:
__ p = 0
Pres = p / 100.0
hv = t_fine – 76800.0
if hv != 0:
__ hv = (raw_H – (dig_H[4] * 64.0 + dig_H[5]/16384.0 * hv)) * (dig_H[2] / 65536.0 * (1.0 + dig_H[6] / 67108864.0 * hv * (1.0 + dig_H[3] / 67108864.0 * hv)))
__ hv = hv * (1.0 – dig_H[1] * hv / 524288.0)
__ if hv > 100.0:
____ hv = 100.0
__ elif hv < 0.0:
____ hv = 0.0
else:
__ hv=0.0
Hum = hv
print “T=%2.1f P=%4.1f H=%1.2f” % (Temp,Pres,Hum)
————————————- ここまで
尚、_はスペースです。
ソースはこちらからダウンロードできます。