enabled reconnect of socket
This commit is contained in:
parent
14ad366484
commit
efc34ed11e
|
@ -5,6 +5,7 @@ import d1motor
|
|||
|
||||
import time
|
||||
import usocket
|
||||
import esp
|
||||
|
||||
class Robot:
|
||||
def __init__(self):
|
||||
|
@ -20,16 +21,25 @@ class Robot:
|
|||
print("motor setup complete")
|
||||
|
||||
# setup socket for remote control
|
||||
print("setting up socket communication")
|
||||
self.socket = usocket.socket()
|
||||
self.addr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1]
|
||||
self.socket.bind(self.addr)
|
||||
print("socket setup complete")
|
||||
|
||||
def remote_control(self):
|
||||
while True:
|
||||
# dirty hack: free memory such that reuse of socket is possible
|
||||
print(esp.freemem())
|
||||
if esp.freemem() < 9 * 1024:
|
||||
s = time.ticks_ms()
|
||||
while esp.freemem() < 9 * 1024:
|
||||
pass
|
||||
print("waited {} ms".format((time.ticks_ms() - s)))
|
||||
print("setting up socket communication")
|
||||
socket = usocket.socket()
|
||||
socket.bind(self.addr)
|
||||
print("socket setup complete")
|
||||
|
||||
print("waiting for connections on {}".format(self.addr))
|
||||
self.socket.listen(1)
|
||||
res = self.socket.accept() # this blocks until someone connects to the socket
|
||||
socket.listen(1)
|
||||
res = socket.accept() # this blocks until someone connects to the socket
|
||||
comm_socket = res[0]
|
||||
print("connected!")
|
||||
listening = True
|
||||
|
@ -63,6 +73,11 @@ class Robot:
|
|||
finally:
|
||||
self.m1.speed(u1)
|
||||
self.m2.speed(u2)
|
||||
comm_socket.close()
|
||||
socket.close()
|
||||
del comm_socket
|
||||
del socket
|
||||
print("disconnected")
|
||||
|
||||
wall_e = Robot()
|
||||
wall_e.remote_control()
|
||||
|
|
Loading…
Reference in New Issue
Block a user