-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
57 lines (48 loc) · 2.42 KB
/
server.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
# server.py
def run_server():
import socket # Import socket module
import serial # Import library for serial communication
import os #
import subprocess # import library for command line interface
import time # for delays and sleep
ser = serial.Serial('/dev/ttyUSB0',115200) # open UArt port on usb between pi and arduino
port = 50001 # Reserve a port for your service every new transfer wants a new port or you must wait.
s = socket.socket() # Create a socket object
#host = "192.168.0.18" # server ip from if config at home
host = '192.168.137.98' # serve ip at school from laptop hotspot using ifconfig
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print ('Server listening....')
while True:
conn, addr = s.accept() # Establish connection with client.
print ('Got connection from', addr)
# ________ Record audio file
recordCMD = "arecord -D hw:1,0 -d 5 -f cd -r 48000 rand.wav -c 1"
runAgain = input("Do you want to start? Enter y/n: ") #v hitting y and enter will automatically start recording
if runAgain == "y":
print("Will execute in command line\n"+ recordCMD) # for debugging
p = subprocess.Popen(recordCMD, shell=True, stdout=subprocess.PIPE)
print ("recording")
time.sleep(5) # since recording is for 5 seconds wait that long
print ("Done rec.")
else
break
# at this point you have a wav file
#data = conn.recv(1024) receives hello from client
#print('Server received', repr(data))
f = open('rand.wav','rb')
# conn.send(fileName.encode())
dataPacket = f.read(1024) # buffering file in packets to send to client
while (dataPacket):
conn.send(dataPacket)
print('Sent ',repr(dataPacket))
print('\n')
dataPacket = f.read(1024)
f.close()
print('Done sending file')
result = conn.recv(1024) # receives result from client
ser.write(result.encode()) # sends result to hadrware
conn.send(b'Thank you for connecting')
conn.close()
if __name__ == "__main__":
run_server()