possible to send command directly to WebREPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: possible to send command directly to WebREPL

Post by HermannSW » Wed Sep 19, 2018 6:40 am

Much more work done, exception handling, now runs with python v2 as well as v3, silent mode became default, added help() and made webrepl_client.py PEP8 compliant. Finally I created github pull request on adding webrepl_client.py remote shell using MicroPython WebREPL protocol (includes documentation):
https://github.com/micropython/webrepl/pull/37

This is final script (for now):
https://github.com/Hermann-SW/webrepl/b ... _client.py

Code: Select all

$ webrepl_client.py 
webrepl_client.py - remote shell using MicroPython WebREPL protocol
Arguments:
  <host> [-v] - open remote shell (to <host>:8266)
Examples:
  webrepl_client.py 192.168.4.1
  webrepl_client.py 192.168.4.1 -v
Special command control sequences:
  "\n"        - end of command in normal mode
  "\x01"      - switch to raw mode
  "\x02"      - switch to normal mode
  "\x03"      - interrupt
  "\x04"      - end of command in raw mode
  just "exit" - end shell

  "\n" gets auto appended without "-v".
  Therefore "-v" is needed for raw mode.
$ 

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: possible to send command directly to WebREPL

Post by HermannSW » Mon Sep 24, 2018 3:07 pm

I did a complete rewrite, no old escapes remained, and display with webrepl_client.py now is the same as in a "screen" terminal session besides a minor detail in raw mode. Unlike webrepl.html in browser, webrepl_client.py can deal with raw mode as well, in addition to normal mode and paste mode. And it is still runnable with Python v2 as well as v3, and PEP8 compliant. The documentation is up to date as well:
https://github.com/Hermann-SW/webrepl#webrepl-shell

Here is sample session from there showing all three modes, and that password input as well as raw mode input now is invisible as in "screen" session:

Code: Select all

$ webrepl_client.py 192.168.4.1 
Password: 

WebREPL connected
>>> A
raw REPL; CTRL-B to exit
>
OK>
MicroPython v1.9.4-8-ga9a3caad0 on 2018-05-11; ESP module with ESP8266
Type "help()" for more information.
>>> a
42
>>> E
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== a=43
=== 
>>> a
43
>>> 4**3**2
262144
>>> exit
### closed ###
$

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: possible to send command directly to WebREPL

Post by HermannSW » Mon Sep 24, 2018 4:32 pm

I did all the work with MicroPython on ESP-01s. Now I flashed MicroPython on one of my ESP32 with Oled modules, enabled WebREPL and connected to it with webrepl_client.py without issues:

Code: Select all

$ webrepl_client.py 192.168.4.1
Password: 

WebREPL connected
>>> B
MicroPython v1.9.4 on 2018-05-11; ESP32 module with ESP32
Type "help()" for more information.
>>> 5**4**3
542101086242752217003726400434970855712890625
>>> 

P.S:
Detailed steps for configuring ESP32 with Oled here:
https://www.esp32.com/viewtopic.php?f=2 ... 240#p31240
Image

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

Re: possible to send command directly to WebREPL

Post by aivarannamaa » Tue Oct 02, 2018 2:41 pm

Whoa! Great job!
I hope your pull request gets accepted.

(Sorry for not checking this thread for so long)

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: possible to send command directly to WebREPL

Post by HermannSW » Tue Oct 02, 2018 7:42 pm

This posting is not directly related to this thread, but my main motivation for creating webrepl_client.py.
Yesterday I completed ESP01s servo bomb drop mechanism:
https://www.esp8266.com/viewtopic.php?f ... 454#p78453

The drop was executed in a wireless MicroPython remote shell by just
(ESP01s runs MicroPython and is access point):

Code: Select all

>>> bomb.drop()
>>> 
I took a 180fps slowmo, this animated .gif created from uploaded youtube video plays 14.4 timese slower than real:
Image

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: possible to send command directly to WebREPL

Post by HermannSW » Mon Oct 15, 2018 11:10 pm

skimj wrote:
Thu Mar 09, 2017 5:13 pm
Is there any trivial way to programmatically send a single command directly to WebREPL?

For a simple, brute example, imagine that you need to reset the device. Your options are: 1) physically press reset button or 2) start a browser with the WebREPL client, press connect, login, and finally type:`machine.reset()` at the terminal prompt. It would be really cool if it were possible to do something like: `webrepl_cli.cmd('machine.reset()')`
webrepl_client.py is the big solution for Python 2/3.

Regarding "trivial" in your first sentence, there really is a solution making use of nice small uwebsockets.client from danni's repo. Details under "From MicroPython to MicroPython" in this posting:
viewtopic.php?f=2&t=5351&p=30829#p30829

This is relevant part showing for what you asked:

Code: Select all

...
websocket = uwebsockets.client.connect('ws://192.168.178.122:8266/')
...
resp = websocket.recv()
assert resp == "Password: ", resp

do("abcd")

do("import payload")

while True:
    do("payload.flip()")
    time.sleep(1)
$ 

Post Reply