forked from larryRBerry/GoPiGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserv.py
More file actions
61 lines (53 loc) · 1.38 KB
/
Copy pathserv.py
File metadata and controls
61 lines (53 loc) · 1.38 KB
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
#!/usr/bin/env python
# This is a basic example for a socket server for the GoPiGo.
# This allows the client to connects can be used to respond to the commands and run the GoPiGo
# the socket server is running on Port 5005 on localhost
# Send a single byte command to the server from the client:
# s - stop
# f - move forward
# b - move back
# l - turn left
# r - turn right
import socket
import gopigo
# Listen on localhost at port 5005
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20
# Create a TCP socket and bind the server to the port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
print "Server Started"
while True:
#Wait for incomming connections
s.listen(1)
#Accept an incoming connection
conn, addr = s.accept()
print '\nConnection address:', addr
while 1:
#Check the data
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
if len(data) <> 1:
print ("Invalid command")
conn.send("Invalid command")
elif data=='f':
gopigo.fwd()
conn.send("Moving forward")
elif data=='s':
gopigo.stop()
conn.send("Stopping")
elif data=='b':
gopigo.bwd()
conn.send("Moving back")
elif data=='l':
gopigo.left()
conn.send("Turning left")
elif data=='r':
gopigo.right()
conn.send("Turning right")
else:
print ("Invalid command")
conn.send("Invalid command")
conn.close()