Basic WebSocket example needed

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Beta_Ravener
Posts: 35
Joined: Tue Aug 09, 2016 6:56 pm

Basic WebSocket example needed

Post by Beta_Ravener » Sat May 27, 2017 7:23 pm

I have several projects for devices that I'd like to be able to be able to control through web browser, but I'd like them to be standalone - not a part of IoT network or anything. I'm using ESP8266, so I can just connect to the device directly as an AP. The device will then serve the web page to the client. I need to be able to send updates (measurements, state..) from device (server) to the client, which is hard to do with basic http requests.

Now, I've read that there is an implementation of WebSockets, used by WebREPL, that should be great for this type of problem. However, I couldn't find any information on how to actually use them from micropython, no example code, not even sure if the module can be just imported or I need to get the module into filesystem. I'd be very thankful for some basic example that would implement micropython server and web client (e.g. server sends increasing number spontaneously and client is just displaying current value and can reset it to 0 on server).

Beta_Ravener
Posts: 35
Joined: Tue Aug 09, 2016 6:56 pm

Re: Basic WebSocket example needed

Post by Beta_Ravener » Sun May 28, 2017 8:16 pm

So, in the meantime I came up with decent implementation that handles multiple connections in sane way. You can find the code here: https://github.com/BetaRavener/upy-websocket-server.

Like WebREPL, the server is started both on AP and STA and upon connection from browser it first serves webpage, which then connects to websocket. The number of concurrent connections can be limited - new connections get HTTP error 503.

There is no counting but the client sends "Hello" when websocket connection is established and server replies with appended " World", which makes client display "Hello World", which I hope is enough as a proof of concept. It's just a work in progress so any suggestions are welcome.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Basic WebSocket example needed

Post by rdagger » Sun Sep 03, 2017 11:27 pm

Would your upy-websocket-server work on an ESP32?

onextw
Posts: 1
Joined: Tue Oct 10, 2017 2:41 am

Re: Basic WebSocket example needed

Post by onextw » Tue Oct 10, 2017 3:24 am

@Beta_Ravener

Thanks Dude, super handy script, Still getting my head around the code (still newish to python and ws's) but performs as advertised. Using it to serve a HSL Color wheel and stream the RGB Values back to the ESP to set PWM Levels as they change on the client's browser. Works well for a while then jams up.

From what I can tell, theres no headers in the Client --> ESP websocket connection (i.e. when it sends "hello" its just sending 5 bytes of data), is that right?

Also is there any value in opening up port 80 for serving the page but opening/binding to another port for data comms?

Realise these are pretty low level questions but at this point im not even sure what i should be googling. :|

Thanks - onextw

Post Reply