webrepl...

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
cyberlab
Posts: 55
Joined: Sat Apr 21, 2018 5:02 am

webrepl...

Post by cyberlab » Mon Nov 26, 2018 3:46 am

hi, is there some command with webrepl for a softreset and to run a py file or some way to do it via wifi? Thanks in advance. :P

User avatar
Roberthh
Posts: 1370
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: webrepl...

Post by Roberthh » Mon Nov 26, 2018 6:45 am

If you are in the REPL session:
Ctrl-D for soft reset
import xxx to run script xxx.py
If you want to have PC-scriptable solution, look at the work of @HerrmannSW

cyberlab
Posts: 55
Joined: Sat Apr 21, 2018 5:02 am

Re: webrepl...

Post by cyberlab » Tue Nov 27, 2018 4:20 am

Hello Roberthh, thanks always for answering, I designed a program in labview to send files to ESP8266 via wifi with this command: python webrepl_cli.py -p password filename.py 192.168.4.1:/filename.py I found it googling, and it works perfectly but at some point I need to send a sofreset via wifi, I understand that Ctrl + D only works via serial port, if you can also how would the complete command?
another question how can I add webrepl_cli.py to the path so that I can send files from any folder on my pc, not just where that file is located? :)

User avatar
Roberthh
Posts: 1370
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: webrepl...

Post by Roberthh » Tue Nov 27, 2018 6:30 am

You can cause a hard reset via command, it is:
machine.reset()
About the path for python modules: The list in sys.path can be extended by the value of PYTHONPATH.

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

Re: webrepl...

Post by HermannSW » Tue Nov 27, 2018 4:58 pm

Roberthh wrote:
Mon Nov 26, 2018 6:45 am
If you want to have PC-scriptable solution, look at the work of @HerrmannSW
In case you want an interactive command line remote shell to MicroPython, you can use WebREPL shell:
https://github.com/Hermann-SW/webrepl#webrepl-shell

As it sounds you are more interested in sending single commands to remote MicroPython (like "machine.reset()").
I did build such tool, currently mp.py needs to run under unix port of MicroPython; it allows to execute a command on remote MicroPython:
viewtopic.php?f=15&t=233&p=31812#p31846

Code: Select all

$ ./micropython mp.py 192.168.4.1 5**4**3
542101086242752217003726400434970855712890625
$ 
"./micropython" starts unix port micropython running script "mp.py".
The command "5**4**3" gets executed on ESP8266 module on 192.168.4.1.

Here you can see how to send CTRL-B, I don't know of a shorter method, hints welcome:

Code: Select all

$ ./micropython mp.py 192.168.4.1 `echo -en '\x02'`

MicroPython v1.9.4-272-g46091b8a on 2018-07-18; ESP module with ESP8266
Type "help()" for more information.
$ 
Sending CTRL-D does soft reset, and confuses mp.py -- but works:

Code: Select all

$ ./micropython mp.py 192.168.4.1 `echo -en '\x04'`

Traceback (most recent call last):
  File "mp.py", line 25, in <module>
  File "mp.py", line 15, in do
TypeError: 'NoneType' object isn't subscriptable
$ 
P.S:
I was not sure and just proved that indeed soft-reset happens with CTRL-D:

Code: Select all

$ ./micropython mp.py 192.168.4.1 import utime
$ ./micropython mp.py 192.168.4.1 utime
<module 'utime'>
$ ./micropython mp.py 192.168.4.1 `echo -en '\x04'`

Traceback (most recent call last):
  File "mp.py", line 25, in <module>
  File "mp.py", line 15, in do
TypeError: 'NoneType' object isn't subscriptable
$ ./micropython mp.py 192.168.4.1 utime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'utime' is not defined
$ ./micropython mp.py 192.168.4.1 "machine.reset_cause()"
0
$ 
Module utime is gone after sending CTRL-D proving soft-reset happened.
But reset-cause is wrong (should be 4, not 0):
https://docs.micropython.org/en/latest/ ... -constants

machine.reset() is better, does soft-reset as well, but results in correct reset_cause (4), at the price of having to press CTRL-C to terminate the command. So I would prefer sending \x04:

Code: Select all

$ ./micropython mp.py 192.168.4.1 import utime
$ ./micropython mp.py 192.168.4.1 utime
<module 'utime'>
$ ./micropython mp.py 192.168.4.1 "machine.reset()"
^CTraceback (most recent call last):
  File "mp.py", line 25, in <module>
  File "mp.py", line 12, in do
  File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 172, in recv
  File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 168, in recv
  File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 80, in read_frame
KeyboardInterrupt: 
$ ./micropython mp.py 192.168.4.1 utime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'utime' is not defined
$ ./micropython mp.py 192.168.4.1 "machine.reset_cause()"
4
$ 
Last edited by HermannSW on Wed Nov 28, 2018 7:20 am, edited 1 time in total.

