I went a step further, not more features for upysh_ yet, but being able to execute a single MicroPython command remotely for being able to use all the Linux bash shell power for working on single command MicroPython output.
Here is a first simple sample, just demonstrating what it is:
Code: Select all
$ ./micropython mp.py 192.168.4.1 5**4**3
542101086242752217003726400434970855712890625
$
In
this posting I did describe how to remote control MicroPython from another MicroPython, using Danni's uwebsocket module. I tried to make use of that with python3, and after many changes to get uwebsocket module (because that is much easier to use than my webrepl_client.py python[23] remote console) to run with python 3 I ended up with this python3 error on difference to MicroPython:
Code: Select all
$ python3 echo_websocket_org.py
Traceback (most recent call last):
File "echo_websocket_org.py", line 2, in <module>
websocket = client.connect("ws://echo.websocket.org/")
File "/home/stammw/uwebsockets/client.py", line 59, in connect
header = sock.readline()[:-2]
AttributeError: 'socket' object has no attribute 'readline'
$
I gave up there and remembered on the unix port of MicroPython. With it uwebsockets module new
echo_websockets_org.py example directly worked:
Code: Select all
$ ./micropython
MicroPython v1.9.4-683-gd94aa57 on 2018-11-13; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import echo_websocket_org
The quick brown fox jumps over the lazy dog
>>>
At the start of this posting I already showed mp.py in action. It is running in unix port MicroPython, uses uwebsockets module to connect to ESP-01s WebREPL with IP address 192.168.4.1 and executed the passed command. Before I show the simple 25 line only mp.py, I will show the intended application (of mixing Linux bash, unix port MicroPython and ESP8266 MicroPython alltogether).
First my upysh_ module needs to be imported, after the ESP-01s was powered on (the quotes around star character are needed to prevent bash from expanding it):
Code: Select all
$ ./micropython mp.py 192.168.4.1 from upysh_ import '*'
upysh_ is intended to be imported using:
[import words (on systems with very small RAM)]
from upysh_ import *
...
grep(file, regex [, opt]), od(file [, opt]), mkdir(...), rmdir(...)
$
Now that upysh_ module is loaded, its "ls" command can get executed on ESP-01s (that sorts files alphabetically), and Linux bash can be used to sort by size. This should just be a simple example:
Code: Select all
$ ./micropython mp.py 192.168.4.1 ls | sort -n
0 empty
11 umlaute.txt
14 webrepl_cfg.py
23 rn.txt
29 numstr.txt
33 tst.txt
60 tloop.py
361 boot.py
384 256.dat
1571 upysh.py
6464 words.py
6633 upysh_.py
$
Now that you have seen how to use mp.py in unix port MicroPython, here it is:
Code: Select all
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:]))
So for doing a MicroPython session I will use upysh_ on the module itself, in a webrepl_client.py remote session.
And if more than the features and commands implemented in upysh_ is needed, I will use mp.py and Linux bash.
P.S:
P.P.S:
This is mixed pipeing example. The mp.py command needs to be quoted for dividing the vertical bars between MicroPython upysh_ processing and Linux bash shell:
Code: Select all
$ ./micropython mp.py 192.168.4.1 'pipe(lambda: micropython.qstr_info(1)) | (tee,"qstr.txt") | done' | sort | tail -6
Q(v)
Q(wc)
Q(webrepl_cfg.py)
Q(wloaded)
Q(words)
Q(x)
$
If teeing output into qstr.txt is not needed, the command line looks much easier:
Code: Select all
$ ./micropython mp.py 192.168.4.1 'micropython.qstr_info(1)' | sort | tail -6
Q(v)
Q(wc)
Q(webrepl_cfg.py)
Q(wloaded)
Q(words)
Q(x)
$
P.P.P.S:
For another application of mp.py see this posting (1000s of calls):
viewtopic.php?f=2&p=31863#p31863