Minimal code REPL and WebREPL examples

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Minimal code REPL and WebREPL examples

Post by ihornehrutsa » Mon Nov 15, 2021 9:37 pm

Connect to Micropython device via serial port (Windows PC Python version):

Code: Select all

# serial_REPL_test.py

port = "COM7"

CRLF = b"\r\n"
command = CRLF + b"1+1" + CRLF + b"2+2" + CRLF + b"3+3" + CRLF

from time import sleep
import serial

s = serial.Serial(port, 115200, timeout=1)
s.write(command)

sleep(1)

while s.in_waiting:
    result = s.readline().decode('utf-8')
    print(result, end='')

s.close()
Responce is:

Code: Select all

>>> 1+1
2
>>> 2+2
4
>>> 3+3
6
>>>
 

ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Re: Minimal code REPL and WebREPL examples

Post by ihornehrutsa » Mon Nov 15, 2021 10:04 pm

Micropython device is connected to a local Wi-Fi network. IP address: 192.168.0.200. WebREPL is started.
Windows PC IP address: 192.168.0.101
Connect to Micropython device WebREPL via WebSocket (Windows PC Python version):

Code: Select all

# websocket_REPL_test.py
'''
pip install websocket-client
if ImportError: cannot import name 'create_connection' from 'websocket'
'''

CRLF = b"\r\n"  # b"\x0D\x0A"
command = CRLF + b"1+1" + CRLF + b"2+2" + CRLF + b"3+3" + CRLF

IP = '192.168.0.200'
PASS = b'12345678' + CRLF

from time import sleep
from websocket import create_connection

try:
    ws = create_connection("ws://" + IP + ":8266")
    ws.settimeout(1)
    ws.send(PASS)
    ws.send(command)
    while True:
        result =  ws.recv()
        print(result, end='')
except Exception as e:
    print()
    print(e)
    ws.close()
    print("### closed ###")
Responce is:

Code: Select all

Password: 
WebREPL connected
>>> 
>>> 1+1
2
>>> 2+2
4
>>> 3+3
6
>>> 
timed out
### closed ###
Micropython device REPL output is:

Code: Select all

WebREPL connection from: ('192.168.0.101', 58912)

>>> 1+1
2
>>> 2+2
4
>>> 3+3
6
>>> dupterm: EOF received, deactivating

ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Re: Minimal code REPL and WebREPL examples

Post by ihornehrutsa » Wed Nov 17, 2021 7:47 am

More comprehensive WebSocket based code from Aivar Annamaa at viewtopic.php?f=2&t=3124&p=28051&hilit= ... ose#p28051

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