Skip to content

Commit 5dba3bd

Browse files
committed
Added websocket
1 parent 55e18e5 commit 5dba3bd

File tree

10 files changed

+1995
-1
lines changed

10 files changed

+1995
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ I based on [this](https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6
5454
```
5555
git clone https://github.com/nopnop2002/esp-idf-mpu6050-dmp
5656
cd esp-idf-mpu6050-dmp
57-
git clone https://github.com/Molorius/esp32-websocket components/websocket
5857
cd IMU_Zero
5958
idf.py set-target {esp32/esp32s2/esp32s3/esp32c2/esp32c3}
6059
idf.py menuconfig
@@ -133,6 +132,7 @@ I used [this](https://canvas-gauges.com/) for gauge display.
133132
I used [this](https://threejs.org/) for 3D display.
134133
You can change it as you like.
135134

135+
![browser-roll-pitch](https://user-images.githubusercontent.com/6020549/226154931-a63b230a-e970-4d29-be67-945ca037f99b.JPG)
136136

137137

138138
# Using IMU Filter

components/websocket/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(COMPONENT_SRCS "websocket.c" "websocket_server.c")
2+
set(COMPONENT_ADD_INCLUDEDIRS "./include")
3+
set(COMPONENT_REQUIRES lwip mbedtls)
4+
register_component()

components/websocket/Kconfig

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
menu "WebSocket Server"
3+
4+
config WEBSOCKET_SERVER_MAX_CLIENTS
5+
int "Max clients"
6+
range 1 1000
7+
default 20
8+
help
9+
Maximum number of clients that the WebSocket
10+
server can handle at a time.
11+
12+
config WEBSOCKET_SERVER_QUEUE_SIZE
13+
int "Queue read size"
14+
range 1 100
15+
default 10
16+
help
17+
Size of the queue to deal with incoming
18+
WebSocket messages. The queue holds the
19+
connection, not the actual message.
20+
21+
config WEBSOCKET_SERVER_QUEUE_TIMEOUT
22+
int "Queue timeout"
23+
range 0 10000
24+
default 30
25+
help
26+
Timeout for adding new connections to the
27+
read queue.
28+
29+
config WEBSOCKET_SERVER_TASK_STACK_DEPTH
30+
int "Stack depth"
31+
range 3000 20000
32+
default 6000
33+
help
34+
Stack depth for the WebSocket server. The task
35+
handles reads.
36+
37+
config WEBSOCKET_SERVER_TASK_PRIORITY
38+
int "Priority"
39+
range 1 20
40+
default 5
41+
help
42+
Priority for the WebSocket server. The task
43+
handles reads.
44+
45+
config WEBSOCKET_SERVER_PINNED
46+
bool "Server pinned to core"
47+
default false
48+
help
49+
Pin the WebSocket server task to a specific core.
50+
The task handles reads.
51+
52+
config WEBSOCKET_SERVER_PINNED_CORE
53+
int "Pinned core"
54+
depends on WEBSOCKET_SERVER_PINNED
55+
range 0 1
56+
default 0
57+
help
58+
Core that the WebSocket server is pinned to.
59+
The task handles reads.
60+
61+
endmenu

components/websocket/LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

components/websocket/README.md

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
2+
By Blake Felt - [email protected]
3+
4+
ESP32 WebSocket
5+
==================
6+
7+
A component for WebSockets on ESP-IDF using lwip netconn.
8+
For an example, see https://github.com/Molorius/ESP32-Examples.
9+
10+
To add to a project, type:
11+
`git submodule add https://github.com/Molorius/esp32-websocket.git components/websocket`
12+
into the base directory of your project.
13+
14+
Some configuration options for the Server can be found in menuconfig in:
15+
Component config ---> WebSocket Server
16+
17+
This presently only has the WebSocket server code working, but client code will be added in the future (the groundwork is there).
18+
19+
The code only allows one WebSocket server at a time, but this merely handles all incoming reads. New connections are added externally, so this can be used to hold various WebSocket connections.
20+
21+
While this can theoretically handle very large messages, hardware constraints (RAM) limits the size of messages. I highly recommend not using more than 5000 bytes per message, but no constraint is in place for this.
22+
23+
Any suggestions or fixes are gladly appreciated.
24+
25+
Table of Contents
26+
=================
27+
* [Enumerations](#enumerations)
28+
* [WEBSOCKET_TYPE_t](#enum-websocket_type_t)
29+
* [Functions](#functions)
30+
* [ws_server_start](#int-ws_server_start)
31+
* [ws_server_stop](#int-ws_server_stop)
32+
* [ws_server_add_client](#int-ws_server_add_clientstruct-netconn-connchar-msguint16_t-lenchar-urlvoid-callback)
33+
* [ws_server_add_client_protocol](#int-ws_server_add_client_protocolstruct-netconn-connchar-msguint16_t-lenchar-urlchar-protocolvoid-callback)
34+
* [ws_server_len_url](#int-ws_server_len_urlchar-url)
35+
* [ws_server_len_all](#int-ws_server_len_all)
36+
* [ws_server_remove_client](#int-ws_server_remove_clientint-num)
37+
* [ws_server_remove_clients](#int-ws_server_remove_clientschar-url)
38+
* [ws_server_remove_all](#int-ws_server_remove_all)
39+
* [ws_server_send_text_client](#int-ws_server_send_text_clientint-numchar-msguint64_t-len)
40+
* [ws_server_send_text_clients](#int-ws_server_send_text_clientschar-urlchar-msguint64_t-len)
41+
* [ws_server_send_text_all](#int-ws_server_send_text_allchar-msguint64_t-len)
42+
* [ws_server_send_text_client_from_callback](#int-ws_server_send_text_client_from_callbackint-numchar-msguint64_t-len)
43+
* [ws_server_send_text_clients_from_callback](#int-ws_server_send_text_clients_from_callbackchar-urlchar-msguint64_t-len)
44+
* [ws_server_send_text_all_from_callback](#int-ws_server_send_text_all_from_callbackchar-msguint64_t-len)
45+
* [ws_server_send_bin_client_from_callback](#int-ws_server_send_bin_client_from_callbackint-numchar-msguint64_t-len)
46+
* [ws_server_send_bin_clients_from_callback](#int-ws_server_send_bin_clients_from_callbackchar-urlchar-msguint64_t-len)
47+
* [ws_server_send_bin_all_from_callback](#int-ws_server_send_bin_all_from_callbackchar-msguint64_t-len)
48+
49+
Enumerations
50+
============
51+
52+
enum WEBSOCKET_TYPE_t
53+
---------------------
54+
55+
The different types of WebSocket events.
56+
57+
*Values*
58+
* `WEBSOCKET_CONNECT`: A new client has successfully connected.
59+
* `WEBSOCKET_DISCONNECT_EXTERNAL`: The other side sent a disconnect message.
60+
* `WEBSOCKET_DISCONNECT_INTERNAL`: The esp32 server sent a disconnect message.
61+
* `WEBSOCKET_DISCONNECT_ERROR`: Disconnect due to a connection error.
62+
* `WEBSOCKET_TEXT`: Incoming text.
63+
* `WEBSOCKET_BIN`: Incoming binary.
64+
* `WEBSOCKET_PING`: The other side sent a ping message.
65+
* `WEBSOCKET_PONG`: The other side successfully replied to our ping.
66+
67+
Functions
68+
=========
69+
70+
int ws_server_start()
71+
---------------------
72+
73+
Starts the WebSocket Server. Use this function before attempting any
74+
sort of transmission or adding a client.
75+
76+
*Returns*
77+
* 1: successful start
78+
* 0: server already running
79+
80+
int ws_server_stop()
81+
--------------------
82+
83+
Stops the WebSocket Server. New clients can still be added and
84+
messages can be sent, but new messages will not be received.
85+
86+
*Returns*
87+
* 1: successful stop
88+
* 0: server was not running before
89+
90+
int ws_server_add_client(struct netconn* conn,char* msg,uint16_t len,char* url,void *callback)
91+
----------------------------------------------------------------------------------------------
92+
93+
Adds a client to the WebSocket Server handler and performs the necessary handshake.
94+
95+
*Parameters*
96+
* `conn`: the lwip netconn connection.
97+
* `msg`: the entire incoming request message to join the server. Necessary for the handshake.
98+
* `len`: the length of `msg`.
99+
* `url`: the NULL-terminated url. Used to keep track of clients, not required.
100+
* `callback`: the callback that is used to run WebSocket events. This must be with parameters(uint8_t num,WEBSOCKET_TYPE_t type,char* msg,uint64_t len) where "num" is the client number, "type" is the event type, "msg" is the incoming message, and "len" is the message length. The callback itself is optional.
101+
102+
*Returns*
103+
* -2: not enough information in `msg` to perform handshake.
104+
* -1: server full, or connection issue.
105+
* 0 or greater: connection number
106+
107+
int ws_server_add_client_protocol(struct netconn* conn,char* msg,uint16_t len,char* url,char* protocol,void *callback)
108+
----------------------------------------------------------------------------------------------------------------------
109+
110+
Adds a client to the WebSocket Server handler and performs the necessary handshake. Will also send
111+
the specified protocol.
112+
113+
*Parameters*
114+
* `conn`: the lwip netconn connection.
115+
* `msg`: the entire incoming request message to join the server. Necessary for the handshake.
116+
* `len`: the length of `msg`.
117+
* `url`: the NULL-terminated url. Used to keep track of clients, not required.
118+
* `protocol`: the NULL-terminated protocol. This will be sent to the client in the header.
119+
* `callback`: the callback that is used to run WebSocket events. This must be with parameters(uint8_t num,WEBSOCKET_TYPE_t type,char* msg,uint64_t len) where "num" is the client number, "type" is the event type, "msg" is the incoming message, and "len" is the message length. The callback itself is optional.
120+
121+
*Returns*
122+
* -2: not enough information in `msg` to perform handshake.
123+
* -1: server full, or connection issue.
124+
* 0 or greater: connection number
125+
126+
int ws_server_len_url(char* url)
127+
--------------------------------
128+
129+
Returns the number of clients connected to the specified URL.
130+
131+
*Parameters*
132+
* `url`: the NULL-terminated string of the desired URL.
133+
134+
*Returns*
135+
* The number of clients connected to the specified URL.
136+
137+
int ws_server_len_all()
138+
-----------------------
139+
140+
*Returns*
141+
* The number of connected clients.
142+
143+
int ws_server_remove_client(int num)
144+
------------------------------------
145+
146+
Removes the desired client.
147+
148+
*Parameters*
149+
* `num`: the client number
150+
151+
*Returns*
152+
* 0: not a valid client number
153+
* 1: client disconnected
154+
155+
int ws_server_remove_clients(char* url)
156+
---------------------------------------
157+
158+
Removes all clients connect to the desired URL.
159+
160+
*Parameters*
161+
* `url`: the NULL-terminated URL.
162+
163+
*Returns*
164+
* The number of clients that were disconnected.
165+
166+
int ws_server_remove_all()
167+
--------------------------
168+
169+
Removes all clients from server.
170+
171+
*Returns*
172+
* The number of clients that were disconnected.
173+
174+
int ws_server_send_text_client(int num,char* msg,uint64_t len)
175+
--------------------------------------------------------------
176+
177+
Sends the desired message to the client.
178+
179+
*Parameters*
180+
* `num`: the client's number.
181+
* `msg`: the desired message.
182+
* `len`: the length of the message.
183+
184+
*Returns*
185+
* 0: message not sent properly
186+
* 1: message sent
187+
188+
int ws_server_send_text_clients(char* url,char* msg,uint64_t len)
189+
-----------------------------------------------------------------
190+
191+
Sends the message to clients connected to the desired URL.
192+
193+
*Parameters*
194+
* `url`: the NULL-terminated URL.
195+
* `msg`: the desired message.
196+
* `len`: the length of the message.
197+
198+
*Returns*
199+
* The number of clients that the message was sent to.
200+
201+
int ws_server_send_text_all(char* msg,uint64_t len)
202+
---------------------------------------------------
203+
204+
Sends the message to all connected clients.
205+
206+
*Parameters*
207+
* `msg`: the desired message
208+
* `len`: the length of the message
209+
210+
*Returns*
211+
* The number of clients that the message was sent to.
212+
213+
int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len)
214+
----------------------------------------------------------------------------
215+
216+
Sends the desired message to the client. Only use this inside the callback function.
217+
218+
*Parameters*
219+
* `num`: the client's number.
220+
* `msg`: the desired message.
221+
* `len`: the length of the message.
222+
223+
*Returns*
224+
* 0: message not sent properly
225+
* 1: message sent
226+
227+
int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len)
228+
-------------------------------------------------------------------------------
229+
230+
Sends the message to clients connected to the desired URL. Only use this inside the callback function.
231+
232+
*Parameters*
233+
* `url`: the NULL-terminated URL.
234+
* `msg`: the desired message.
235+
* `len`: the length of the message.
236+
237+
*Returns*
238+
* The number of clients that the message was sent to.
239+
240+
int ws_server_send_text_all_from_callback(char* msg,uint64_t len)
241+
-----------------------------------------------------------------
242+
243+
Sends the message to all connected clients. Only use this inside the callback function.
244+
245+
*Parameters*
246+
* `msg`: the desired message
247+
* `len`: the length of the message
248+
249+
*Returns*
250+
* The number of clients that the message was sent to.
251+
252+
int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len)
253+
----------------------------------------------------------------------------
254+
255+
Sends the desired binary message to the client. Only use this inside the callback function.
256+
257+
*Parameters*
258+
* `num`: the client's number.
259+
* `msg`: the desired message.
260+
* `len`: the length of the message.
261+
262+
*Returns*
263+
* 0: message not sent properly
264+
* 1: message sent
265+
266+
int ws_server_send_bin_clients_from_callback(char* url,char* msg,uint64_t len)
267+
-------------------------------------------------------------------------------
268+
269+
Sends the binary message to clients connected to the desired URL. Only use this inside the callback function.
270+
271+
*Parameters*
272+
* `url`: the NULL-terminated URL.
273+
* `msg`: the desired message.
274+
* `len`: the length of the message.
275+
276+
*Returns*
277+
* The number of clients that the message was sent to.
278+
279+
int ws_server_send_bin_all_from_callback(char* msg,uint64_t len)
280+
-----------------------------------------------------------------
281+
282+
Sends a binary message to all connected clients. Only use this inside the callback function.
283+
284+
*Parameters*
285+
* `msg`: the desired message
286+
* `len`: the length of the message
287+
288+
*Returns*
289+
* The number of clients that the message was sent to.

components/websocket/component.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# websocket component makefile
2+
# all files are in default positions

0 commit comments

Comments
 (0)