-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver6.py
138 lines (101 loc) · 3.22 KB
/
server6.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import pyaudio
import asyncio
import RPi.GPIO as GPIO
from apa102_pi.colorschemes import colorschemes
BUTTON = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN)
NUM_LED = 430
# PyAudio configuration
CHUNK = 1024
CHANNELS = 2
RATE = 16000
OUTPUT = True
RESPEAKER_WIDTH = 2
NUM_LED = 430
INPUT = True
# Server configuration
HOST1 = '192.168.1.40'
HOST2 = '192.168.1.50'
PORT = 65000
BACKLOG = 5
SIZE = 1024
listening = False
async def rainbow(status):
while True :
MY_CYCLE = colorschemes.TheaterChase(num_led=NUM_LED, pause_value=0.04,
num_steps_per_cycle=35, num_cycles=5)
MY_CYCLE.start()
async def white(status):
while True:
MY_CYCLE = colorschemes.Solid(num_led=NUM_LED, pause_value=3,
num_steps_per_cycle=1, num_cycles=1)
MY_CYCLE.start()
p = pyaudio.PyAudio()
async def echo_server(reader, writer):
global listening
listening = True
print ("listening:",listening)
print ("\nwelcome\n",writer._transport.get_extra_info('peername'))
stream = p.open(format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=CHANNELS,
rate=RATE,
output=OUTPUT)
while True:
data = await reader.read(CHUNK) # Max number of bytes to read
# data = conn.recv(SIZE)
if not data:
break
stream.write(data)
# await writer.drain() # Flow control, see later
stream.stop_stream()
stream.close()
listening =False
print ("listening:",listening)
await send_sound()
await asyncio.sleep(0)
# p.terminate()
async def send_sound():
print("we are in")
global listening
if not GPIO.input(BUTTON) and not listening:
print ("listening:",listening)
stream = p.open(format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=CHANNELS,
rate=RATE,
input=INPUT,
input_device_index=2,
frames_per_buffer=CHUNK)
'''Socket server initialization'''
# connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connection.connect((HOST, PORT))
client_socket = socket.socket() # instantiate
client_socket.connect((HOST2, PORT)) # connect to the server
print('Three Seconds of white light')
while( not GPIO.input(BUTTON) ):
data = stream.read(CHUNK)
client_socket.send(data)
for i in range(5):
data = stream.read(CHUNK)
client_socket.send(data)
client_socket.close()
stream.stop_stream()
stream.close()
# p.terminate()
print("terminated")
await asyncio.sleep(0)
async def main(host, port):
print ("start")
server = await asyncio.start_server(echo_server, host, port)
loop = asyncio.get_event_loop()
print (loop)
task = asyncio.create_task(send_sound())
print ("loop,future:",loop,task)
async with server:
await asyncio.gather(
server.serve_forever(), task )
# await server.serve_forever()
asyncio.run(main(HOST1, PORT))