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).
Basic WebSocket example needed
-
- Posts: 35
- Joined: Tue Aug 09, 2016 6:56 pm
Re: Basic WebSocket example needed
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.
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.
Re: Basic WebSocket example needed
Would your upy-websocket-server work on an ESP32?
Re: Basic WebSocket example needed
@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
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