Minimal MicroPython uwebsockets.client that works on esp8266

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Minimal MicroPython uwebsockets.client that works on esp8266

Post by HermannSW » Mon Oct 15, 2018 9:49 pm

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)
$ 
Last edited by HermannSW on Wed Nov 14, 2018 8:24 pm, edited 1 time in total.
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Minimal MicroPython uwebsockets.client that works on esp8266

Post by pfalcon » Tue Oct 16, 2018 5:09 pm

Cool, but MicroPython has builtin module "websocket" for websocket support. That's how WebREPL is implemented.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Minimal MicroPython uwebsockets.client that works on esp8266

Post by HermannSW » Tue Oct 16, 2018 7:26 pm

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'>
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Minimal MicroPython uwebsockets.client that works on esp8266

Post by pfalcon » Wed Oct 17, 2018 12:07 pm

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.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Minimal MicroPython uwebsockets.client that works on esp8266

Post by HermannSW » Wed Nov 14, 2018 8:06 pm

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)
Last edited by HermannSW on Wed Nov 14, 2018 8:17 pm, edited 2 times in total.
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Minimal MicroPython uwebsockets.client that works on esp8266

Post by HermannSW » Fri Nov 16, 2018 7:56 am

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:
Image
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

Post Reply