Raspberry Pi上のPythonでI2Cを制御する為に、smbusモジュールを使用します。
smbusモジュールがインストールされていない場合、下記コマンドでインストールしてください。
pip install smbus
d6t_44l_06.pyはパッケージとしてimportして使用しますが、テスト用に単体で温度データを取得できます。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
D6T-44L-06 script.
==================
Temperature measure 5 times/second.
Human-detectable distance: MAX 7[m]
5.0[V] I2C(0Ah)
4x4 array 5~45[deg]
***
2020/9/7
* Remake def pixels().
* Previous def result[0] is device temperature.
* Sometime temperature is 41degC over.
* Max temperature set 40degC.
"""
import threading
import time
import numpy as np
i2c_enable = False
try:
import smbus
i2c_enable = True
except:
i2c_enable = False
ADDRESS = 0x0a
REGISTER = 0x4c
class Sensor():
def __init__(self, *args, **kwargs):
self.avg_count = 10
self.enable = False
self._previous_result = None
self._shutdowning = False
self.temperature = 0.0
self.i2c_enable = i2c_enable
self._pixels_data = None
if self.i2c_enable:
self.enable = True
self._i2c = smbus.SMBus(1)
_thread = threading.Thread(
name='D6T-44L-06_Measuring',
target=self._thread,
)
_thread.daeom = True
_thread.start()
@property
def array(self):
return (self.x, self.y)
def avg(self, pixels):
if self._pixels_data is None:
self._pixels_data = [pixels]
elif len(self._pixels_data) < self.avg_count:
self._pixels_data = np.append(
self._pixels_data, [pixels], axis=0)
elif len(self._pixels_data) == self.avg_count:
self._pixels_data = np.delete(
self._pixels_data, 0, axis=0)
self._pixels_data = np.append(
self._pixels_data, [pixels], axis=0)
def __del__(self):
self._shutdowning = True
@property
def high(self):
return 45.0
@property
def low(self):
return 5.0
@property
def name(self):
return 'D6T-44L-06'
@property
def pixels(self):
result = None
if self._pixels_data is not None:
result = np.average(self._pixels_data, axis=0)
return result
def stop(self):
self._shutdowning = True
def _thread(self):
if self._shutdowning:
return
# for parent Thermo class.
result = None
if self.enable:
result = []
try:
data = self._i2c.read_i2c_block_data(
ADDRESS,
REGISTER,
32)
except:
self.i2c_enable = False
self.enable = False
return
self.temperature = (data[1]*256 + data[0]) / 10.0
data.pop(0)
data.pop(0)
if data[1] < 250 :
for i in range(4):
for j in range(4):
_temperature \
= int((data[i*2*j+1])*256 + data[i*2*j]) / 10.0
if _temperature > 41.0:
break
result.append(_temperature)
if len(result) != 16:
result = self._previous_result
else:
self._previous_result = result
self.avg(result)
time.sleep(0.2)
_thread = threading.Thread(
name='D6T-44L-06_Measuring',
target=self._thread,
)
_thread.daeom = True
_thread.start()
@property
def x(self):
return 4
@property
def y(self):
return 4
if __name__ == '__main__':
import time
sensor = Sensor()
while True:
tmp = sensor.pixels
if tmp is not None:
print(tmp)
#print(max(tmp), min(tmp))
time.sleep(1)
pass