ESP32 Websocket Server

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

ESP32 Websocket Server

Postby Baldhead » Sat Jul 11, 2020 8:27 pm

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.

stdenits
Posts: 23
Joined: Sun May 17, 2020 3:18 pm

Re: ESP32 Websocket Server

Postby stdenits » Sun Jul 12, 2020 4:24 am

@ 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.

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Sun Jul 12, 2020 3:30 pm

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.

stdenits
Posts: 23
Joined: Sun May 17, 2020 3:18 pm

Re: ESP32 Websocket Server

Postby stdenits » Sun Jul 12, 2020 5:26 pm

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.

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Re: ESP32 Websocket Server

Postby Zeni241 » Mon Jul 13, 2020 1:17 pm

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

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Wed Jul 15, 2020 9:30 pm

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.

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Fri Jul 17, 2020 12:06 am

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".

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Fri Jul 17, 2020 11:14 pm

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.

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Mon Jul 20, 2020 8:38 pm

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.

Baldhead
Posts: 468
Joined: Sun Mar 31, 2019 5:16 am

Re: ESP32 Websocket Server

Postby Baldhead » Tue Jul 21, 2020 4:36 am

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.

Who is online

Users browsing this forum: No registered users and 78 guests