-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tempered.py
92 lines (70 loc) · 2.32 KB
/
Tempered.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
import RPi.GPIO as GPIO
import time
def bin2dec(string_num):
return str(int(string_num, 2))
class TemperatureSensor():
def __init__(self):
data = []
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.HIGH)
time.sleep(0.025)
GPIO.output(4, GPIO.LOW)
time.sleep(0.02)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
for i in range(0, 500):
data.append(GPIO.input(4))
bit_count = 0
tmp = 0
count = 0
HumidityBit = ""
TemperatureBit = ""
crc = ""
Humidity = 0
Temperature = 0
def GetTempandVlaga(self):
try:
while self.data[self.count] == 1:
self.tmp = 1
self.count = self.count + 1
for i in range(0, 32):
self.bit_count = 0
while self.data[self.count] == 0:
self.tmp = 1
self.count = self.count + 1
while self.data[self.count] == 1:
self.bit_count =self.bit_count + 1
self.count = self.count + 1
if self.bit_count > 3:
if i >= 0 and i < 8:
self.HumidityBit = self.HumidityBit + "1"
if i >= 16 and i < 24:
self.TemperatureBit = self.TemperatureBit + "1"
else:
if i >= 0 and i < 8:
self.HumidityBit = self.HumidityBit + "0"
if i >= 16 and i < 24:
self.TemperatureBit = self.TemperatureBit + "0"
except:
return "ERR_RANGE"
try:
for i in range(0, 8):
self.bit_count = 0
while data[self.count] == 0:
self.tmp = 1
self.count = self.count + 1
while data[self.count] == 1:
self.bit_count = self.bit_count + 1
self.count = self.count + 1
if self.bit_count > 3:
self.crc = self.crc + "1"
else:
self.crc = self.crc + "0"
except:
return "ERR_RANGE"
self.Humidity = bin2dec(self.HumidityBit)
self.Temperature = bin2dec(self.TemperatureBit)
if int(self.Humidity) + int(self.Temperature) - int(bin2dec(self.crc)) == 0:
return "Humidity:" + self.Humidity + "%" + " "" Temperature:" + self.Temperature + "C"
else:
return "ERR_CRC"