cyberlab
Posts: 55
Joined: Sat Apr 21, 2018 5:02 am

Re: webrepl...

Post by cyberlab » Wed Nov 28, 2018 5:49 am

use something like this: python webrepl_cli.py -p password 192.168.4.1 "machine.reset ()", but it does not work

User avatar
Roberthh
Posts: 1370
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: webrepl...

Post by Roberthh » Wed Nov 28, 2018 6:43 am

maybe is should be:
python webrepl_cli.py -p password 192.168.4.1 "import machine;machine.reset ()",

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

Re: webrepl...

Post by HermannSW » Wed Nov 28, 2018 7:25 am

I agree that executing a single command finally should be possible with official tool webrepl_cli.py.
But that is wishful thinking:

Code: Select all

$ ~/webrepl/webrepl_cli.py -p abcd 192.168.4.1 "import machine"
One remote file is required
$ 
webrepl_cli.py enforces presence of ":" in 1st or 2nd argument and then does scp like copy, nothing more (currently):
https://github.com/micropython/webrepl/ ... li.py#L243

That is the reason I implemented WebREPL shell, and later mp.py to execute a single remote command.

@pfalcon once mentioned that MicroPython websocket module should be used instead of uwebsocket I use in mp.py, but also stated that that module is not documented, so not usable without much reverse engineering:
viewtopic.php?f=2&t=5392&p=31073&hilit= ... .py#p31082

mp.py has the disadvantage that it has to be run in unix port MicroPython, but that is not really a usage issue if you run Linux.
The whole mp.py is only 30 lines of code:

Code: Select all

$ cat mp.py
import uwebsockets.client
import sys

websocket = uwebsockets.client.connect('ws://'+sys.argv[1]+':8266/')

def do(cmd,out=True):
    inp = cmd+"\r\n"
    websocket.send(inp)

    resp = ""
    while not("\n>>> " in resp or resp.startswith(">>> ")):
        resp = websocket.recv()
        while inp != "" and resp != "" and inp[0] == resp[0]:
            inp = inp[1:]
            resp = resp[1:]
        if out and not("\n>>> " in resp or resp.startswith(">>> ")):
            print(resp,end='')


resp = websocket.recv()
assert resp == "Password: ", resp

do("abcd",False)

do(" ".join(sys.argv[2:]))
$ 

Perhaps reengineering undocumented MicroPython websocket module and getting "execute remote command" in should be tried.

Yes, it is so short because is uses all that websocket stuff from Danni's nicely working MicroPython uwebsocket implementation (that comes with working samples for client as well as server side):
https://github.com/danni/uwebsockets#mi ... bsocketorg

But code should not need to be much bigger in webrepl_cli.py when use of uwebsocket will be replaced by MicroPython's undocumented websocket module.


P.S:
There is no documentation, but at least websocket module example for the server side:
https://github.com/micropython/micropyt ... webrepl.py

It is weprepl module that provides WebREPL access to a module, started in boot.py by:

Code: Select all

import webrepl
webrepl.start()
which gets added by enabling WebREPL using "import webrepl_setup".

cyberlab
Posts: 55
Joined: Sat Apr 21, 2018 5:02 am

Re: webrepl...

Post by cyberlab » Wed Nov 28, 2018 6:23 pm

hello good day Roberthh & HermannSW. Roberthh, Neither works in this way, as HermannSW mentions, it appears: one remote file is required.
Excuse my Ignorance, I have few months that I knew about micropython, so Hermann, I understand you designed a way to send commands via wifi using the mp.py code, I have several doubts about it, you comment that only works in the Unix port, it means that to send you command I have to have a unix operating system installed?
if it's not too much trouble, can you be more specific so that a newbie like me can understand?
Thank you both again for your time!

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

Re: webrepl...

Post by HermannSW » Thu Nov 29, 2018 11:33 am

From what I heard MacOS has a unix like shell, should be doable there as well.

I am using Linux only since 2007, Raspbian since some years. There is only one program I have to run under virtual Win7 and one under virtual Win10 at IBM, everything else I do under Linux at work.

Even Microsoft has realized that they miss something and added Linux subsystem to Win10.

I tried to get webrepl_client.py running under Win7 cygwin python3, but after resolving many dependencies I struggled to get libuv1 working which is needed for python threading in websocket module.

Then I tried to compile micropython unix port under Win7 cygwin without success:
https://github.com/micropython/micropyt ... ix-version

So if you are running Windows you can either install a virtual Linux and use webrepl_client.py as well as mp.py with unix port MicroPython from there. Or you use just your browser to execute commands remotely on MicroPython module:
https://micropython.org/webrepl/?

That link does not allow to connect to MicroPython module for me, both under virtual Windows as well as under Linux.
But I did "git install" the webrepl project under cygwin (should be doable under Windows as well):
https://github.com/micropython/webrepl

As you can see opening the local webrepl.html works fine in Windows Firefox:
Image

Post Reply