Page 1 of 1
Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Mon Oct 15, 2018 9:49 pm
by HermannSW
I
this posting I did show how based on danni's
Micropython websockets (esp8266 implementation) one ESP-01s module remotely controled a second ESP-01s module via WebREPL.
Today I wanted to minimize the sample and go against "ws://echo.websocket.org/" instead. After fixing two minor issues wrt missing port in URL and being too strict on websocket upgrade response, new minimal sample client echo_websocket_org.py just works:
https://github.com/Hermann-SW/uwebsocke ... bsocketorg
Code: Select all
$ webrepl_client.py 192.168.3.1
Password:
WebREPL connected
>>>
>>>
MicroPython v1.9.4-272-g46091b8a on 2018-07-18; ESP module with ESP8266
Type "help()" for more information.
>>> import echo_websocket_org
fo0bAr
>>> exit
### closed ###
$
$ cat echo_websocket_org.py
import uwebsockets.client
websocket = uwebsockets.client.connect("ws://echo.websocket.org/")
websocket.send("fo0bAr\r\n")
resp = websocket.recv()
print(resp)
$
Re: Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Tue Oct 16, 2018 5:09 pm
by pfalcon
Cool, but MicroPython has builtin module "websocket" for websocket support. That's how WebREPL is implemented.
Re: Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Tue Oct 16, 2018 7:26 pm
by HermannSW
pfalcon wrote: ↑Tue Oct 16, 2018 5:09 pm
Cool, but MicroPython has builtin module "websocket" for websocket support. That's how WebREPL is implemented.
Thanks, that is new to me.
Can I use it to implement a websocket client like I did wih uwebsockets module?
Where can I find documentation on websocket module?
It does not appear in 1.9.4 list of modules:
http://docs.micropython.org/en/latest/py-modindex.html
It is there, but help() does not reveal much:
Code: Select all
>>> websocket
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'websocket' is not defined
>>> import websocket
>>> websocket
<module 'websocket'>
>>> help(websocket)
object <module 'websocket'> is of type module
__name__ -- websocket
websocket -- <class 'websocket'>
>>>
Re: Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Wed Oct 17, 2018 12:07 pm
by pfalcon
Can I use it to implement a websocket client like I did wih uwebsockets module?
Well, as was mentioned, WebREPL uses it. Both server side as runs on ESP8266, and client side (webrepl_cli.py), though in the latter, it was disabled and switched to an adhoc Python implementation to get the same script to run both on MicroPython and CPython.
The caveat is that this module is considered "experimental" (i.e. API may change) and that's why it's not documented.
Re: Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Wed Nov 14, 2018 8:06 pm
by HermannSW
I looked into webrepl_cli.py, and that looks much more difficult for connecting a module's WebREPL than using Danni's uwebsockets lib with examples. While working on
mp.py yesterday, a step inbetween was a nice demo of uwebsocket module. Running in unix port MicroPython print() can be used (in unix_demo.py). This is the output it generates:
Code: Select all
$ ./micropython unix_demo.py
WebREPL connected
>>> import machine
>>> led = machine.Pin(1, machine.Pin.OUT)
>>> led.value(0)
>>> led.value(1)
>>> led.value(0)
>>> led.value(1)
>>> led.value(0)
>>> ^CTraceback (most recent call last):
File "unix_demo.py", line 28, in <module>
File "unix_demo.py", line 14, in do
File "unix_demo.py", line 9, in wait_for_prompt
File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 172, in recv
File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 168, in recv
File "/home/stammw/micropython/ports/unix/uwebsockets/protocol.py", line 80, in read_frame
KeyboardInterrupt:
$
And this is unix_demo.py showing how easy it is to execute commands remotely via WebREPL. Only the while loop and delays run in unix port micropython, the other commands are executed on ESP-01s via WebREPL and do blink:
Code: Select all
import uwebsockets.client
import utime
websocket = uwebsockets.client.connect('ws://192.168.4.1:8266/')
def wait_for_prompt():
resp = ""
while not(">>> " in resp):
resp = websocket.recv()
print(resp,end='')
def do(cmd):
websocket.send(cmd+"\r\n")
wait_for_prompt()
resp = websocket.recv()
assert resp == "Password: ", resp
do("abcd")
do("import machine")
do("led = machine.Pin(1, machine.Pin.OUT)")
while True:
do("led.value(0)")
utime.sleep(0.5)
do("led.value(1)")
utime.sleep(1.5)
Re: Minimal MicroPython uwebsockets.client that works on esp8266
Posted: Fri Nov 16, 2018 7:56 am
by HermannSW
This little utility might be useful for others as well, remote MicroPython module uptime:
Code: Select all
$ uptime.py
'up 30.64 hours'
$
$ cat ~/bin/uptime.py
#!/bin/bash
cd ~/micropython/ports/unix
./micropython mp.py 192.168.4.1 '"up %.2f hours"%(utime.ticks_ms()/(1000*3600))'
$
ESP-01s module is connected to cell phone mains adapter via USB adapter only, accessed via Wifi:
