-
Notifications
You must be signed in to change notification settings - Fork 8
/
Raspend.py
202 lines (166 loc) · 6.91 KB
/
Raspend.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
#This module contains the code for controlling devices by giving output to GPIO pins.
#when the mode will be automatic we will take data from sensors and on the basis of sensor
#outout we will decide which appliances we want to turn on and which ones to turn off.
#when the mode will be manual this code will fetch data from Google Sheets given by user and give the output
#on GPIO pins accordingly.
#import gspread for manipulating google spreadsheets, RPi.GPIO for using GPIO functions,
#ServiceAccountCredentials from oauth2client.service_account for authorising access to API
#dht11 for using dht11 temperature sensor.
import gspread
import RPi.GPIO as GPIO
import time
from threading import Thread
from oauth2client.service_account import ServiceAccountCredentials
import dht11
import datetime
scope = ['https://spreadsheets.google.com/feeds' , 'https://www.googleapis.com/auth/drive']
#secret key for accessing account is present in client_secret.json file
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
#authorise access using credentials
client = gspread.authorize(creds)
#open all the sheets (Client, Status, Mode)
sheet1 = client.open('Client').sheet1
sheet2 = client.open('Status').sheet1
sheet3 = client.open('Mode').sheet1
#variables denoting the state of appliances
global L1,L2,F1,F2,mode
#function for printing the rows of given sheet, sheet name should be passed as arguments.
def printSheet(sheet) :
row_count=len(sheet.get_all_records())+1
for i in range(row_count) :
RowInfo = sheet.row_values(i+1)
print(RowInfo)
#GPIO.BCM option means that you are referring to the pins by the 'Broadcom SOC channel'
#GPIO.cleanup() is used to cleanup all the ports that you have used.
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
#setting pins 4, 17, 23, 24 of Raspberry Pi's GPIO pins in GPIO.OUT mode so devices connected to them can act as
#output devices
#Light 1 and 2 are connected to pin 4, 17 of GPIO resepectively. And Fan 1 and Fan 2 are connected to pin 23, 24.
GPIO.setup(4,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
#fetching current mode from Mode spreadsheet, mode 1 denotes manual mode and mode 0 denotes automatic mode.
mode = int(sheet3.cell(1,2).value.encode('utf-8'))
#lux contains the output of Light sensor, temperature contains the output of temperature sensor, lux_thresh1 and lux_thresh2
#are the thresholds for lux so we can decide below what level we have to turn lights on and either one or both of them
#should be turned on, and above what level we have to turn them off.
#Same is for temp_thresh1, temp_thresh2
#no_of_person contains the number of persons present in the room. We are calculating it by subutracting the no of persons exiting
#the room from the number of people entered in room
global lux, temperature, lux_thresh1, lux_thresh2, temp_thresh1, temp_thresh2, no_of_person
lux = 0
temperature = 0
lux_thresh1 = 200
lux_thresh2 = 400
temp_thresh2 = 20
temp_thresh1 = 30
no_of_person = 0
#setitng pin 8, 21 of Raspberry Pi's GPIO pins in GPIO.IN mode so devices connected to them can act as input devices.
#we are going to use these pins for connecting sensors.
#pin 8 and 21 is for entry PIR Sensor and exit PIR sensor respectively.
GPIO.setup(8,GPIO.IN)
GPIO.setup(21,GPIO.IN)
#function for continuosly calculating the number of person entering in room. #PIR sesnor for entry is connected to
#pin 8 of GPIO.
def PIR_Sensor_forEntry():
global no_of_person
time.sleep(2)
while True:
if(GPIO.input(8)):
no_of_person += 1
print("no of person = ", no_of_person)
time.sleep(6)
#function to continuously calculate the number of person exiting the room.
def PIR_Sensor_forExit():
global no_of_person
time.sleep(2)
while True:
if(GPIO.input(21)):
no_of_person -= 1
print("no of person = ", no_of_person)
time.sleep(6)
#this function will continuously check the temperature of the surroundings. Temperature sensor(DHT11) is connected to
#pin 14 of GPIO.
def temperatureSensor() :
global temperature
instance = dht11.DHT11(pin=14)
while True:
result = instance.read()
if result.is_valid():
print("Last valid input: " + str(datetime.datetime.now()))
print("Temperature: %d C" % result.temperature)
print("Humidity: %d %%" % result.humidity)
temperature = result.temperature
time.sleep(1)
#this function will continuous check the mode, it will continuously fetch the input given given by the user
# using google spreadsheet and will change state of devices accordingly using GPIO pins. And if the mode changes to automatic
#it will stop doing the above written functionality. This function will work for manual mode.
def t1():
global mode
print("t1")
print(mode)
while True:
if(mode == 1):
mode = int(sheet3.cell(1,2).value.encode('utf-8'))
time.sleep(2)
L1 = int(sheet1.cell(2,2).value.encode('utf-8'))
L2 = int(sheet1.cell(3,2).value.encode('utf-8'))
F1 = int(sheet1.cell(4,2).value.encode('utf-8'))
F2 = int(sheet1.cell(5,2).value.encode('utf-8'))
GPIO.output(4,L1)
GPIO.output(17,L2)
GPIO.output(23,F1)
GPIO.output(24,F2)
print(L1,L2,F1,F2)
#This code will execute in automatic mode. This function will continuously fetch the mode, and it will continuously fetch data
#from sensors and will change the status of devices automatically.
def t2():
global mode
global no_of_person
global temperature
global lux
global lux_thresh1, lux_thresh2, temp_thresh1, temp_thresh2
print(mode)
while True:
if(mode == 0):
mode = int(sheet3.cell(1,2).value.encode('utf-8'))
time.sleep(2)
L1 = int(sheet2.cell(2,2).value.encode('utf-8'))
L2 = int(sheet2.cell(3,2).value.encode('utf-8'))
F1 = 0
F2 = 0
if(no_of_person >= 1):
if(temperature <= temp_thresh1 and temperature >= temp_thresh2):
F1 = 1
elif(temperature > 30) :
F1 = 1
F2 = 1
if(lux > lux_thresh2) :
if(L1 == 1 and L2 == 1) :
L2 = 0
elif(L1 == 1) :
L1 = 0
elif(L2 == 1) :
L2 = 0
elif(lux < lux_thresh1) :
if(L1 == 0 and L2 == 0) :
L1 = 1
elif(L1 == 1 and L2 == 0) :
L2 = 1
elif(L1 == 0 and L2 == 1) :
L1 = 1
GPIO.output(4,L1)
GPIO.output(17,L2)
GPIO.output(23,F1)
GPIO.output(24,F2)
sheet2.update_cell(2,2,L1)
sheet2.update_cell(3,2,L2)
sheet2.update_cell(4,2,F1)
sheet2.update_cell(5,2,F2)
Thread(target=t1).start()
Thread(target=t2).start()
Thread(target = PIR_Sensor_forEntry).start()
Thread(target = PIR_Sensor_forExit).start()
Thread(target = temperatureSensor).start()