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.
Post Reply
skimj
Posts: 3
Joined: Thu Mar 09, 2017 5:25 am

possible to send command directly to WebREPL

Post by skimj » 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()')`

Following the webrepl_cli.py code, I'm able to create a connection, login and get the remote WebREPL version. webrepl_cli.send_req() uses a coded 'op' of 1, 2, or 3 to simplify the communication. Possibly, I could add an option 4 and hard-code the desired command in the micropython device. (I can't find the receiving end of this WebREPL code)

Obviously, the WebREPL http client ternimal is capable of sending individual characters, same as if they were typed at a REPL prompt, which tells me there would at least be a way to send raw socket data to accomplish the same goal? I don't have a clue how to even approach this method.

Any other ideas?

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

Re: How to send commands directly to WebREPL? Raw-mode?

Post by aivarannamaa » Sun Jun 10, 2018 12:12 pm

I'm also interested in accessing the REPL programmatically over network, instead of serial.

I noticed that WebREPL doesn't respond to Ctrl+A (but does respond to Ctrl+B). Is it a browser issue? If I implement my own client for the WebREPL interface, can I put it into raw mode by sending a `\x01` ?

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

Re: possible to send command directly to WebREPL

Post by aivarannamaa » Sun Jun 10, 2018 3:26 pm

Looks like WebREPL can be used quite like serial REPL, including raw-mode, interrupts etc.

Here's my proof-of-concept client script (with https://github.com/websocket-client/websocket-client). Note that here you need to manually suffix each command in normal mode with \n and in raw mode with \x04:

Code: Select all

import websocket
import threading
from time import sleep

def on_message(ws, message):
    print(message, end="")

def on_close(ws):
    print("### closed ###")

websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://192.168.1.226:8266", on_message = on_message, on_close = on_close)
wst = threading.Thread(target=ws.run_forever)
wst.daemon = True
wst.start()

conn_timeout = 5
while not ws.sock.connected and conn_timeout:
    sleep(1)
    conn_timeout -= 1
    
while ws.sock.connected:
    inp = (input()
           .replace("\\n", "\r\n")      # end of command in normal mode
           .replace("\\x01", "\x01")    # switch to raw mode
           .replace("\\x02", "\x02")    # switch to normal mode
           .replace("\\x03", "\x03")    # interrupt
           .replace("\\x04", "\x04"))   # end of command in raw mode

    if inp == "exit":
        ws.close()
    else:
        ws.send(inp)

Post Reply