Hi,
I am building a ESP8266-based robot for learning SLAM, and I also want to be able to control it manually over WiFi. I can send commands over WebREPL to initialize various movement commands (move for certain duration, rotate, etc.), but it is not convenient, because I have to press Enter after every command. It would be much better to utilize arrow keys and "drive" it directly.
For that, I need to listen to keyboard inputs over WebREPL. In full Python, there are various libraries that handle this, like Pygame, Keyboard, Curses, Pynput, Msvcrt, and so on, but none of them are ported to MicroPython, and somehow I don't think they would work over WebREPL anyway.
Can anyone advise on how I can implement this? I don't need anything fancy, just for ESP8266 to do basic detection when a certain key is pressed and released on my PC over WebREPL. I suspect this could be done with a web interface hosted by ESP8266, but it would be so much more convenient to have a listener function that I could call from within my IDE (I'm using Thonny).
Listening for keypress/keydown over WebREPL
Re: Listening for keypress/keydown over WebREPL
The way WebREPL works is that it uses "dupterm" to duplicate the stdin/stdout. I haven't tried this on ESP8266 but I think you should be able to use sys.stdin.read(1) to do a blocking read of stdin.
Code: Select all
while True:
cmd = sys.stdin.read(1)
if cmd == 'a':
...etc
Code: Select all
import select
p = select.poll()
p.register(sys.stdin)
MP_STREAM_POLL_RD = const(1)
while True:
# poll returns a list of (stream, flags). We only registered one stream, so must be stdin.
_, flags = p.poll(0)[0]
if flags & MP_STREAM_POLL_RD:
cmd = sys.stdin.read(1)
if cmd == 'a':
... etc
else:
...no input... do other stuff
Re: Listening for keypress/keydown over WebREPL
Thanks, jimmo, but how exactly do I get this to work? I tried running the first script you provided via Thonny in WebREPL mode, then went to the console, and tried pressing 'a', key, but the command does not get executed. Here is a screenshot:
Did I misunderstand how to use this?
Did I misunderstand how to use this?
Re: Listening for keypress/keydown over WebREPL
I think this might be a question for Thonny and/or WebREPL (I was testing this with a direct VCP serial connection). See my reply on the other thread -- viewtopic.php?f=2&t=9187&p=52327#p52327