-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
243 lines (190 loc) · 6.14 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
import RPi.GPIO as GPIO
import time
import adafruit_dht
from board import *
import os
from datetime import datetime
now = datetime.now()
def firebase_init():
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
credential = credentials.Certificate('sensorandnetwork-firebase-adminsdk-iy4wj-3e52cbe6c1.json')
firebase_admin.initialize_app(credential, {'databaseURL' : 'https://sensorandnetwork-default-rtdb.firebaseio.com/'})
dir = db.reference()
return dir
# DB에서 사용자의 요청을 읽어옴
# 아무 요청이 없을 시 비어잇는 문자열 반환
# status가 false일시 메세지 반환
def get_request(dir):
request = dir.child('message').get()
request_message = request['message']
request_status = request['status']
if request_status == 'False':
return request_message
else:
return 'Yet'
# record에 날짜, 메시지, 안밖 온도 저장
def save_record(dir, request_message, color):
from datetime import datetime
record_ref = dir.child('record')
date = datetime.today().strftime('%Y%m%d%H%M')
record = record_ref.push({'date' : date,
'message' : request_message})
decorate_ref = dir.child('Decorate')
date = datetime.today().strftime('%Y%m%d')
decorate = decorate_ref.push({'color' : color,
'date' : date})
dir = firebase_init()
# get_request함수를 10초마다 한번씩 실행
# 공백이 반환되면 반복문의 처음으로 돌아가서 재실행
# 문자열이 반환되면 탈출
while(True):
request_message = ''
print("readread")
request_message = get_request(dir)
time.sleep(3)
if request_message == 'Yet':
continue
else:
dir.child('message').set({'message' : request_message, 'status' : 'True'})
break
print(request_message)
# 파일에 메시지 저장
f = open("a.txt", mode = 'wt', encoding = 'utf-8')
f.write(request_message)
f.close()
#save_record(dir, request_message, 'blue', '15', '20')
isBlind = False #true = Blind state // #false = no blind state
red = 13
green = 19
blue = 26
DC = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
pwmBlue = GPIO.PWM(blue, 100)
pwmRed = GPIO.PWM(red, 100)
pwmGreen = GPIO.PWM(green, 100)
pwmBlue.start(0)
pwmRed.start(0)
pwmGreen.start(0)
f = open("a.txt")
line = f.readline()
#f.write("")
print(line)
isWlqwnd = "집중" in line #case1
isTodrkr = "생각" in line or "상상" in line or "아이디어" in line #case2
isGbtlr = "슬퍼" in line or "우울" in line or "속상해" in line #case3
isDnsehd = "운동" in line #case4
isDkswjd = "불안" in line or "초조" in line or "짜증" in line or "쉬고" in line #case5
isDkarl = "암기" in line or "외우기" in line or "기억" in line #case6
isEkdldjxm = "식욕" in line or "다이어트" in line or "빠져라" in line #case7
if isWlqwnd:
print("집중")
pwmBlue.ChangeDutyCycle(100)
pwmRed.ChangeDutyCycle(0)
pwmGreen.ChangeDutyCycle(0)
save_record(dir, request_message, 'blue')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case1.py")
elif isTodrkr:
print("생각")
pwmBlue.ChangeDutyCycle(0)
pwmRed.ChangeDutyCycle(100)
pwmGreen.ChangeDutyCycle(100)
save_record(dir, request_message, 'yellow')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case2.py")
elif isGbtlr:
print("휴식")
pwmBlue.ChangeDutyCycle(98)
pwmRed.ChangeDutyCycle(38)
pwmGreen.ChangeDutyCycle(86)
save_record(dir, request_message, 'green')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case3.py")
elif isDnsehd:
print("운동")
pwmBlue.ChangeDutyCycle(0)
pwmRed.ChangeDutyCycle(100)
pwmGreen.ChangeDutyCycle(0)
save_record(dir, request_message, 'red')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case4.py")
elif isDkswjd:
print("안정")
pwmBlue.ChangeDutyCycle(0)
pwmRed.ChangeDutyCycle(0)
pwmGreen.ChangeDutyCycle(100)
save_record(dir, request_message, 'green')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case5.py")
elif isDkarl:
print("암기")
pwmBlue.ChangeDutyCycle(0)
pwmRed.ChangeDutyCycle(100)
pwmGreen.ChangeDutyCycle(100)
save_record(dir, request_message, 'yellow')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case6.py")
elif isEkdldjxm:
print("다이어트")
GPIO.output(red,False)
GPIO.output(green, False)
GPIO.output(blue, True)
save_record(dir, request_message, 'blue')
if now.hour >= 6 or now.hour <= 18:
if isBlind:
os.system("python3 blind_close.py")
isBlind = True
else:
if not isBlind:
os.system("python3 blind_open.py")
isBline = False
os.system("python3 case7.py")
else:
print("no no no no")