Select one of the symbols to view example projects that use it.
 
Outline
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include "esp_netif.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include <esp_http_server.h>
TAG
async_resp_arg
ws_async_send(void *)
trigger_async_send(httpd_handle_t, httpd_req_t *)
echo_handler(httpd_req_t *)
ws
start_webserver()
stop_webserver(httpd_handle_t)
disconnect_handler(void *, esp_event_base_t, int32_t, void *)
connect_handler(void *, esp_event_base_t, int32_t, void *)
app_main()
Files
loading (2/4)...
SourceVuESP-IDF Framework and Examplesws_echo_server samplemain/ws_echo_server.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* WebSocket Echo Server Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *//* ... */ #include <esp_wifi.h> #include <esp_event.h> #include <esp_log.h> #include <esp_system.h> #include <nvs_flash.h> #include <sys/param.h> #include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" #include <esp_http_server.h>10 includes /* A simple example that demonstrates using websocket echo server *//* ... */ static const char *TAG = "ws_echo_server"; /* * Structure holding server handle * and internal socket fd in order * to use out of request send *//* ... */ struct async_resp_arg { httpd_handle_t hd; int fd; }{ ... }; /* * async send function, which we put into the httpd work queue *//* ... */ static void ws_async_send(void *arg) { static const char * data = "Async data"; struct async_resp_arg *resp_arg = arg; httpd_handle_t hd = resp_arg->hd; int fd = resp_arg->fd; httpd_ws_frame_t ws_pkt; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); ws_pkt.payload = (uint8_t*)data; ws_pkt.len = strlen(data); ws_pkt.type = HTTPD_WS_TYPE_TEXT; httpd_ws_send_frame_async(hd, fd, &ws_pkt); free(resp_arg); }{ ... } static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req) { struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); if (resp_arg == NULL) { return ESP_ERR_NO_MEM; }{...} resp_arg->hd = req->handle; resp_arg->fd = httpd_req_to_sockfd(req); esp_err_t ret = httpd_queue_work(handle, ws_async_send, resp_arg); if (ret != ESP_OK) { free(resp_arg); }{...} return ret; }{ ... } /* * This handler echos back the received ws data * and triggers an async send if certain message received *//* ... */ static esp_err_t echo_handler(httpd_req_t *req) { if (req->method == HTTP_GET) { ESP_LOGI(TAG, "Handshake done, the new connection was opened"); return ESP_OK; }{...} httpd_ws_frame_t ws_pkt; uint8_t *buf = NULL; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); ws_pkt.type = HTTPD_WS_TYPE_TEXT; /* Set max_len = 0 to get the frame len */ esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_recv_frame failed to get frame len with %d", ret); return ret; }{...} ESP_LOGI(TAG, "frame len is %d", ws_pkt.len); if (ws_pkt.len) { /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */ buf = calloc(1, ws_pkt.len + 1); if (buf == NULL) { ESP_LOGE(TAG, "Failed to calloc memory for buf"); return ESP_ERR_NO_MEM; }{...} ws_pkt.payload = buf; /* Set max_len = ws_pkt.len to get the frame payload */ ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret); free(buf); return ret; }{...} ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload); }{...} ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type); if (ws_pkt.type == HTTPD_WS_TYPE_TEXT && strcmp((char*)ws_pkt.payload,"Trigger async") == 0) { free(buf); return trigger_async_send(req->handle, req); }{...} ret = httpd_ws_send_frame(req, &ws_pkt); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_send_frame failed with %d", ret); }{...} free(buf); return ret; }{ ... } static const httpd_uri_t ws = { .uri = "/ws", .method = HTTP_GET, .handler = echo_handler, .user_ctx = NULL, .is_websocket = true }{...}; static httpd_handle_t start_webserver(void) { httpd_handle_t server = NULL; httpd_config_t config = HTTPD_DEFAULT_CONFIG(); // Start the httpd server ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); if (httpd_start(&server, &config) == ESP_OK) { // Registering the ws handler ESP_LOGI(TAG, "Registering URI handlers"); httpd_register_uri_handler(server, &ws); return server; }{...} ESP_LOGI(TAG, "Error starting server!"); return NULL; }{ ... } static esp_err_t stop_webserver(httpd_handle_t server) { // Stop the httpd server return httpd_stop(server); }{ ... } static void disconnect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { httpd_handle_t* server = (httpd_handle_t*) arg; if (*server) { ESP_LOGI(TAG, "Stopping webserver"); if (stop_webserver(*server) == ESP_OK) { *server = NULL; }{...} else { ESP_LOGE(TAG, "Failed to stop http server"); }{...} }{...} }{ ... } static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { httpd_handle_t* server = (httpd_handle_t*) arg; if (*server == NULL) { ESP_LOGI(TAG, "Starting webserver"); *server = start_webserver(); }{...} }{ ... } void app_main(void) { static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. * Read "Establishing Wi-Fi or Ethernet Connection" section in * examples/protocols/README.md for more information about this function. *//* ... */ ESP_ERROR_CHECK(example_connect()); /* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected, * and re-start it upon connection. *//* ... */ #ifdef CONFIG_EXAMPLE_CONNECT_WIFI ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));/* ... */ #endif // CONFIG_EXAMPLE_CONNECT_WIFI #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));/* ... */ #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET /* Start the server for the first time */ server = start_webserver(); }{ ... }
Details
Show:
from
Types: Columns: