-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiServer.py
69 lines (57 loc) · 1.66 KB
/
PiServer.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
import socket
import serial as serial
import PiToArduino
from threading import Thread
import time
host = ''
port = 6009 # arbituary port #
def setupServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created.")
try:
s.bind((host, port))
except socket.error as msg:
print(msg)
print("Socket bind complete.")
return s
def setupConnection():
s.listen(2) # Allows one connection at a time.
conn, address = s.accept()
print("Connected to: " + address[0] + ":" + str(address[1]))
return conn
def get(b, data):
dataDict = {b: str(data[1])}
return dataDict[b] # insert raspi to arduino communication
def dataTransfer(conn):
while True: # A big loop that sends/receives data until told not to.
data = conn.recv(1024) # receive the data
data = data.decode('utf-8')
# Split the data such that you separate the command from the rest of the data.
dataMessage = data.split(' ', 1)
command = dataMessage[0]
if command == "x":
print(get("x", dataMessage))
break
elif command == 'z':
print(get("z", dataMessage))
break
elif command == 'KILL':
print("Server is shutting down.")
s.close()
break
conn.close()
s = setupServer()
#ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) #set up arduino comm.
#ser.flush()
#while True:
time.sleep(3)
PiToArduino.piToArduino()
#abc = "50 34\n"
#ser.write(abc.encode('utf-8'))
while True:
try:
conn = setupConnection()
dataTransfer(conn)
except:
print("Interrupt")
break