1- //***Head_Files***
1+ //***Head_Files***//
22#include <stdio.h>
33#include <stdlib.h>
44#include <string.h>
1616#include "wifi.h"
1717#include "main.h"
1818#include <driver/i2s_std.h>
19- //***Parameters***
19+ #include "lwip/sockets.h"
20+
21+ //***Parameters***//
2022//Tags
2123#define MAINTAG "main"
2224#define I2STAG "I2S"
25+ #define TCPTAG "TCP"
2326//UART pin assign
2427#define TXD_PIN (GPIO_NUM_17)
2528#define RXD_PIN (GPIO_NUM_18)
26-
2729/* I2S port and GPIOs */
2830static bool stop_music_flag = true;
2931static i2s_chan_handle_t tx_handle = NULL ;
@@ -40,18 +42,21 @@ static i2s_chan_handle_t rx_handle = NULL;
4042#define I2S_DI_IO GPIO_NUM_NC
4143extern const uint8_t music_pcm_start [] asm("_binary_car_2_raw_start" );
4244extern const uint8_t music_pcm_end [] asm("_binary_car_2_raw_end" );
43- static TaskHandle_t i2s_music_task_handle = NULL ; // Task handle for the music playback
44- static bool is_music_playing = false; // Flag to track if music is playing
45-
45+ static TaskHandle_t i2s_music_task_handle = NULL ;
46+ static bool is_music_playing = false;
47+ //TCP UART DEBUG
48+ #define TCP_PORT 8080
49+ #define TCP_TAG "TCP_SERVER"
50+ static int tcp_client_socket = -1 ;
51+ static int tcp_server_socket = -1 ;
52+ static struct sockaddr_in server_addr , client_addr ;
53+ static TaskHandle_t tcp_server_task_handle = NULL ;
54+ #define LOG_BUFFER_SIZE 512
55+ static char log_buffer [LOG_BUFFER_SIZE ];
4656//Queue
4757QueueHandle_t msg_queue = NULL ;
4858
49- //Data type
50-
51- //***Functions***
52-
53- // Initializzation
54- //uart init
59+ //***Functions***//
5560void init_uart () {
5661 const uart_config_t uart_config = {
5762 .baud_rate = 115200 ,
@@ -63,17 +68,99 @@ void init_uart() {
6368 uart_param_config (UART_NUM_1 , & uart_config );
6469 uart_set_pin (UART_NUM_1 , TXD_PIN , RXD_PIN , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
6570 uart_driver_install (UART_NUM_1 , 4096 * 5 , 0 , 0 , NULL , 0 );
71+
72+ uart_driver_install (UART_NUM_0 , 4096 * 5 , 0 , 0 , NULL , 0 );
73+
6674}
75+ int custom_log_vprintf (const char * fmt , va_list args ) {
76+ int len = vsnprintf (log_buffer , LOG_BUFFER_SIZE , fmt , args );
77+ if (len > 0 && tcp_client_socket > 0 ) {
78+ int sent = send (tcp_client_socket , log_buffer , len , 0 );
79+ if (sent < 0 ) {
80+ ESP_LOGE ("LOG_TCP" , "Failed to send log via TCP: errno %d" , errno );
81+ }
82+ }
83+ return vprintf (fmt , args );
84+ }
85+ void setup_log_redirection () {
86+ esp_log_set_vprintf (custom_log_vprintf );
87+ }
88+ void tcp_server_task (void * pvParameters ) {
89+ struct sockaddr_in server_addr , client_addr ;
90+ socklen_t client_addr_len = sizeof (client_addr );
91+
92+ // 创建服务器套接字
93+ tcp_server_socket = socket (AF_INET , SOCK_STREAM , 0 );
94+ if (tcp_server_socket < 0 ) {
95+ ESP_LOGE (TCPTAG , "Unable to create socket: errno %d" , errno );
96+ vTaskDelete (NULL );
97+ return ;
98+ }
6799
68- //queue init
69- void queue_init (){
70- msg_queue = xQueueCreate (50 , sizeof (msg_struct ));
71- if (msg_queue == NULL ) {
72- ESP_LOGE (MAINTAG , "Failed to create queue" );
100+ // 设置服务器地址
101+ server_addr .sin_family = AF_INET ;
102+ server_addr .sin_addr .s_addr = htonl (INADDR_ANY );
103+ server_addr .sin_port = htons (TCP_PORT );
104+
105+ // 绑定套接字
106+ if (bind (tcp_server_socket , (struct sockaddr * )& server_addr , sizeof (server_addr )) < 0 ) {
107+ ESP_LOGE (TCPTAG , "Socket unable to bind: errno %d" , errno );
108+ close (tcp_server_socket );
109+ vTaskDelete (NULL );
110+ return ;
111+ }
112+
113+ // 开始监听
114+ if (listen (tcp_server_socket , 1 ) < 0 ) {
115+ ESP_LOGE (TCPTAG , "Error occurred during listen: errno %d" , errno );
116+ close (tcp_server_socket );
117+ vTaskDelete (NULL );
118+ return ;
119+ }
120+ ESP_LOGI (TCPTAG , "Socket listening on port %d" , TCP_PORT );
121+
122+ // 主循环
123+ while (1 ) {
124+ vTaskDelay (1000 / portTICK_PERIOD_MS );
125+ ESP_LOGI (TCPTAG , "Waiting for a new client..." );
126+ tcp_client_socket = accept (tcp_server_socket , (struct sockaddr * )& client_addr , & client_addr_len );
127+ if (tcp_client_socket < 0 ) {
128+ ESP_LOGE (TCPTAG , "Unable to accept connection: errno %d" , errno );
129+ vTaskDelay (100 / portTICK_PERIOD_MS );
130+ continue ;
131+ }
132+ ESP_LOGI (TCPTAG , "Client connected" );
133+
134+ // 客户端通信循环
135+ while (1 ) {
136+ vTaskDelay (1000 / portTICK_PERIOD_MS );
137+ char recv_buf [64 ]; // 临时接收缓冲区
138+ int len = recv (tcp_client_socket , recv_buf , sizeof (recv_buf ) - 1 , 0 );
139+ if (len <= 0 ) {
140+ ESP_LOGE (TCPTAG , "Client disconnected or error: errno %d" , errno );
141+ close (tcp_client_socket );
142+ tcp_client_socket = -1 ; // 重置客户端套接字
143+ break ;
144+ }
145+
146+ recv_buf [len ] = '\0' ; // 确保接收到的数据是字符串
147+ ESP_LOGI (TCPTAG , "Received data: %s" , recv_buf );
148+
149+ // 示例:将收到的数据原样返回
150+ if (send (tcp_client_socket , recv_buf , len , 0 ) < 0 ) {
151+ ESP_LOGE (TCPTAG , "Error occurred during sending: errno %d" , errno );
152+ close (tcp_client_socket );
153+ tcp_client_socket = -1 ; // 重置客户端套接字
154+ break ;
155+ }
156+ }
73157 }
158+
159+ // 关闭服务器套接字
160+ close (tcp_server_socket );
161+ vTaskDelete (NULL );
74162}
75163
76- //i2s music
77164static esp_err_t i2s_driver_init (void ) {
78165 i2s_chan_config_t chan_cfg =
79166 I2S_CHANNEL_DEFAULT_CONFIG (I2S_NUM , I2S_ROLE_MASTER );
@@ -102,8 +189,6 @@ static esp_err_t i2s_driver_init(void) {
102189 ESP_ERROR_CHECK (i2s_channel_init_std_mode (tx_handle , & std_cfg ));
103190 return ESP_OK ;
104191}
105-
106-
107192static void i2s_music (void * args )
108193{
109194 esp_err_t ret = ESP_OK ;
@@ -112,7 +197,6 @@ static void i2s_music(void *args)
112197 ESP_ERROR_CHECK (i2s_channel_enable (tx_handle ));
113198 while (1 ) {
114199 if (stop_music_flag ) {
115- // 停止播放逻辑
116200 // ESP_LOGI(I2STAG, "[music] Music playback stopped.");
117201 vTaskDelay (100 / portTICK_PERIOD_MS );
118202 continue ;
@@ -146,7 +230,6 @@ static void i2s_music(void *args)
146230
147231 vTaskDelete (NULL );
148232}
149-
150233void start_music_playback () {
151234 if (!is_music_playing ) {
152235 ESP_LOGI (MAINTAG , "Starting engine sound playback." );
@@ -156,7 +239,6 @@ void start_music_playback() {
156239 ESP_ERROR_CHECK (i2s_channel_enable (tx_handle ));
157240 }
158241}
159-
160242void stop_music_playback () {
161243 if (is_music_playing ) {
162244 ESP_LOGI (MAINTAG , "Stopping engine sound playback." );
@@ -167,15 +249,19 @@ void stop_music_playback() {
167249 }
168250}
169251
170-
171- // Main function to handle data and trigger music playback
252+ void queue_init (){
253+ msg_queue = xQueueCreate (20 , sizeof (msg_struct ));
254+ if (msg_queue == NULL ) {
255+ ESP_LOGE (MAINTAG , "Failed to create queue" );
256+ }
257+ }
172258void send_msg_to_stm32 (void * pvParameters ) {
173259
174260 msg_struct msg_data ;
261+
175262 while (1 ) {
263+ char uart_data [8192 ];
176264 if (xQueueReceive (msg_queue , & msg_data , portMAX_DELAY )) {
177- char uart_data [2048 ];
178-
179265 switch (msg_data .type ) {
180266 case DataType_Control :
181267 snprintf (uart_data , sizeof (uart_data ), "{\"Type\":\"Control\",\"L\":%d,\"R\":%d,\"A\":%d}\n" ,
@@ -197,13 +283,14 @@ void send_msg_to_stm32(void *pvParameters) {
197283 break ;
198284
199285 case DataType_PID :
200- snprintf (uart_data , sizeof (uart_data ), "{\"Type\":\"PID\",\"Balance_Kp\":%f,\"Balance_Ki\":%f,\"Balance_Kd\":%f,\"Velocity_Kp\":%f,\"Velocity_Ki\":%f,\"Velocity_Kd\":%f}\n" ,
286+ snprintf (uart_data , sizeof (uart_data ), "{\"Type\":\"PID\",\"Balance_Kp\":%f,\"Balance_Ki\":%f,\"Balance_Kd\":%f,\"Velocity_Kp\":%f,\"Velocity_Ki\":%f,\"Velocity_Kd\":%f,\"BalanceShift\":%f }\n" ,
201287 msg_data .balancePID .Kp ,
202288 msg_data .balancePID .Ki ,
203289 msg_data .balancePID .Kd ,
204290 msg_data .velocityPID .Kp ,
205291 msg_data .velocityPID .Ki ,
206- msg_data .velocityPID .Kd );
292+ msg_data .velocityPID .Kd ,
293+ msg_data .balanceshift );
207294 break ;
208295
209296 case DataType_Mode_Control :
@@ -218,12 +305,14 @@ void send_msg_to_stm32(void *pvParameters) {
218305 uart_write_bytes (UART_NUM_1 , uart_data , strlen (uart_data ));
219306 ESP_LOGI (MAINTAG , "Sent data to UART: %s" , uart_data );
220307 }
308+ vTaskDelay (100 / portTICK_PERIOD_MS );
221309 }
222310}
223311
224312//Main function
225313void app_main (void )
226314{
315+ BaseType_t result ;
227316 // Initialize NVS
228317 esp_err_t ret ;
229318 ret = nvs_flash_init ();
@@ -232,26 +321,37 @@ void app_main(void)
232321 ret = nvs_flash_init ();
233322 }
234323 ESP_ERROR_CHECK ( ret );
324+ setup_log_redirection ();
235325
236326 // Initialize WIFI
237327 initialise_wifi ();
238-
239328 // Start getting time and weather
240329 // weather_time_task_init();
241-
330+
242331 //Queue init
243332 queue_init ();
244-
245333 // Intialize UART
246334 init_uart ();
247335
248- //Initialize Blutooth
249- ble_init ();
250-
251336 //Task init
252- xTaskCreate (send_msg_to_stm32 , "send_msg_to_stm32" , 4096 * 20 , NULL , 10 , NULL );
253-
337+ if (xTaskCreate (send_msg_to_stm32 , "send_msg_to_stm32" , 8192 , NULL , 10 , NULL ) != pdPASS )
338+ {
339+ ESP_LOGE (MAINTAG , "Failed to create send_msg_to_stm32 task" );
340+ }
254341 //I2S init
255342 i2s_driver_init ();
256- xTaskCreate (i2s_music , "i2s_music" , 4096 * 5 , NULL , 5 , & i2s_music_task_handle );
343+ if (xTaskCreate (i2s_music , "i2s_music" , 8192 , NULL , 5 , & i2s_music_task_handle ) != pdPASS )
344+ {
345+ ESP_LOGE (MAINTAG , "Failed to create i2s_music task" );
346+ }
347+
348+
349+ //Initialize Blutooth
350+ ble_init ();
351+ if (xTaskCreate (tcp_server_task , "tcp_server_task" , 4096 , NULL , 6 , & tcp_server_task_handle ) != pdPASS )
352+ {
353+ ESP_LOGE (MAINTAG , "Failed to create tcp_server_task task" );
354+ }
355+ size_t free_heap = xPortGetFreeHeapSize ();
356+ printf ("Free heap size: %d bytes\n" , free_heap );
257357}
0 commit comments