-
Notifications
You must be signed in to change notification settings - Fork 4
/
digitalSen_thread.py
54 lines (48 loc) · 1.61 KB
/
digitalSen_thread.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
import threading
import time
#water temperature
from w1thermsensor import W1ThermSensor
#BME280
import smbus2
import bme280
port = 1
address = 0x76
bus = smbus2.SMBus(port)
try:
calibration_params = bme280.load_calibration_params(bus, address)
# the sample method will take a single reading and return a
# compensated_reading object
data = bme280.sample(bus, address, calibration_params)
except:
print("Attention: no BME has been detected!")
class D_Temp(threading.Thread): # digital temperature sensors (water temp and bme280 (temp+hum+press))
def __init__(self, sleep):
threading.Thread.__init__(self)
self.sleep = sleep
self.W_temp = 0
self.temp=0
self.hum=0
self.pres=0
def run(self):
while True:
# reads every sleep interval >0.8 (sensor response minimum delay)
try:
self.W_temp = W1ThermSensor().get_temperature()
except:
self.W_temp = 0
time.sleep(self.sleep)
try:
data = bme280.sample(bus, address, calibration_params)
self.temp = data.temperature
self.hum = data.humidity
self.pres = data.pressure
except:
self.temp = 0
self.hum = 0
self.pres = 0
def read_d_temp(self): # read digital temperature sensors
water_temp = self.W_temp
air_temp= self.temp
air_hum= self.hum
air_pres=self.pres
return water_temp, air_temp, air_hum, air_pres