Listening for keypress/keydown over WebREPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Listening for keypress/keydown over WebREPL

Post by laukejas » Fri Nov 13, 2020 8:24 pm

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).

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Listening for keypress/keydown over WebREPL

Post by jimmo » Sat Nov 14, 2020 4:51 am

laukejas wrote:
Fri Nov 13, 2020 8:24 pm
Can anyone advise on how I can implement this?
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
If you want do other things in the background then you can use poll with a zero timeout to see if there are new characters available:

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

laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Re: Listening for keypress/keydown over WebREPL

Post by laukejas » Sat Nov 14, 2020 6:15 am

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:

Image

Did I misunderstand how to use this?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Listening for keypress/keydown over WebREPL

Post by jimmo » Sun Nov 15, 2020 4:31 am

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

Post Reply