Page 1 of 3

ESP32 Websocket Server

Posted: Sat Jul 11, 2020 8:27 pm
by Baldhead
Hi,

I need some way of bidirectional comunication between a client and a server.

I read something about websockets and server-sent events.

I already have a javascript client running in a browser, ie: chrome, and in esp32 i have a http server running.

The client(chrome) connects with esp32 http server.
The esp32 http server send the file "upload_script.html" to the chrome.
The client send some commands to server.
I need that server send(update) some comands to client too, but i wouldn't like to use long polling or ask the server every 500 ms or less for example.

In the future i want to use https/wss and write an app to run in android and ios.

Any suggestion will be welcome.

Thank's for the help.

Re: ESP32 Websocket Server

Posted: Sun Jul 12, 2020 4:24 am
by stdenits
@ Baldhead
Baldhead wrote:
Sat Jul 11, 2020 8:27 pm
I need some way of bidirectional comunication between a client and a server.
Hello.

You already have - this is a connection through a web socket that will be open until it is closed by the user or something goes wrong, not as intended.

If you mean the exchange protocol, then you can take a look at this project. Pay attention to: ../vue-frontend/src/socket.js and ../vue-frontend/src/App.vue

It uses JS Socket + JSON with Vuetifi. You only need the first two.
JSON makes it easy to parse messages from both sides.

Re: ESP32 Websocket Server

Posted: Sun Jul 12, 2020 3:30 pm
by Baldhead
Hello,

But i need that the server send messages without client request.

Of course, at the beginning the client will request the first connection.

Thanks.

Re: ESP32 Websocket Server

Posted: Sun Jul 12, 2020 5:26 pm
by stdenits
Baldhead wrote:
Sun Jul 12, 2020 3:30 pm
But i need that the server send messages without client request.
I did not use it yet, but there is Async send func.

Perhaps someone more experienced will tell you about this.

Re: ESP32 Websocket Server

Posted: Mon Jul 13, 2020 1:17 pm
by Zeni241
For two way communication in IoT world, the preferred and most used protocol is MQTT (instead of http).

A few desirable features of MQTT are:

Small code footprint (to make it easy to implement in small devices)
Low power consumption
Low bandwidth consumption
Low latency
Use of a publish/subscribe (“pub/sub”) pattern
MQTT has the momentum of the big public clouds—Amazon Web Services, Microsoft Azure, and Google Cloud Platform—behind it.
Here are a few links to get you going.

http://www.steves-internet-guide.com/mq ... cs-course/
http://www.lucadentella.it/en/2016/10/2 ... roduzione/
https://www.hivemq.com/mqtt-essentials/
https://mosquitto.org/
https://www.digitalocean.com/community/ ... untu-18-04

Re: ESP32 Websocket Server

Posted: Wed Jul 15, 2020 9:30 pm
by Baldhead
Thank's stdenits and Zeni241,

I think that for now webSockets are OK and "httpd_ws_recv_frame", "httpd_ws_send_frame" and "httpd_ws_send_frame_async" maybe solve my problem.

I would like an example using these functions in "esp_http_server.h", maybe communicating with a javascript running in a browser.

Thank's.

Re: ESP32 Websocket Server

Posted: Fri Jul 17, 2020 12:06 am
by Baldhead
Hi,

I found an example and managed to make it work.

Just changed the python script by javascript on the client side.

I didn't find the documentation of function "httpd_ws_send_frame_async".

Re: ESP32 Websocket Server

Posted: Fri Jul 17, 2020 11:14 pm
by Baldhead
Hi,

How can i get "@param[in] fd Socket descriptor for sending data" to use in function
"esp_err_t httpd_ws_send_frame_async(httpd_handle_t hd, int fd, httpd_ws_frame_t *frame)" ????

I want to send data from http_server running in esp32 to the client asynchronously.

The example in
"https://github.com/espressif/esp-idf/bl ... o_server.c"
doesn't seem to be asynchronous communication to me since it depends on an "HTTP_GET" from the client.


Thank's.

Re: ESP32 Websocket Server

Posted: Mon Jul 20, 2020 8:38 pm
by Baldhead
Hi,

I am new with sockets programming.

How i have access to all "websockets" clients connected to the server ?

Is there a list of connected clients to the server that i can access ?
These list return "@param[in] fd Socket descriptor for sending data" ?

How to know which clients are still connected to the server ?
How to know those clients who disconnected from server without using "websocket close handshake" ?

I need to update all clients and the server with the same data message when any of these change your state.

Star topology with the server at the center of star.

Thank's.

Re: ESP32 Websocket Server

Posted: Tue Jul 21, 2020 4:36 am
by Baldhead
Hi,

How many clients(open sockets) the websocket server can have ?

Can i set the max open connections using this ?
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_open_sockets = 5;

I tested and the server accepted only 3 connections. The four connection i had to update the browser several times to connect and the five connection not open.
The server send the html page but dont open the four and five websocket connection.

Thank's.