fixed issue with reconnection problem after socket is closed

This commit is contained in:
Simon Pirkelmann 2019-06-05 08:04:26 +02:00
parent 4d7c0e7be7
commit 69c40a3fe2

View File

@ -9,35 +9,39 @@ import esp
class Robot: class Robot:
def __init__(self): def __init__(self):
print("setting up I2C") print("setting up I2C ...")
d1 = Pin(5) d1 = Pin(5)
d2 = Pin(4) d2 = Pin(4)
i2c = I2C(scl=d1, sda=d2) i2c = I2C(scl=d1, sda=d2)
addr = i2c.scan()[0] i2c_addr = i2c.scan()[0]
print("i2c scan = {}".format(addr)) print("i2c scan = {}".format(i2c_addr))
print("setting up motors") print("setting up motors ...")
self.m1 = d1motor.Motor(0, i2c, address=addr) self.m1 = d1motor.Motor(0, i2c, address=i2c_addr)
self.m2 = d1motor.Motor(1, i2c, address=addr) self.m2 = d1motor.Motor(1, i2c, address=i2c_addr)
print("motor setup complete") self.m1.speed(0)
self.m2.speed(0)
print("motor setup complete!")
ip = my_ip[0]
# setup socket for remote control # setup socket for remote control
self.addr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1] self.addr = usocket.getaddrinfo(ip, 1234)[0][-1]
def remote_control(self): def remote_control(self):
while True: while True:
# dirty hack: free memory such that reuse of socket is possible print("setting up socket communication ...")
print(esp.freemem()) socket_setup_complete = False
if esp.freemem() < 9 * 1024: while not socket_setup_complete:
s = time.ticks_ms() try:
while esp.freemem() < 9 * 1024: print("trying to create socket on address {}".format(self.addr))
pass
print("waited {} ms".format((time.ticks_ms() - s)))
print("setting up socket communication")
socket = usocket.socket() socket = usocket.socket()
socket.bind(self.addr) socket.bind(self.addr)
print("socket setup complete") print("socket setup complete!")
socket_setup_complete = True
except Exception as e:
print("could not create socket. error msg: {}\nwaiting 1 sec and retrying...".format(e))
time.sleep(1.0)
print("waiting for connections on {}".format(self.addr)) print("waiting for connections on {} ...".format(self.addr))
socket.listen(1) socket.listen(1)
res = socket.accept() # this blocks until someone connects to the socket res = socket.accept() # this blocks until someone connects to the socket
comm_socket = res[0] comm_socket = res[0]
@ -49,25 +53,25 @@ class Robot:
# ui \in [-1.0, 1.0] # ui \in [-1.0, 1.0]
data = comm_socket.readline() data = comm_socket.readline()
data_str = data.decode() data_str = data.decode()
print("Data received: {}".format(data_str)) #print("Data received: {}".format(data_str))
try: try:
print("processing data = {}".format(data_str)) #print("processing data = {}".format(data_str))
l = data_str.strip('()\n').split(',') l = data_str.strip('()\n').split(',')
print("l = {}".format(l)) #print("l = {}".format(l))
u1 = int(float(l[0])*10000) u1 = int(float(l[0])*100)
print("u1 = {}".format(u1)) #print("u1 = {}".format(u1))
u2 = int(float(l[1])*10000) u2 = int(float(l[1])*100)
print("u2 = {}".format(u2)) #print("u2 = {}".format(u2))
except ValueError: except ValueError:
print("ValueError: Data has wrong format.") print("ValueError: Data has wrong format.")
print("Data received: {}".format(data_str)) print("Data received: {}".format(data_str))
print("Shutting down") print("Shutting down ...")
u1 = u2 = 0 u1 = u2 = 0
listening = False listening = False
except IndexError: except IndexError:
print("IndexError: Data has wrong format.") print("IndexError: Data has wrong format.")
print("Data received: {}".format(data_str)) print("Data received: {}".format(data_str))
print("Shutting down") print("Shutting down ...")
u1 = u2 = 0 u1 = u2 = 0
listening = False listening = False
finally: finally:
@ -77,7 +81,7 @@ class Robot:
socket.close() socket.close()
del comm_socket del comm_socket
del socket del socket
print("disconnected") print("disconnected!")
wall_e = Robot() wall_e = Robot()
wall_e.remote_control() wall_e.remote_control()