-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc_rpi_controller.py
63 lines (53 loc) · 1.51 KB
/
rc_rpi_controller.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
import io
import socket
import sys
import struct
import serial
import time
import pytz
import datetime
# Server socket intialization
server_socket = socket.socket()
server_socket.bind(('192.168.137.1', 59722))
print("Socket created and binded.")
#s.getsockname()
#print("Socket bound to", s.getsockname())
server_socket.listen(1)
print("Socket is listening.")
connection, add = server_socket.accept()
print("Got connection from ", add)
output = "Thanks for connection."
connection.sendall(output.encode('utf-8'))
# Arduino control
ser = serial.Serial("COM3", 9600, timeout=1)
try:
while True:
f = open('Control_Data.csv', 'w')
f.write("Command, Timestamp \n")
output = connection.recv(1024)
output = output.decode("utf-8")
if(len(output)==0):
ser.write(chr(0).encode())
ser.close()
break
print(output)
if output=="right":
#print("Forward Right")
ser.write(chr(6).encode())
f.write("Forward Right" + ",")
elif output=="left":
#print("Forward Left")
ser.write(chr(7).encode())
f.write("Forward Left" + ",")
elif output=="forward":
#print("Forward")
ser.write(chr(1).encode())
f.write("Forward" + ",")
utc_time = datetime.datetime.now(pytz.utc)
local_time = (utc_time.astimezone(pytz.timezone('Asia/Calcutta')))
date = (str(local_time)).split()[0]
f.write(str(local_time) + "\n")
finally:
f.close()
connection.close()
server_socket.close()