-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensors.py
More file actions
220 lines (181 loc) · 6.33 KB
/
sensors.py
File metadata and controls
220 lines (181 loc) · 6.33 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#PIR Motion Sensor\
import Adafruit_DHT
import RPi.GPIO as GPIO
import time
from datetime import datetime
from multiprocessing import Process, Value
from ctypes import c_bool
import stepper
print('Successfully Loaded Sensors')
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#Motion Sensor PIN
PIR=12
#AdaFruit uses GPIO.BCM pin# 23 instead of 16
#Temperature & Humidity Sensor PIN
DHT11=23
#Light Sensor PIN
LDR=18
#Boolean storing the value, which show if the blinds are opened or closed
blindsIsOpen = Value(c_bool, False)
#Flick to end a process
stop_process = Value(c_bool, False)
#Stores how many active processes there are
active_processes = Value('i', 0)
#Sets the PIR motion sensor as an input
GPIO.setup(PIR,GPIO.IN)
#Sets up the DHT11 temperature/humidity sensor
dht11Sensor=Adafruit_DHT.DHT11
#Trigger the blinds open/close
def TriggerBlinds():
#adds a process
#(note: this isn't a process but is required for the manual opening of the blinds)
active_processes.value += 1
if(blindsIsOpen.value):
print(datetime.now().strftime("%H:%M.%S") + " Closing blinds...")
else:
print(datetime.now().strftime("%H:%M.%S") + " Opening blinds...")
#Operates the blinds, number of steps/direction
stepper.OperateBlinds(2000, blindsIsOpen.value)
#Updates the status of the blinds
blindsIsOpen.value = not blindsIsOpen.value
#removes the process once execution is finished
active_processes.value -= 1
#graphs the temperature/hum values on the GUI
def graphDHT11(Graph):
#gets current hour
hour = datetime.now().strftime("%H")
Graph(0,0,0, False)
while True:
#read humidity, Temperature
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHT11)
#get datetime
currentTime = datetime.now().strftime("%H:%M.%S")
minuteSec = currentTime.split(':')[1]
#if it ticks over to the next hour
if (currentTime.split(':')[0] != hour):
tempHum = []
hour = currentTime.split(':')[0]
#reset the graph
Graph(temperature, humidity, minuteSec, True)
else:
Graph(temperature, humidity, minuteSec, False)
#reads the values from the temperature sensor
def ReadDHT11():
#Adds a process
active_processes.value += 1
#wait until the other processes are finished
while (active_processes.value > 1):
time.sleep(0.2)
stop_process.value = False
print("DHT11 process started")
while True:
#read humidity/temperatured
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHT11)
#Stop the Process
if (stop_process.value):
print("DHT11 process closed")
active_processes.value -= 1
break
#Temperature levels are low (room is cold)
if (temperature < 20):
#Blinds are closed
if (not blindsIsOpen.value):
#Open blinds (let heat into the room)
TriggerBlinds()
#Temperature levels are high (room is warm)
else:
#blinds are open
if (blindsIsOpen.value):
#Close blinds (trap heat)
TriggerBlinds()
#reads the values from the light sensor
def ReadLight():
#Adds a process
active_processes.value += 1
#wait until the other processes are finished
while (active_processes.value > 1):
time.sleep(0.2)
stop_process.value = False
print("Light process started")
while True:
#Stop the Process
if (stop_process.value):
print("Light process closed")
active_processes.value -= 1
break
#reset the capacitor
GPIO.setup(LDR, GPIO.OUT)
GPIO.output(LDR, GPIO.LOW)
time.sleep(2)
#set the starttime
startTime = time.time()
GPIO.setup(LDR, GPIO.IN)
#wait until the pin changes value, then timestamp it
while (GPIO.input(LDR) == GPIO.LOW):
endTime = time.time()
#light levels high
#resistance low, (elapsed time short)
if (endTime-startTime < 1):
if (not blindsIsOpen.value):
TriggerBlinds()
#light levels low
#resistance high, (elapsed time long)
else:
if (blindsIsOpen.value):
TriggerBlinds()
#reads the values from the motion sensor
def ReadMotion():
#Adds a Process
active_processes.value += 1
#wait until the other processes are finished
while (active_processes.value > 1):
time.sleep(0.2)
stop_process.value = False
#set the starttime
startTime = time.time()
print("Motion process started")
while True:
time.sleep(0.5)
isMotion = GPIO.input(PIR)
#Stop the process
if (stop_process.value):
print("Motion process closed")
active_processes.value -= 1
break
#if theres movement
if (isMotion):
#reset the timer
startTime = time.time()
#if blinds are closed, open them
if (not blindsIsOpen.value):
TriggerBlinds()
#if the the time has elapsed and the blinds are still open, close them
if (time.time() - startTime > 30 and blindsIsOpen.value):
TriggerBlinds()
#Stop the current processes
def StopProcesses():
stop_process.value = True
#Create a process to run the 'ReadMotion' Function
def BlindsMotion():
StopProcesses()
p = Process(target=ReadMotion, args=())
p.start()
#Create a process to run the 'ReadLight' Function
def BlindsLight():
StopProcesses()
p = Process(target=ReadLight, args=())
p.start()
#Create a process to run the 'ReadDHT11' Function
def BlindsTemperature():
StopProcesses()
p = Process(target=ReadDHT11, args=())
p.start()
#Create a process to run the 'TriggerBlinds' Function
def BlindsManual():
StopProcesses()
#wait till all the processes are closed
while (not active_processes.value == 0):
time.sleep(0.2)
p = Process(target=TriggerBlinds, args=())
p.start()