From 254a4e25ae699ae8b775ac676db919569034921c Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Sat, 13 Apr 2019 14:25:41 +0200 Subject: [PATCH] added wireless control via sockets --- micropython_firmware/main.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/micropython_firmware/main.py b/micropython_firmware/main.py index 97db979..8dcafff 100644 --- a/micropython_firmware/main.py +++ b/micropython_firmware/main.py @@ -54,16 +54,16 @@ class Robot: def spin_right(self, seconds): print("You spin my head right round, right round..") - self.m1.control(0.8) - self.m2.control(-0.8) + self.m1.control(1.0) + self.m2.control(-1.0) time.sleep(seconds) self.m1.control(0.0) self.m2.control(0.0) def spin_left(self, seconds): print("spinning left") - self.m1.control(-0.8) - self.m2.control(0.8) + self.m1.control(-1.0) + self.m2.control(1.0) time.sleep(seconds) self.m1.control(0.0) self.m2.control(0.0) @@ -75,3 +75,30 @@ class Robot: self.backward(0.5) wall_e = Robot() + +import usocket + +# wait for connections on port 1234 +mysocket = usocket.socket() +myaddr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1] +mysocket.bind(myaddr) +mysocket.listen(1) +res = mysocket.accept() # this blocks until someone connects to the socket +comm_socket = res[0] + +# read data until 'q\n' is sent +listening = True +while listening: + data = comm_socket.readline() + data_str = data.decode() + print(data_str) + if data_str == 'f\n': + wall_e.forward(0.5) + elif data_str == 'b\n': + wall_e.backward(0.5) + elif data_str == 'l\n': + wall_e.spin_left(0.5) + elif data_str == 'r\n': + wall_e.spin_right(0.5) + elif data_str == 'q\n': + listening = False