forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P014_SI7021.py
153 lines (137 loc) · 4.07 KB
/
_P014_SI7021.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
#############################################################################
##################### SI7021/HTU21D plugin for RPIEasy ######################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import gpios
import fcntl # smbus does not work properly with HTU21... we need direct i2c access
import time
class Plugin(plugin.PluginProto):
PLUGIN_ID = 14
PLUGIN_NAME = "Environment - SI7021/HTU21D"
PLUGIN_VALUENAME1 = "Temperature"
PLUGIN_VALUENAME2 = "Humidity"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_I2C
self.vtype = rpieGlobals.SENSOR_TYPE_TEMP_HUM
self.readinprogress = 0
self.valuecount = 2
self.senddataoption = True
self.timeroption = True
self.timeroptional = False
self.formulaoption = True
self._nextdataservetime = 0
self.htu = None
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.uservar[0] = 0
self.uservar[1] = 0
self.readinprogress = 0
if self.enabled:
try:
i2cl = self.i2c
except:
i2cl = -1
try:
i2cport = gpios.HWPorts.geti2clist()
if i2cl==-1:
i2cl = int(i2cport[0])
except:
i2cport = []
if len(i2cport)>0 and i2cl>-1:
self.htu = None
try:
self.htu = HTU21D(i2cl)
except Exception as e:
self.htu = None
if self.htu:
try:
self.initialized = self.htu.init
except:
self.htu = None
if self.htu is None:
self.initialized = False
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HTU21D/Si7021 can not be initialized! "+str(e))
def plugin_read(self): # deal with data processing at specified time interval
result = False
if self.initialized and self.readinprogress==0 and self.enabled:
self.readinprogress = 1
try:
temp = self.htu.read_temperature()
hum = self.htu.read_humidity()
self.set_value(1,temp,False)
self.set_value(2,hum,False)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HTU21 read error! "+str(e))
self.enabled = False
self.plugin_senddata()
self._lastdataservetime = rpieTime.millis()
result = True
self.readinprogress = 0
return result
class HTU21D:
#control constants
_I2C_ADDRESS = 0x40
_SOFTRESET = bytes([0xFE])
_TRIGGER_TEMPERATURE_NO_HOLD = bytes([0xF3])
_TRIGGER_HUMIDITY_NO_HOLD = bytes([0xF5])
#From: /linux/i2c-dev.h
I2C_SLAVE = 0x0703
I2C_SLAVE_FORCE = 0x0706
def __init__(self, device_number=1):
try:
self.i2cr = open("/dev/i2c-"+str(device_number),"rb",buffering=0)
self.i2cw = open("/dev/i2c-"+str(device_number),"wb",buffering=0)
fcntl.ioctl(self.i2cr, self.I2C_SLAVE,0x40) # I2CADDR
fcntl.ioctl(self.i2cw, self.I2C_SLAVE,0x40) # I2CADDR
self.i2cw.write(self._SOFTRESET)
time.sleep(0.015)
self.init = True
except:
self.init = False
def read_temperature(self):
if self.init:
self.i2cw.write(self._TRIGGER_TEMPERATURE_NO_HOLD)
time.sleep(0.050)
data = self.i2cr.read(3)
return self._get_temperature_from_buffer(data)
def read_humidity(self):
if self.init:
self.i2cw.write(self._TRIGGER_HUMIDITY_NO_HOLD)
time.sleep(0.025)
data = self.i2cr.read(3)
return self._get_humidity_from_buffer(data)
def close(self):
if self.init:
self.i2cr.close()
self.i2cw.close()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def _get_temperature_from_buffer(self, data):
if len(data)>1:
raw = (data[0] << 8) + data[1]
raw *= 175.72
raw /= 1 << 16
raw -= 46.85
else:
raw = 0
return raw
def _get_humidity_from_buffer(self, data):
if len(data)>1:
raw = (data[0] << 8) + data[1]
raw *= 125.0
raw /= 1 << 16
raw -= 6
else:
raw = 0
return raw