Using webrepl instead of USB connection with Thonny IDE

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

Re: Using webrepl instead of USB connection with Thonny IDE

Post by laukejas » Sat Nov 14, 2020 7:51 pm

aivarannamaa wrote:
Fri Nov 13, 2020 10:18 pm
Nope, Thonny's Shell does not support capturing keystrokes. Actually, I don't see how you could capture keystrokes in MicroPython even if you were using a proper terminal emulator.

> Far fewer exceptions that in the previous build I tried

Do you mean misbehaviors of Thonny? Please report them at https://github.com/thonny/thonny/issues/new
I will, thanks! So there is absolutely no way to send keystrokes to a microcontroller over WiFi?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Using webrepl instead of USB connection with Thonny IDE

Post by aivarannamaa » Sat Nov 14, 2020 8:45 pm

laukejas wrote:
Sat Nov 14, 2020 7:51 pm
So there is absolutely no way to send keystrokes to a microcontroller over WiFi?
I'm not entirely sure, but I don't know how.

In a terminal emulator you probably can send combinations with Ctrl -- eg. Ctrl+F should send byte 0x06 (smaller values are special for MicroPython REPL). I haven't tried it, though and it won't work in Thonny.
Aivar Annamaa
https://thonny.org

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

Re: Using webrepl instead of USB connection with Thonny IDE

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

aivarannamaa wrote:
Sat Nov 14, 2020 8:45 pm
In a terminal emulator you probably can send combinations with Ctrl -- eg. Ctrl+F should send byte 0x06 (smaller values are special for MicroPython REPL). I haven't tried it, though and it won't work in Thonny.
I think laukejas is referring to viewtopic.php?f=2&t=9303

See my reply there. Admittedly this was tested on STM32 not ESP8266 but from MicroPython's perspective it's just bytes arriving on the UART, so it should be similar.

How does Thonny's shell work differently to, say, screen or miniterm.py? Is it line buffered?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Using webrepl instead of USB connection with Thonny IDE

Post by aivarannamaa » Sun Nov 15, 2020 6:43 am

jimmo wrote:
Sun Nov 15, 2020 4:30 am
How does Thonny's shell work differently to, say, screen or miniterm.py? Is it line buffered?
Yes, I think this is the right term for this. It doesn't send anything to the back-end until you press ENTER.

Actually, I thought that the same happens with terminal emulator + screen/tio/miniterm. Doesn't it? When MP code does `sys.stdout.read(2)` and I'm pressing "x", "y", "backspace", "z" in miniterm, what gets read? (Sorry, I don't have a device with me at the moment to try it out)
Aivar Annamaa
https://thonny.org

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

Re: Using webrepl instead of USB connection with Thonny IDE

Post by jimmo » Sun Nov 15, 2020 11:05 am

aivarannamaa wrote:
Sun Nov 15, 2020 6:43 am
Yes, I think this is the right term for this. It doesn't send anything to the back-end until you press ENTER.

Actually, I thought that the same happens with terminal emulator + screen/tio/miniterm. Doesn't it? When MP code does `sys.stdout.read(2)` and I'm pressing "x", "y", "backspace", "z" in miniterm, what gets read? (Sorry, I don't have a device with me at the moment to try it out)
The characters get sent directly. This is what allows things like tab completion to work on the REPL.

I assume you mean sys.stdin.read though?

To put it more concretly, the example code I gave in the other thread works exactly like you'd expect -- you can sys.stdin.read(1) and receive the characters exactly as the user types them (including control characters etc).

How does Thonny avoid the echo on the MicroPython REPL?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Using webrepl instead of USB connection with Thonny IDE

Post by aivarannamaa » Mon Nov 16, 2020 11:24 am

jimmo wrote:
Sun Nov 15, 2020 11:05 am
The characters get sent directly. This is what allows things like tab completion to work on the REPL.
Indeed. I never thought about tab-completion and arrow up/down, but now I understand that it must work like this.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
I assume you mean sys.stdin.read though?
Yes, my mistake.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
How does Thonny avoid the echo on the MicroPython REPL?
It just consumes the echo after submitting the input (which is always a single line ending with CRLF).
Aivar Annamaa
https://thonny.org

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

Re: Using webrepl instead of USB connection with Thonny IDE

Post by laukejas » Thu Nov 19, 2020 10:14 pm

aivarannamaa wrote:
Mon Nov 16, 2020 11:24 am
jimmo wrote:
Sun Nov 15, 2020 11:05 am
The characters get sent directly. This is what allows things like tab completion to work on the REPL.
Indeed. I never thought about tab-completion and arrow up/down, but now I understand that it must work like this.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
I assume you mean sys.stdin.read though?
Yes, my mistake.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
How does Thonny avoid the echo on the MicroPython REPL?
It just consumes the echo after submitting the input (which is always a single line ending with CRLF).
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Using webrepl instead of USB connection with Thonny IDE

Post by aivarannamaa » Fri Nov 20, 2020 6:49 am

laukejas wrote:
Thu Nov 19, 2020 10:14 pm
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?
Unfortunately there is no way for it. What you can do is

* use Thonny for saving your code to main.py
* select "Tools => Open system shell" which (starting from Thonny 3.3) opens your MicroPython repl in the Terminal
* press Ctrl+D to soft reboot and start your scrip
* send your keystrokes in normal way

I'm considering enabling "Run => Run current script in Terminal" in a future version and implementing automatic reconnect in Thonny when you have closed the terminal.
Aivar Annamaa
https://thonny.org

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

Re: Using webrepl instead of USB connection with Thonny IDE

Post by laukejas » Fri Nov 20, 2020 3:24 pm

aivarannamaa wrote:
Fri Nov 20, 2020 6:49 am
laukejas wrote:
Thu Nov 19, 2020 10:14 pm
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?
Unfortunately there is no way for it. What you can do is

* use Thonny for saving your code to main.py
* select "Tools => Open system shell" which (starting from Thonny 3.3) opens your MicroPython repl in the Terminal
* press Ctrl+D to soft reboot and start your scrip
* send your keystrokes in normal way

I'm considering enabling "Run => Run current script in Terminal" in a future version and implementing automatic reconnect in Thonny when you have closed the terminal.
Pressing "Open system shell" opens the shell, but I don't think WebREPL is running. All I'm seeing is plain command line, waiting for regular inputs, not like WebREPL. Am I missing some dependency or something?
Also, I am not quite sure I understand what you meant by "send your keystrokes in normal way", could you please explain?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Using webrepl instead of USB connection with Thonny IDE

Post by aivarannamaa » Fri Nov 20, 2020 3:58 pm

laukejas wrote:
Fri Nov 20, 2020 3:24 pm
Pressing "Open system shell" opens the shell, but I don't think WebREPL is running. All I'm seeing is plain command line, waiting for regular inputs, not like WebREPL. Am I missing some dependency or something?
Oops, I forgot that we were talking about WebREPL. In this case I can't offer any work-arounds at the moment.
laukejas wrote:
Fri Nov 20, 2020 3:24 pm
Also, I am not quite sure I understand what you meant by "send your keystrokes in normal way", could you please explain?
I meant just pressing the keys in terminal window and doing sys.stdin.read(1) in your MicroPython code. But at the moment Thonny provides terminal window for MicroPython REPL only via serial connection.
Aivar Annamaa
https://thonny.org

Post Reply