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