forked from RomanKosovnenko/MySmartRoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomLights.py
204 lines (164 loc) · 5.99 KB
/
RoomLights.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
from datetime import datetime, time, date, datetime, timedelta
from yeelight import Bulb
from astral import Astral
import RPi.GPIO as GPIO
import time as timer
import pytz
#LedStripControl_gpio
RledStrip_gpio = None
GledStrip_gpio = None
BledStrip_gpio = None
WledStrip_gpio = None
#LEDout_gpio
isOnled_gpio = 40
pirOnOffLed_gpio = 15
LedStripModeStatusLed_gpio = 38
BulbModeStatusLed_gpio = 36
#input_gpio
pir_gpio = 11
pirOnOffBtn_gpio = 13
onOffManualBtn_gpio = 31
changeModeBtn_gpio = 29
onOffLEDIndicatorsBtn_gpio = 33
#flags
areLED_indicators_enabled = True
isOn = False
isPirStopped = False
bulb_ip = "192.168.178.186"
lightMode = 1
#PIR mode settings
timeOn_duration = 10
timeWhenPirRestart = None
def changeStatusofLed(gpio, value):
if areLED_indicators_enabled:
GPIO.output(gpio, value)
def isNight(city: str='Berlin'):
"""
Checks if it is a night in the provided city.
The list of the cities is supported by the Astral library.
:param city: String name of interested city.
The list of supported cities could be found here:
https://astral.readthedocs.io/en/latest/#cities
"""
astral = Astral()
astral_location = astral[city]
current_time = datetime.now(pytz.utc)
sun_information = astral_location.sun(date=current_time, local=False) # Gets time of sunrise and sunset
# It is early morning (before sun dawn) or late evening (after sun dusk).
return sun_information['dawn'] >= current_time or sun_information['dusk'] <= current_time
def pirOnOffBtn_callback(channel):
changePirStatus()
if isPirStopped:
timeWhenPirRestart = (datetime.now() + timedelta(days=1)).date().strftime("%d/%m/%Y")
print(timeWhenPirRestart)
else:
timeWhenPirRestart = None
def onOffManualBtn_callback(channel):
togleLight()
def updateLED_indicators():
if areLED_indicators_enabled:
GPIO.output(isOnled_gpio, isOn)
GPIO.output(pirOnOffLed_gpio, isPirStopped)
# Change modes indicators according to mode
if lightMode == 1: # Only Bulb will be turned on
GPIO.output(LedStripModeStatusLed_gpio, False)
GPIO.output(BulbModeStatusLed_gpio, True)
elif lightMode == 2: # Only LED will be turned on
GPIO.output(LedStripModeStatusLed_gpio, True)
GPIO.output(BulbModeStatusLed_gpio, False)
elif lightMode == 3: # Both LED and Bulb will be turned on
GPIO.output(LedStripModeStatusLed_gpio, True)
GPIO.output(BulbModeStatusLed_gpio, True)
else:
GPIO.output(LedStripModeStatusLed_gpio, False)
GPIO.output(BulbModeStatusLed_gpio, False)
print("Unexpected lightMode: %s" % lightMode)
else:
for led in [LedStripModeStatusLed_gpio, BulbModeStatusLed_gpio, isOnled_gpio, pirOnOffLed_gpio]:
GPIO.output(led, False)
def changeModeBtn_callback(channel):
global lightMode
if lightMode == 1:
lightMode = 2
elif lightMode == 2:
lightMode = 3
elif lightMode == 3:
lightMode = 1
else:
print("Unexpected lightMode: %s" % lightMode)
updateLED_indicators()
def togleLight():
global isOn
if not isOn:
isOn = True
if lightMode == 1 or lightMode == 3:
bulb.turn_on()
elif lightMode == 2 or lightMode == 3:
print("Led strip is On")
else:
isOn = False
if lightMode == 1 or lightMode == 3:
bulb.turn_off()
elif lightMode == 2 or lightMode == 3:
print("Led strip is Off")
changeStatusofLed(isOnled_gpio, isOn)
print(isOn)
def changePirStatus():
global isPirStopped
isPirStopped = not isPirStopped
changeStatusofLed(pirOnOffLed_gpio, isPirStopped)
def checkPirRestart(isStillNight):
global timeWhenPirRestart
if (timeWhenPirRestart is not None) and not isStillNight and datetime.now().date().strftime("%d/%m/%Y") == timeWhenPirRestart:
changePirStatus()
timeWhenPirRestart = "None"
bulbrequests_count = 0
def checkBulbPowerStatus():
global bulbrequests_count
bulbrequests_count += 1
print(bulbrequests_count)
if bulbrequests_count == 60:
timer.sleep(10)
bulbrequests_count = 0
props = bulb.get_properties(['power'])
return props['power']
def OnOffLEDInticators_callback(channel):
global areLED_indicators_enabled
areLED_indicators_enabled = not areLED_indicators_enabled
updateLED_indicators()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(isOnled_gpio, GPIO.OUT)
GPIO.setup(pirOnOffLed_gpio, GPIO.OUT)
GPIO.setup(LedStripModeStatusLed_gpio, GPIO.OUT)
GPIO.setup(BulbModeStatusLed_gpio, GPIO.OUT)
GPIO.setup(pir_gpio, GPIO.IN)
GPIO.setup(pirOnOffBtn_gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(onOffManualBtn_gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(changeModeBtn_gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(onOffLEDIndicatorsBtn_gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(pirOnOffBtn_gpio, GPIO.RISING, callback=pirOnOffBtn_callback)
GPIO.add_event_detect(onOffManualBtn_gpio, GPIO.RISING, callback=onOffManualBtn_callback)
GPIO.add_event_detect(changeModeBtn_gpio, GPIO.RISING, callback=changeModeBtn_callback)
GPIO.add_event_detect(onOffLEDIndicatorsBtn_gpio, GPIO.RISING, callback=OnOffLEDInticators_callback)
changeStatusofLed(LedStripModeStatusLed_gpio, False)
changeStatusofLed(BulbModeStatusLed_gpio, True)
bulb = Bulb(bulb_ip)
if checkBulbPowerStatus() == 'on':
isOn = True
changeStatusofLed(isOnled_gpio, isOn)
while True:
isStillNight = isNight()
checkPirRestart(isStillNight)
if isStillNight and not isPirStopped and checkBulbPowerStatus() != 'on':
if GPIO.input(pir_gpio) == 0:
# if True:
timer.sleep(2)
if GPIO.input(pir_gpio) == 1:
# if False:
togleLight()
timer.sleep(timeOn_duration)
if not isPirStopped:
togleLight()
else:
timer.sleep(10)