You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
idf.py menuconfig # Configure WiFi/Ethernet and WebSocket URI
28
+
idf.py build
29
+
```
30
+
31
+
3.**Flash and monitor:**
32
+
```bash
33
+
idf.py -p PORT flash monitor
34
+
```
35
+
36
+
4.**Run tests:**
37
+
```bash
38
+
pytest .
39
+
```
4
40
5
41
## How to Use Example
6
42
@@ -15,6 +51,20 @@ This example can be executed on any ESP32 board, the only required interface is
15
51
* Configure the websocket endpoint URI under "Example Configuration", if "WEBSOCKET_URI_FROM_STDIN" is selected then the example application will connect to the URI it reads from stdin (used for testing)
16
52
* To test a WebSocket client example over TLS, please enable one of the following configurations: `CONFIG_WS_OVER_TLS_MUTUAL_AUTH` or `CONFIG_WS_OVER_TLS_SERVER_AUTH`. See the sections below for more details.
17
53
54
+
### Pre-configured SDK Configurations
55
+
56
+
This example includes several pre-configured `sdkconfig.ci.*` files for different testing scenarios:
57
+
58
+
***sdkconfig.ci** - Default configuration with WebSocket over Ethernet (IP101 PHY, ESP32, IPv6) and hardcoded URI.
59
+
***sdkconfig.ci.plain_tcp** - WebSocket over plain TCP (no TLS, URI from stdin) using Ethernet (IP101 PHY, ESP32, IPv6).
60
+
***sdkconfig.ci.mutual_auth** - WebSocket with mutual TLS authentication (client/server certificate verification, skips CN check) and URI from stdin.
61
+
***sdkconfig.ci.dynamic_buffer** - WebSocket with dynamic buffer allocation, Ethernet (IP101 PHY, ESP32, IPv6), and hardcoded URI.
* Mutual Authentication: When `CONFIG_WS_OVER_TLS_MUTUAL_AUTH=y` is enabled, it's essential to provide valid certificates for both the server and client.
@@ -26,7 +76,7 @@ This example can be executed on any ESP32 board, the only required interface is
26
76
Please note: This example represents an extremely simplified approach to generating self-signed certificates/keys with a single common CA, devoid of CN checks, lacking password protection, and featuring hardcoded key sizes and types. It is intended solely for testing purposes.
27
77
In the outlined steps, we are omitting the configuration of the CN (Common Name) field due to the context of a testing environment. However, it's important to recognize that the CN field is a critical element of SSL/TLS certificates, significantly influencing the security and efficacy of HTTPS communications. This field facilitates the verification of a website's identity, enhancing trust and security in web interactions. In practical deployments beyond testing scenarios, ensuring the CN field is accurately set is paramount for maintaining the integrity and reliability of secure communications
28
78
29
-
### Generating a self signed Certificates with OpenSSL
79
+
### Generating a self signed Certificates with OpenSSL manually
30
80
* The example below outlines the process for creating new certificates for both the server and client using OpenSSL, a widely-used command line tool for implementing TLS protocol:
31
81
32
82
```
@@ -63,6 +113,46 @@ Please see the openssl man pages (man openssl) for more details.
63
113
It is **strongly recommended** to not reuse the example certificate in your application;
64
114
it is included only for demonstration.
65
115
116
+
### Certificate Generation Options
117
+
118
+
#### Option 1: Manual OpenSSL Commands
119
+
Follow the step-by-step process in the section above to understand certificate generation.
120
+
121
+
#### Option 2: Automated Script
122
+
**Note:** Test certificates are already available in the example. If you want to regenerate them or create new ones, use the provided `generate_certs.sh` script:
123
+
124
+
```bash
125
+
# Auto-detect local IP address (recommended for network testing)
126
+
./generate_certs.sh
127
+
128
+
# Specify custom hostname or IP address
129
+
./generate_certs.sh 192.168.1.100
130
+
131
+
# Use localhost (for local-only testing)
132
+
./generate_certs.sh localhost
133
+
```
134
+
135
+
This script automatically generates all required certificates in the correct directories and cleans up temporary files.
136
+
137
+
**Important:** The server certificate's Common Name (CN) must match the hostname or IP address that ESP32 clients use to connect. If not specified, the script attempts to auto-detect your local IP address. Certificate verification will fail if there's a mismatch between the CN and the actual connection address.
138
+
139
+
**CN Mismatch Handling:**
140
+
If you encounter certificate verification failures due to CN mismatch, you have two options:
141
+
142
+
1.**Recommended (Secure):** Regenerate certificates with the correct CN:
143
+
```bash
144
+
./generate_certs.sh <actual_hostname_or_ip>
145
+
```
146
+
147
+
2.**Testing Only (Less Secure):** Skip CN verification by enabling `CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK=y` in `idf.py menuconfig`.
148
+
149
+
⚠️ **WARNING:** This option disables an important security check and should **NEVER** be used in production environments. It makes your application vulnerable to man-in-the-middle attacks.
150
+
151
+
#### Option 3: Online Certificate Generators
152
+
-**mkcert**: `install mkcert` then `mkcert -install` and `mkcert localhost`
153
+
-**Let's Encrypt**: For production certificates (free, automated renewal)
154
+
-**Online generators**: Search for "self-signed certificate generator" online
155
+
66
156
### Build and Flash
67
157
68
158
Build the project and flash it to the board, then run monitor tool to view serial output:
@@ -73,7 +163,36 @@ idf.py -p PORT flash monitor
73
163
74
164
(To exit the serial monitor, type ``Ctrl-]``.)
75
165
76
-
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
166
+
See the [ESP-IDF Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
167
+
168
+
## Testing with pytest
169
+
170
+
### Install Dependencies
171
+
172
+
Before running the pytest tests, you need to install the required Python packages:
173
+
174
+
```
175
+
pip install -r esp-protocols/ci/requirements.txt
176
+
```
177
+
178
+
### Run pytest
179
+
180
+
After installing the dependencies, you can run the pytest tests:
181
+
182
+
Run all tests in current directory:
183
+
```
184
+
pytest .
185
+
```
186
+
187
+
Run specific test file:
188
+
```
189
+
pytest pytest_websocket.py
190
+
```
191
+
192
+
To specify the target device or serial port, use:
193
+
```
194
+
pytest --target esp32 --port /dev/ttyUSB0
195
+
```
77
196
78
197
## Example Output
79
198
@@ -105,9 +224,101 @@ W (9162) WEBSOCKET: Received=hello 0003
105
224
```
106
225
107
226
108
-
## Python Flask echo server
227
+
## WebSocket Test Server
228
+
229
+
### Standalone Test Server
230
+
231
+
This example includes a standalone WebSocket test server (`websocket_server.py`) that can be used for testing your ESP32 WebSocket client:
232
+
233
+
#### Quick Start
234
+
235
+
**Plain WebSocket (No TLS):**
236
+
```bash
237
+
# Plain WebSocket server (no encryption)
238
+
python websocket_server.py
239
+
240
+
# Custom port
241
+
python websocket_server.py --port 9000
242
+
```
243
+
244
+
**Server-Only Authentication:**
245
+
```bash
246
+
# TLS WebSocket server (ESP32 verifies server)
247
+
python websocket_server.py --tls
248
+
249
+
# Custom port with TLS
250
+
python websocket_server.py --port 9000 --tls
251
+
```
252
+
253
+
**Mutual Authentication:**
254
+
```bash
255
+
# TLS with client certificate verification (both verify each other)
**Plain WebSocket (No TLS or Client Verification):**
291
+
```bash
292
+
# Basic server (port 8080)
293
+
python websocket_server.py
294
+
295
+
# Custom port
296
+
python websocket_server.py --port 9000
297
+
```
109
298
110
-
By default, the `wss://echo.websocket.org` endpoint is used. You can setup a Python websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
299
+
**Server-Only Authentication (ESP32 verifies server, server doesn't verify ESP32):**
300
+
```bash
301
+
# TLS server without client verification
302
+
python websocket_server.py --tls
303
+
304
+
# Custom port with TLS
305
+
python websocket_server.py --port 9000 --tls
306
+
```
307
+
308
+
**Mutual Authentication (Both ESP32 and server verify each other's certificates):**
The server will display the connection URL (e.g., `wss://192.168.1.100:8080`) that you can use in your ESP32 configuration.
318
+
319
+
### Alternative: Python Flask Echo Server
320
+
321
+
By default, the `wss://echo.websocket.org` endpoint is used. You can also setup a Python Flask websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
- Verify WiFi/Ethernet configuration in `idf.py menuconfig`
356
+
- Check if the WebSocket server is running and accessible
357
+
- Ensure the URI is correct (use `wss://` for TLS, `ws://` for plain TCP)
358
+
359
+
**TLS certificate errors:**
360
+
-**Certificate verification failed:** The most common cause is CN mismatch. Ensure the server certificate's Common Name matches the hostname/IP you're connecting to:
361
+
- Check your connection URI (e.g., if connecting to `wss://192.168.1.100:8080`, the certificate CN must be `192.168.1.100`)
362
+
- Regenerate certificates with the correct CN: `./generate_certs.sh <your_hostname_or_ip>`
363
+
- For testing only, you can bypass CN check with `CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK=y` (NOT recommended for production)
364
+
- Verify certificate files are properly formatted and accessible
365
+
- Ensure the CA certificate used to sign the server certificate is loaded on the ESP32
366
+
367
+
**Build errors:**
368
+
- Clean build: `idf.py fullclean`
369
+
- Check ESP-IDF version compatibility
370
+
- Verify all dependencies are installed
371
+
372
+
**Test failures:**
373
+
- Ensure the device is connected and accessible via the specified port
374
+
- Check that the target device matches the configuration (`--target esp32`)
375
+
- Verify pytest dependencies are installed correctly
376
+
377
+
### Getting Help
378
+
379
+
- Check the [ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/)
380
+
- Review the [WebSocket client component documentation](../../README.md)
381
+
- Report issues on the [ESP Protocols repository](https://github.com/espressif/esp-protocols)
0 commit comments