-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTempHum.py
More file actions
151 lines (122 loc) · 4.8 KB
/
Copy pathTempHum.py
File metadata and controls
151 lines (122 loc) · 4.8 KB
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
import RPi.GPIO as GPIO
import config
import time
from time import sleep
from datetime import datetime, time as datetime_time, timedelta
from utils.common import get_time
import app_logger
import Adafruit_DHT
from RootSensor import RootSensor
def usleep(x): return sleep(x/1000000.0)
def read_raw_val(pin):
max_wait_count = 10000
error = 0
raw_val = []
res = [int(0)]*5
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
usleep(500000)
GPIO.output(pin, GPIO.LOW)
usleep(20000)
GPIO.output(pin, GPIO.HIGH)
usleep(40)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# wait first law
for count in range(0, max_wait_count):
if GPIO.input(pin):
break
if count == (max_wait_count-1):
error = -1
return (res, error)
# waiting data
for pulse in range(0, 41):
for count in range(0, max_wait_count):
if not GPIO.input(pin):
break
raw_val.append(count)
if count == (max_wait_count-1):
error = -2
return (res, error)
for count in range(0, max_wait_count):
if GPIO.input(pin):
break
raw_val.append(count)
if count == (max_wait_count-1):
error = -3
return (res, error)
count = 0
for pulse in range(1, 81, 2):
indx = int(count/8)
count = count+1
res[indx] <<= 1
if raw_val[pulse+1] >= raw_val[pulse]:
res[indx] |= 0x1
# check parity
if not res[4] == ((res[0]+res[1]+res[2]+res[3]) & 0xff):
error = -4
return (res, error)
return (res, error)
class TempHum(RootSensor):
def probe(self):
with self.lock:
if not hasattr(config, "GPIO_GET_TEMP_HUMID"):
return False
GPIO.setup(config.GPIO_ENABLE_POWER_TEMP_HUM, GPIO.OUT)
GPIO.output(config.GPIO_ENABLE_POWER_TEMP_HUM, GPIO.LOW)
GPIO.setup(config.GPIO_GET_TEMP_HUMID, GPIO.IN, pull_up_down=GPIO.PUD_UP)
return True
def read_dht22_data(pin):
humidity = 0
temperature = 0
data, error = read_raw_val(pin)
if not error == 0:
return (humidity, temperature, error)
temperature = float(((data[2] & 0x7F) << 8 | data[3])) / 10
humidity = float((data[0] << 8 | data[1])) / 10
if (data[2] & 0x80) != 0:
temperature = -temperature
return (humidity, temperature, error)
def read_val(self):
with self.lock:
tot_humidity = 0
tot_temperature = 0
try:
num_good_cycles = 0
num_cycles_wrong = 0
GPIO.output(config.GPIO_ENABLE_POWER_TEMP_HUM, GPIO.HIGH)
# sleep(1) # wait sensor warm up - not waiting now, because at's constantly enabled
timeout = time.time() + 5 # 5 seconds
while time.time() < timeout:
error = 0
humidity, temperature = Adafruit_DHT.read_retry(config.TEMP_DHT_VER, config.GPIO_GET_TEMP_HUMID)
#humidity, temperature,error=read_dht22_data(config.GPIO_GET_TEMP_HUMID) # my implemented read
if error == 0 and temperature != None and humidity < 100 and humidity > 5:
tot_humidity += humidity
tot_temperature += temperature
num_good_cycles+=1
if num_good_cycles>3:
break # enough good data
else:
num_cycles_wrong+=1
GPIO.output(config.GPIO_ENABLE_POWER_TEMP_HUM, GPIO.LOW)
if num_good_cycles > 0 :
tot_humidity /= num_good_cycles
tot_temperature /= num_good_cycles
else:
#to not enable fan by default
tot_humidity=50
tot_temperature=25
app_logger.debug("can't measure temperature and humidity, wrong cycles: "+str(num_cycles_wrong))
#app_logger.info( 'Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(tot_temperature, tot_humidity)+", num cycles: "+str(num_cycles_wrong))
except Exception as e:
app_logger.exception("get temp error, temp="+str(tot_temperature)+", hum="+str(tot_humidity))
finally:
self.data_bus.temperature=tot_temperature
self.data_bus.humidity=tot_humidity
return (tot_temperature, tot_humidity, num_cycles_wrong)
def get_status_str(self):
if not self.data_bus.temperature:
self.read_val()
ret = 'Temp: {0:0.1f} °C \nHumidity: {1:0.1f} %\n'.format(
self.data_bus.temperature, self.data_bus.humidity,)
return ret