| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-04-12 13:14:22 +04:00
										 |  |  |  * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-06-26 13:28:23 +04:00
										 |  |  | #include <esp_log.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | #include "nvs_flash.h"
 | 
					
						
							|  |  |  | #include "protocol_examples_common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "esp_websocket_client.h"
 | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  | #include "esp_system.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | #include "esp_event.h"
 | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  | #include "esp_netif.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-15 15:41:46 +01:00
										 |  |  | static const char *TAG = "websocket"; | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 11:01:08 +02:00
										 |  |  | static void log_error_if_nonzero(const char *message, int error_code) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (error_code != 0) { | 
					
						
							|  |  |  |         ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data; | 
					
						
							|  |  |  |     switch (event_id) { | 
					
						
							| 
									
										
										
										
											2024-08-16 14:31:41 -05:00
										 |  |  |     case WEBSOCKET_EVENT_BEGIN: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_BEGIN"); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  |     case WEBSOCKET_EVENT_CONNECTED: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED"); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case WEBSOCKET_EVENT_DISCONNECTED: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED"); | 
					
						
							| 
									
										
										
										
											2022-10-04 11:01:08 +02:00
										 |  |  |         log_error_if_nonzero("HTTP status code",  data->error_handle.esp_ws_handshake_status_code); | 
					
						
							|  |  |  |         if (data->error_handle.error_type == WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT) { | 
					
						
							|  |  |  |             log_error_if_nonzero("reported from esp-tls", data->error_handle.esp_tls_last_esp_err); | 
					
						
							|  |  |  |             log_error_if_nonzero("reported from tls stack", data->error_handle.esp_tls_stack_err); | 
					
						
							|  |  |  |             log_error_if_nonzero("captured as transport's socket errno",  data->error_handle.esp_transport_sock_errno); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case WEBSOCKET_EVENT_DATA: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_DATA"); | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "Received opcode=%d", data->op_code); | 
					
						
							| 
									
										
										
										
											2020-07-17 17:59:05 +02:00
										 |  |  |         if (data->op_code == 0x08 && data->data_len == 2) { | 
					
						
							|  |  |  |             ESP_LOGW(TAG, "Received closed message with code=%d", 256 * data->data_ptr[0] + data->data_ptr[1]); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ESP_LOGW(TAG, "Received=%.*s", data->data_len, (char *)data->data_ptr); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 16:05:24 +04:00
										 |  |  |         // If received data contains json structure it succeed to parse
 | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  |         ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case WEBSOCKET_EVENT_ERROR: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR"); | 
					
						
							| 
									
										
										
										
											2022-10-04 11:01:08 +02:00
										 |  |  |         log_error_if_nonzero("HTTP status code",  data->error_handle.esp_ws_handshake_status_code); | 
					
						
							|  |  |  |         if (data->error_handle.error_type == WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT) { | 
					
						
							|  |  |  |             log_error_if_nonzero("reported from esp-tls", data->error_handle.esp_tls_last_esp_err); | 
					
						
							|  |  |  |             log_error_if_nonzero("reported from tls stack", data->error_handle.esp_tls_stack_err); | 
					
						
							|  |  |  |             log_error_if_nonzero("captured as transport's socket errno",  data->error_handle.esp_transport_sock_errno); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2024-08-16 14:31:41 -05:00
										 |  |  |     case WEBSOCKET_EVENT_FINISH: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "WEBSOCKET_EVENT_FINISH"); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | static void websocket_app_start(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-15 17:27:47 +08:00
										 |  |  |     esp_websocket_client_config_t websocket_cfg = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     websocket_cfg.uri = CONFIG_WEBSOCKET_URI; | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 17:27:47 +08:00
										 |  |  |     ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg); | 
					
						
							| 
									
										
										
										
											2023-09-08 13:02:28 +04:00
										 |  |  |     // This call demonstrates adding another header; it's called to increase code coverage
 | 
					
						
							|  |  |  |     esp_websocket_client_append_header(client, "HeaderNewKey", "value"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esp_websocket_client_start(client); | 
					
						
							|  |  |  |     char data[32]; | 
					
						
							|  |  |  |     int i = 0; | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  |     while (i < 1) { | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |         if (esp_websocket_client_is_connected(client)) { | 
					
						
							|  |  |  |             int len = sprintf(data, "hello %04d", i++); | 
					
						
							|  |  |  |             ESP_LOGI(TAG, "Sending %s", data); | 
					
						
							| 
									
										
										
										
											2020-03-05 16:26:26 +01:00
										 |  |  |             esp_websocket_client_send_text(client, data, len, portMAX_DELAY); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-08 17:39:38 +08:00
										 |  |  |         vTaskDelay(1000 / portTICK_PERIOD_MS); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-14 18:09:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 13:14:22 +04:00
										 |  |  |     // Sending text data
 | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Sending fragmented text message"); | 
					
						
							| 
									
										
										
										
											2023-09-08 13:02:28 +04:00
										 |  |  |     memset(data, 'a', sizeof(data)); | 
					
						
							|  |  |  |     esp_websocket_client_send_text_partial(client, data, sizeof(data), portMAX_DELAY); | 
					
						
							|  |  |  |     memset(data, 'b', sizeof(data)); | 
					
						
							|  |  |  |     esp_websocket_client_send_cont_msg(client, data, sizeof(data), portMAX_DELAY); | 
					
						
							|  |  |  |     esp_websocket_client_send_fin(client, portMAX_DELAY); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 13:14:22 +04:00
										 |  |  |     vTaskDelay(1000 / portTICK_PERIOD_MS); | 
					
						
							|  |  |  |     // Sending binary data
 | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Sending fragmented binary message"); | 
					
						
							|  |  |  |     char binary_data[128]; | 
					
						
							|  |  |  |     memset(binary_data, 0, sizeof(binary_data)); | 
					
						
							|  |  |  |     esp_websocket_client_send_bin_partial(client, binary_data, sizeof(binary_data), portMAX_DELAY); | 
					
						
							|  |  |  |     memset(binary_data, 1, sizeof(binary_data)); | 
					
						
							|  |  |  |     esp_websocket_client_send_cont_msg(client, binary_data, sizeof(binary_data), portMAX_DELAY); | 
					
						
							|  |  |  |     esp_websocket_client_send_fin(client, portMAX_DELAY); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     esp_websocket_client_destroy(client); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  | int main(void) | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     ESP_LOGI(TAG, "[APP] Startup.."); | 
					
						
							| 
									
										
										
										
											2023-05-17 07:35:21 +02:00
										 |  |  |     ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); | 
					
						
							|  |  |  |     esp_log_level_set("*", ESP_LOG_INFO); | 
					
						
							| 
									
										
										
										
											2023-02-15 15:41:46 +01:00
										 |  |  |     esp_log_level_set("websocket_client", ESP_LOG_DEBUG); | 
					
						
							|  |  |  |     esp_log_level_set("transport_ws", ESP_LOG_DEBUG); | 
					
						
							|  |  |  |     esp_log_level_set("trans_tcp", ESP_LOG_DEBUG); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(nvs_flash_init()); | 
					
						
							| 
									
										
										
										
											2019-11-29 10:54:02 +01:00
										 |  |  |     ESP_ERROR_CHECK(esp_netif_init()); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  |     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()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     websocket_app_start(); | 
					
						
							| 
									
										
										
										
											2023-06-13 13:23:24 +04:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2019-06-20 15:37:40 +08:00
										 |  |  | } |