webrepl...

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: webrepl...

Post by HermannSW » Thu Nov 29, 2018 10:12 pm

This is just a headsup, I am nearly done with nodejs script mp.js running a command on remote MicroPython (like mp.py). That should allow to send remote MicroPython commands on any OS with installed nodejs. And mp.js is really small:

Code: Select all

$ wc --lines mp.js
27 mp.js
$ 
$ ./mp.js
mp.js pwd host cmd [...]
$ 
You can do simple stuff:

Code: Select all

$ node mp.js abcd 192.168.4.1 5**4**3
542101086242752217003726400434970855712890625
$ 
$ ./mp.js abcd 192.168.4.1 'print("ab\ncd")'
ab
cd
$ 
And more:

Code: Select all

$ ./mp.js abcd 192.168.4.1 import utime
$ ./mp.js abcd 192.168.4.1 utime.localtime"()"
(2000, 1, 1, 6, 13, 30, 5, 1)
$ ./mp.js abcd 192.168.4.1 import ntptime
$ ./mp.js abcd 192.168.4.1 ntptime.settime"()"
$ ./mp.js abcd 192.168.4.1 utime.localtime"()"
(2018, 11, 29, 22, 9, 2, 3, 333)
$ 
With correct escaping and imported upysh_ module you can even do pipelining in MicroPython on the command line:

Code: Select all

$ ./mp.js abcd 192.168.4.1 'pipe("boot.py") | (head, 14) | (tail,4) | done'
#import network
#sta_if = network.WLAN(network.STA_IF)
#sta_if.active(True)
#sta_if.connect("MicroPython-5cd6ae", "12345678")
$ 
Work in progress, but posting mp.js is not far away ... ;-)
Last edited by HermannSW on Thu Nov 29, 2018 11:05 pm, edited 2 times in total.
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

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

Re: webrepl...

Post by HermannSW » Thu Nov 29, 2018 10:42 pm

OK, here it is.
In order to use mp.js, you need to install npm on your system, and then Node.js WebSocket library "ws":
https://www.npmjs.com/package/ws

You find complete mp.js script here:
https://gist.github.com/Hermann-SW/fbed ... 756bead886

It consists of two parts. First part just does command line argument processing and variable initialization:

Code: Select all

#!/usr/bin/env node

[scpt,pwd,host] = process.argv.slice(1);
cmd  = process.argv.slice(4).join(" ");
out  = 0;
last = "";

if (process.argv.length < 5) {
    console.log((require("path")).basename(scpt) + " pwd host cmd [...]");
    process.exit(1);
}
...

The second part does WebSocket connection against MicroPython host with enabled WebREPL, send password and command in ws.onopen and have tricky finite state machine absorbing what is returned from remote MicroPython module but not wanted as output in ws.onmessage:

Code: Select all

...
var ws = new (require('ws'))("ws://" + host + ":8266/");

ws.onopen = function () { ws.send(pwd + "\r\n" + cmd + "\r\n"); }

ws.onmessage = function (ev) {
    if ((last == "\r\n") && (ev.data == ">>> "))  { out = 0; ws.terminate(); }
    if (out == 2)                           { process.stdout.write(ev.data); }
    if (ev.data == "\r\nWebREPL connected\r\n>>> ")               { out = 1; }
    last = ev.data.slice(-2);
    if ((out == 1) && (last == "\r\n"))                           { out = 2; }
}

P.S:
As with mp.py before and in addition to the examples in previous posting, mp.js can be used to pipe interesting MicroPython information into bash shell for further processing. This gives the last 6 interned strings of MicroPython module when sorted:

Code: Select all

$ node mp.js abcd 192.168.4.1 import micropython
$ ./mp.js abcd 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)
$ 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

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

Re: webrepl...

Post by cyberlab » Sat Dec 08, 2018 2:44 am

good day to all, following the advice of Roberthh who gave me to extend the list in sys.path to add webrepl_cli.py but all the examples that I found as do it in windows, I still do not work I get the message that can not open webrepl_cli.py, only works if I keep a copy of webrepl_cli.py in each folder that wants to send files: python webrepl_cli.py -p password file.xxx 192.168.4.1:/file.xxx, can someone guide me how to do it please? first of all, Thanks!

Post Reply