/
methods.py
233 lines (221 loc) · 10.3 KB
/
methods.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
'''
UNIT 14 P2 Line 136
'''
import time, os, sys, glob, threading
# FILES IMPORT BELOW
from board import Board
from lightClass import Light as L
from LedClass import Led as l
from tempClass import Temp as t
from distClass import Dist as d
from pirClass import Pir as p
from buzzerClass import Buzz as b
from lcd1602 import LCD1602
from bottle import *
import json, inspect
from pins import Pins
board = Board().board
lcd = LCD1602(board)
def pisetup():
tempSet()
split = '###########################################'
board.setmode(board.BOARD) #set GPIO up
inputs = [tempSensor, pirSensor, deSensor] # Set there categories in arrays
outputs = [pirLight, dtSensor, buzzSensor, fadeLed, lightSensor, ledBlue, ledGreen, ledRed, lsLight, rgbR, rgbG, rgbB]
buttons = [lightButton, buzzButton]
print(split)
print('### ATTEMPTING TO IMPORT AND SETUP PINS ###')
Pins(inputs, outputs, buttons, board, time) #Set up pins from a class
print('### ALL THE PINS ARE IMPORTED AND SETUP ###')
print(split)
print('### RUNNING HOME AUTOMATION IN GUI MODE ###')
print(split)
def pwmpins():
rpwm = board.PWM(rgbR, 100)
rpwm.start(0)
gpwm = board.PWM(rgbG, 100)
gpwm.start(0)
bpwm = board.PWM(rgbB, 100)
bpwm.start(0)
vpwm = board.PWM(fadeLed, 100)
vpwm.start(100)
return rpwm, gpwm, bpwm, vpwm
##########################################################################################################
def getAll(lcdyon):
list, c, temprgb = getTemp(board, ledRed, ledGreen, ledBlue, highTemp, lowTemp) # Gets the values needed for the print of values
pir = getPir(pirSensor, board, pirLight, buzzSensor) #
light, ls = getLight(lightSensor, board, lsLight) #
dist = getDist(dtSensor, deSensor, board) #
layoutString = ('C PIR Dis Light')
theString = (str(c) + ' ' + str(pir) + ' ' + str(dist) + ' ' + str(light)) # String for the LCD screen
if lcdyon == 1:
lcd.lcd_string('C PIR Dis Light',lcd.LCD_LINE_1)
lcd.lcd_string(str(c) + ' ' + str(pir) + ' ' + str(dist) + ' ' + str(light),lcd.LCD_LINE_2)
return c, pir, light, dist, temprgb, ls
##########################################################################################################
def tempSet():
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
##########################################################################################################
def getTemp(board, ledRed, ledGreen, ledBlue, highTemp, lowTemp):
c, f = t.read_temp() # Get temp values
f = round(f, 2)
c = round(c, 2)
ct = str(int(c)) # converts degrees c to string
ft = str(int(f)) # converts degrees f to string
temp = (ct + ' Celsius & ' + ft + ' Fahrenheit') # creates compiled string of temperature values
sys.stdout.write("\033[K")
print ('### Temperature: ' + temp)
tempFin = (ct + ' ' + ft)
gui = (str(c) + ' C ' + str(f) + ' F')
if f <= lowTemp:
temprgb = 'blue'
elif f >= highTemp:
temprgb = 'red'
else:
temprgb = 'green'
tempLight(board, f, ledRed, ledGreen, ledBlue, highTemp, lowTemp)
c = round(c)
return tempFin, c, temprgb
##########################################################################################################
def getDist(dtSensor, deSensor, board):
dval = d(dtSensor, deSensor, board)
value = (dval.distValue)
value = round(value)
value = str(value)
sys.stdout.write("\033[K")
print('### Distance: ' + value)
return value
##########################################################################################################
def getPir(pirSensor, board, pirLight, buzzSensor):
pval = p(pirSensor, board)
value = pval.pirState
sys.stdout.write("\033[K")
print ('### PIR Value = ' + str(value) + ' // 1 = on // 0 = off')
if value == 1:
finValue = 'ON'
l(pirLight, board, 0).LedOn() # Turns PIR LIGHT ON
b(buzzSensor, board).buzzOn()
time.sleep(0.4)
b(buzzSensor, board).buzzOff()
else:
finValue = 'OFF'
return finValue
##########################################################################################################
def getLight(lightSensor, board, lsLight):
LSV = 0
lval = L(lightSensor, board, LSV)
value = lval.LSV
sys.stdout.write("\033[K")
if value > 1500:
l(lsLight, board, 0).LedOn()
finValue = 'ON'
else:
l(lsLight, board, 0).LedOff()
finValue = 'OFF'
print ('### Light Sensor Value = ' + str(value))
return value, finValue
##########################################################################################################
def ButtonSwitch(lightButton, board, lightState, vpwm):
while True:
time.sleep(0.15)
if board.input(lightButton) == 0:
if lightState == 'on':
vpwm.ChangeDutyCycle(0)
lightState = 'off'
elif lightState == 'off':
vpwm.ChangeDutyCycle(100)
lightState = 'on'
else:
pass
def BuzzSwitch(board, buzzSensor, buzzButton, buzzState): # U14P2: The whole method is constantly running in a thread meaning it's always being checked.
while True: # Loops the entire method forever.
board.wait_for_edge(buzzButton, board.FALLING) # This waits for the button to be pressed before proceeding with the next line of code.
b(buzzSensor, board).buzzOn() # Turns the buzzer on
board.wait_for_edge(buzzButton, board.RISING) # This waits for the button to be released from being pressed before it proceeds with the next line of code.
b(buzzSensor, board).buzzOff() # Turns the buzzer off
##########################################################################################################
def tempLight(board, f, ledRed, ledGreen, ledBlue, highTemp, lowTemp):
if f <= lowTemp:
l(ledBlue, board, 0).LedOn()
l(ledGreen, board, 0).LedOff()
l(ledRed, board, 0).LedOff()
elif f >= highTemp:
l(ledRed, board, 0).LedOn()
l(ledGreen, board, 0).LedOff()
l(ledBlue, board, 0).LedOff()
else:
l(ledGreen, board, 0).LedOn()
l(ledRed, board, 0).LedOff()
l(ledBlue, board, 0).LedOff()
##########################################################################################################
def Closeness(board, buzzSensor, dist):
if dist <= 5:
b(buzzSensor, board).buzzOn()
time.sleep(0.6)
b(buzzSensor, board).buzzOff()
elif dist <= 10:
b(buzzSensor, board).buzzOn()
time.sleep(0.45)
b(buzzSensor, board).buzzOff()
elif dist <= 15:
b(buzzSensor, board).buzzOn()
time.sleep(0.35)
b(buzzSensor, board).buzzOff()
elif dist <= 20:
b(buzzSensor, board).buzzOn()
time.sleep(0.2)
b(buzzSensor, board).buzzOff()
elif dist <= 25:
b(buzzSensor, board).buzzOn()
time.sleep(0.1)
b(buzzSensor, board).buzzOff()
else:
b(buzzSensor, board).buzzOn()
time.sleep(0.03)
b(buzzSensor, board).buzzOff()
##########################################################################################################
def setLcd(line1, line2):
lcd.lcd_string(str(line1.get()), lcd.LCD_LINE_1)
lcd.lcd_string(str(line2.get()), lcd.LCD_LINE_2)
##########################################################################################################
def testBuzz(board, buzzSensor):
b(buzzSensor, board).buzzTest()
##########################################################################################################
def clearLcd(line1, line2):
lcd.lcd_clear()
##########################################################################################################
#Pins
tempSensor = 7
dtSensor = 29
deSensor = 31
pirSensor = 32
buzzSensor = 35
lightSensor = 18
lightButton = 16
buzzButton = 22
fadeLed = 11
pirLight = 10
ledRed = 36
ledGreen = 38
ledBlue = 40
lsLight = 12
rgbR = 19
rgbG = 21
rgbB = 23
# Required Variables
highTemp = 68
lowTemp = 63
lightState = 'on' # Current state of the light stored in a variable
buzzState = 'off'
lcdyon = 0
### ABOUT STRINGS ###
abouttext = ('Welcome, to my Home Automation system running on Python 3 on my RaspberryPi 2, examples showing how the program works are listed the other cards on this page, when you are finished being shown how the user interface functions. Return to the top of the page, navigate back to the home page and begin using the interface.')
toggle = ("Below is a toggle button, when you click a toggle button it switches between 2 values, they are 'TRUE' and 'FALSE', or it can also be interpreted as 'ON' and 'OFF'. It's primary use in this system is to switch lights and buzzers on and off, toggle the toggle button below to see what it does on this card.")
slider = ("A slider is an object that allows a range to be selected from by the method of sliding the dot to the value you desire, it's purpose in this system is to set the power output to a light to vary the brightness, or to set how much Red, Green or Blue is in an RGB LED. Drag the slider below to see what it does.")
button = ("Buttons are multi-purpose interactive objects in which do something when they are clicked if they are programmed to do so. Buttons on this system generally send values to some of the sensors and pull values from the rest to recieve an output, Buttons on this system are also used for pinging the buzzer to act as a doorbell and ring. Click the button below to see what happenes when it is clicked.")
txt = ("Text box's are used to enter strings of data which can be used in a number of ways, in this program, the text box's are used to enter what will be printed to the LCD screen by the user, the LCD screen has a maximum limit of 16 characters per row. Type below and click the button to print the text.")
label = ("Labels are strings which can be modified through the CSS, HTML and other forms of programming, in this program everything readable is a string and some solid blocks of colour are filled text lines to present a colour coded meaning such as green is on and red is off.")