Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW Hermann-SW/mp.js
Last active Nov 30, 2018

Embed
What would you like to do?
Node.js script allowing for remote MicroPython command execution
#!/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);
}
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; }
}
@Hermann-SW

This comment has been minimized.

Copy link
Owner Author

commented Nov 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.