-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizrealtime.py
64 lines (55 loc) · 1.54 KB
/
wizrealtime.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
import pywizlight
import pyaudio
import wave
import audioop
import math
import asyncio
async def turn_on_lights():
global LOCK_ON
global LOCK_OFF
if not LOCK_ON:
# Connect to the WiZ lights
first = pywizlight.wizlight("192.168.1.16")
second = pywizlight.wizlight("192.168.1.25")
# Turn on the lights
await first.turn_on()
await second.turn_on()
LOCK_ON = True
LOCK_OFF = False
async def turn_off_lights():
global LOCK_ON
global LOCK_OFF
if not LOCK_OFF:
# Connect to the WiZ lights
first = pywizlight.wizlight("192.168.1.16")
second = pywizlight.wizlight("192.168.1.25")
# Turn on the lights
await first.turn_off()
await second.turn_off()
LOCK_ON = False
LOCK_OFF = True
def get_volume(data):
# Calculate the volume in dB
volume = audioop.rms(data, 2)
volume = 20 * math.log10(volume)
return volume
async def main():
p = pyaudio.PyAudio()
THRESHOLD = 50
# Set up a stream to read from the microphone
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024)
while True:
data = stream.read(1024)
volume = get_volume(data)
if volume > THRESHOLD:
await turn_on_lights()
else:
await turn_off_lights()
LOCK_ON = False
LOCK_OFF = False
loop = asyncio.get_event_loop()
loop.run_until_complete(main())