Skip to content

Commit d2e1994

Browse files
committed
docs(websocket): enhance README with TOC, quick start and add pytest dependencies and execution instructions
1 parent 6f6237a commit d2e1994

File tree

2 files changed

+168
-3
lines changed

2 files changed

+168
-3
lines changed

components/esp_websocket_client/examples/target/README.md

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
# Websocket Sample application
22

3-
This example will shows how to set up and communicate over a websocket.
3+
This example shows how to set up and communicate over a websocket.
4+
5+
## Table of Contents
6+
7+
- [Hardware Required](#hardware-required)
8+
- [Configure the project](#configure-the-project)
9+
- [Pre-configured SDK Configurations](#pre-configured-sdk-configurations)
10+
- [Server Certificate Verification](#server-certificate-verification)
11+
- [Generating Self-signed Certificates](#generating-a-self-signed-certificates-with-openssl)
12+
- [Build and Flash](#build-and-flash)
13+
- [Testing with pytest](#testing-with-pytest)
14+
- [Example Output](#example-output)
15+
- [Python Flask Echo Server](#python-flask-echo-server)
16+
17+
## Quick Start
18+
19+
1. **Install dependencies:**
20+
```bash
21+
pip install -r esp-protocols/ci/requirements.txt
22+
```
23+
24+
2. **Configure and build:**
25+
```bash
26+
idf.py menuconfig # Configure WiFi/Ethernet and WebSocket URI
27+
idf.py build
28+
```
29+
30+
3. **Flash and monitor:**
31+
```bash
32+
idf.py -p PORT flash monitor
33+
```
34+
35+
4. **Run tests:**
36+
```bash
37+
pytest .
38+
```
439

540
## How to Use Example
641

@@ -15,6 +50,27 @@ This example can be executed on any ESP32 board, the only required interface is
1550
* 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)
1651
* 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.
1752

53+
### Pre-configured SDK Configurations
54+
55+
This example includes several pre-configured `sdkconfig.ci.*` files for different testing scenarios:
56+
57+
* **sdkconfig.ci** - Default configuration with WebSocket over Ethernet (IP101 PHY, ESP32, IPv6) and hardcoded URI.
58+
* **sdkconfig.ci.plain_tcp** - WebSocket over plain TCP (no TLS, URI from stdin) using Ethernet (IP101 PHY, ESP32, IPv6).
59+
* **sdkconfig.ci.mutual_auth** - WebSocket with mutual TLS authentication (client/server certificate verification, skips CN check) and URI from stdin.
60+
* **sdkconfig.ci.dynamic_buffer** - WebSocket with dynamic buffer allocation, Ethernet (IP101 PHY, ESP32, IPv6), and hardcoded URI.
61+
62+
To use a specific configuration for building:
63+
```
64+
rm sdkconfig
65+
cp sdkconfig.ci.plain_tcp sdkconfig
66+
idf.py build
67+
```
68+
69+
Or specify it directly when building:
70+
```
71+
idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.ci.plain_tcp" build
72+
```
73+
1874
### Server Certificate Verification
1975

2076
* 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 +82,7 @@ This example can be executed on any ESP32 board, the only required interface is
2682
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.
2783
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
2884

29-
### Generating a self signed Certificates with OpenSSL
85+
### Generating a self signed Certificates with OpenSSL manually
3086
* 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:
3187

3288
```
@@ -63,6 +119,26 @@ Please see the openssl man pages (man openssl) for more details.
63119
It is **strongly recommended** to not reuse the example certificate in your application;
64120
it is included only for demonstration.
65121

122+
### Certificate Generation Options
123+
124+
#### Option 1: Manual OpenSSL Commands
125+
Follow the step-by-step process in the section above to understand certificate generation.
126+
127+
#### Option 2: Automated Script
128+
**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:
129+
130+
```bash
131+
chmod +x generate_certs.sh
132+
./generate_certs.sh
133+
```
134+
135+
This script automatically generates all required certificates in the correct directories and cleans up temporary files.
136+
137+
#### Option 3: Online Certificate Generators
138+
- **mkcert**: `install mkcert` then `mkcert -install` and `mkcert localhost`
139+
- **Let's Encrypt**: For production certificates (free, automated renewal)
140+
- **Online generators**: Search for "self-signed certificate generator" online
141+
66142
### Build and Flash
67143

68144
Build the project and flash it to the board, then run monitor tool to view serial output:
@@ -73,7 +149,36 @@ idf.py -p PORT flash monitor
73149

74150
(To exit the serial monitor, type ``Ctrl-]``.)
75151

76-
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
152+
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.
153+
154+
## Testing with pytest
155+
156+
### Install Dependencies
157+
158+
Before running the pytest tests, you need to install the required Python packages:
159+
160+
```
161+
pip install -r esp-protocols/ci/requirements.txt
162+
```
163+
164+
### Run pytest
165+
166+
After installing the dependencies, you can run the pytest tests:
167+
168+
Run all tests in current directory:
169+
```
170+
pytest .
171+
```
172+
173+
Run specific test file:
174+
```
175+
pytest pytest_websocket.py
176+
```
177+
178+
To specify the target device or serial port, use:
179+
```
180+
pytest --target esp32 --port /dev/ttyUSB0
181+
```
77182

78183
## Example Output
79184

@@ -135,3 +240,32 @@ if __name__ == '__main__':
135240
# gunicorn -b 0.0.0.0:5000 --workers 4 --threads 100 module:app
136241
app.run(host="0.0.0.0", debug=True)
137242
```
243+
244+
## Troubleshooting
245+
246+
### Common Issues
247+
248+
**Connection failed:**
249+
- Verify WiFi/Ethernet configuration in `idf.py menuconfig`
250+
- Check if the WebSocket server is running and accessible
251+
- Ensure the URI is correct (use `wss://` for TLS, `ws://` for plain TCP)
252+
253+
**TLS certificate errors:**
254+
- For self-signed certificates, ensure `CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK=y` is enabled
255+
- Verify certificate files are properly formatted and accessible
256+
257+
**Build errors:**
258+
- Clean build: `idf.py fullclean`
259+
- Check ESP-IDF version compatibility
260+
- Verify all dependencies are installed
261+
262+
**Test failures:**
263+
- Ensure the device is connected and accessible via the specified port
264+
- Check that the target device matches the configuration (`--target esp32`)
265+
- Verify pytest dependencies are installed correctly
266+
267+
### Getting Help
268+
269+
- Check the [ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/)
270+
- Review the [WebSocket client component documentation](../../README.md)
271+
- Report issues on the [ESP Protocols repository](https://github.com/espressif/esp-protocols)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
# Generate CA, Server, and Client certificates automatically
3+
4+
# Create directories if they don't exist
5+
mkdir -p main/certs/server
6+
7+
echo "Generating CA certificate..."
8+
openssl genrsa -out main/certs/ca_key.pem 2048
9+
openssl req -new -x509 -days 3650 -key main/certs/ca_key.pem -out main/certs/ca_cert.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=TestCA"
10+
11+
echo "Generating Server certificate..."
12+
openssl genrsa -out main/certs/server/server_key.pem 2048
13+
openssl req -new -key main/certs/server/server_key.pem -out server_csr.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
14+
openssl x509 -req -days 3650 -in server_csr.pem -CA main/certs/ca_cert.pem -CAkey main/certs/ca_key.pem -CAcreateserial -out main/certs/server/server_cert.pem
15+
16+
echo "Generating Client certificate..."
17+
openssl genrsa -out main/certs/client_key.pem 2048
18+
openssl req -new -key main/certs/client_key.pem -out client_csr.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=TestClient"
19+
openssl x509 -req -days 3650 -in client_csr.pem -CA main/certs/ca_cert.pem -CAkey main/certs/ca_key.pem -CAcreateserial -out main/certs/client_cert.pem
20+
21+
# Clean up CSR files
22+
rm server_csr.pem client_csr.pem
23+
24+
echo "Certificates generated successfully!"
25+
echo "Generated files:"
26+
echo " - main/certs/ca_cert.pem (CA certificate)"
27+
echo " - main/certs/ca_key.pem (CA private key)"
28+
echo " - main/certs/client_cert.pem (Client certificate)"
29+
echo " - main/certs/client_key.pem (Client private key)"
30+
echo " - main/certs/server/server_cert.pem (Server certificate)"
31+
echo " - main/certs/server/server_key.pem (Server private key)"

0 commit comments

Comments
 (0)