Skip to content

5inch_P4_IDF_04_Serial_Port_Usage: ESP32-P4 Serial Port Communication and AT Command Networking

1. Course Introduction

In this lesson, we use the ESP-IDF UART driver to perform serial communication with an external WiFi module via ESP32-P4's UART1 (TX=GPIO47, RX=GPIO48). After the program is flashed, the development board uses AT commands to control the WiFi module to switch to AP+STA mode, connect to a specified router, query the IP address, and start a TCP server. The serial monitor prints each AT command and the module's responses in real time.

Building on the GPIO fundamentals from Lesson 02, this lesson introduces UART serial communication and AT command interaction for the first time. Learners need to prepare a WiFi module that supports AT command control (such as an ESP8266). Through this experiment, learners will complete end-to-end validation of the full chain: UART initialization, data transmission and reception, AT command sending, and response parsing.

2. Learning Objectives

  • Be able to open the Lesson04 project in ESP-IDF and set the target chip to esp32p4.
  • Be able to explain the call order and purpose of uart_driver_install, uart_set_pin, and uart_param_config.
  • Be able to explain the reason why AT commands end with \r\n, and how to determine whether a command executed successfully.
  • Be able to complete compilation and flashing, and observe the AT command and response interaction process in the serial monitor.
  • Be able to determine whether module communication is normal based on whether the serial output prints OK and WiFi Connected.

3. Preparations

  • Applicable development board: CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display Development Board.
  • Wiring: Connect ESP32-P4's GPIO47 (TX) to the WiFi module's RX; connect ESP32-P4's GPIO48 (RX) to the WiFi module's TX; connect the grounds together.
  • Software: VS Code, ESP-IDF Extension (ESP-IDF v5.5.4 or later).
  • Project dependencies: Keep the main/main.c file and the peripheral/bsp_uart component.
  • Network: An available 2.4 GHz WiFi router; the SSID and password must be written into the WIFI_SSID and WIFI_PASS macros in main.c.

Code download link:

https://github.com/Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/tree/master/example/V1.0

4. Software Operation Steps

The operations in this section follow the ESP-IDF basic workflow from the first lesson. First confirm that ESP-IDF v5.5.4, the UART flashing method, the actual serial port, and the target chip esp32p4 are set correctly, then check the project, components, configuration, and run results against the images below item by item.

In File Explorer, locate this lesson's project directory, right-click and select to open it with VS Code;

Open this lesson's code (Lesson 4); the images are for illustration only.

In File Explorer, locate this lesson's project directory, right-click and select to open it with VS Code; after opening, confirm that the project name in the window matches this lesson

After opening, confirm that the project name in the window matches this lesson.

First select the code execution environment ESP-IDF v5.5.4, set the flashing method to UART, then select the serial port that actually corresponds to the development board. Then in the ESP-IDF Extension panel, click Set Espressif Device Target and select esp32p4.

After setup, the status bar should display ESP-IDF v5.5.4, UART, the required COM port, and ESP32-P4.

Confirm ESP-IDF, UART, serial port, and target chip

Click SDK Configuration Editor in the VS Code bottom status bar or the ESP-IDF extension panel, and wait for the configuration page to fully load before modifying parameters. If the page is still loading, do not run Build immediately.

View the LED control main program

Set the target chip

In the search box, type flash, and ensure Flash SPI mode: QIO; Flash Sampling Mode: STR Mode; Flash SPI speed: 80 MHz; Flash size: 16 MB. These parameters should match the on-board Flash of the Advance-P4.

Open the SDK Configuration Editor

After confirming the configuration is correct, click Save in the upper right corner; make sure the changes are saved, then run Build to compile.

Click Full Clean to clear the cache left by the previous compilation. Run this operation after the first compilation, after switching project configurations, or after modifying SDK parameters, to avoid old configurations affecting new compilation results.

Run Full Clean

Before compiling, re-check the initialization of GPIO48 and the blinking task code, then click Build.

Compile the project

Confirm the development board is connected to the computer via USB, then click Select Port to Use to choose the corresponding serial port.

Click Flash to flash the firmware.

Select the serial port

After flashing completes, you can click Monitor to view the serial log and confirm whether the task was created normally. Press Ctrl + ] to exit the monitor.

Flash the firmware

Finally, you can use the one-click operation button in the ESP-IDF status bar to run compilation, flashing, and open the serial monitor in sequence. Use this only after the project configuration, serial port, and code have all been confirmed correct; if you need to locate a problem, still follow the steps above item by item.

Open the monitor

5. Hardware Operation Steps

With the power off, use DuPont wires to connect ESP32-P4's GPIO47 (TX) to the WiFi module's RX, connect GPIO48 (RX) to the WiFi module's TX, and connect the GND of both. Note that TX/RX must be cross-connected.

(Connect the VCC of UART1 interface to the VCC pin of the wifi module)

(Connect the GND of UART1 interface to the GND pin of the wifi module)

(Turn the TX of UART1 interface to the RX pin of the wifi module) (Cross connection)

(Turn the RX of UART1 interface to the TX pin of the wifi module) (Cross connection)

Connect the WiFi module wiring

Use a USB Type-C data cable that supports data transfer to connect the ESP32-P4 development board to the computer. After connecting, the development board's power indicator should light up.

The image shows the connection locations of the development board to the external module or USB data cable; power off before plugging or unplugging, and verify the connector orientation and supply voltage

Then, switch the toggle switch on the 5-inch Advance-P4 to the UART1 position. Only in this way can the UART1 interface be used.

image-20260728143059267

This is the design on the hardware side.

image-20260728143125064

Switch to UART1 port:

Among the three interfaces shown in the figure, only the UART1 interface can be used at this time.

Alternatively, the expansion header at the bottom can also be used.

That is, either the UART1 interface or the expansion header can be used, but not both.

Switch to Wireless Module port:

Among the three interfaces shown in the figure, only the wireless module can be used at this time.

Alternatively, the expansion header at the bottom can also be used.

That is, either the wireless module or the expansion header can be used, but not both.

Summary:

The UART1 interface and the Wireless Module can only be used when switched to the corresponding port.

The expansion header at the bottom can be used regardless of the position of the mode switch, but it cannot be used simultaneously with the above interfaces. (When used simultaneously, only one of the three interfaces can be selected.)

After burning the code, you will be able to see the AT commands you sent through the monitor on ESP-IDF, as well as the responses returned to you by the wifi module via the serial port. (Green is sent by Advance-P4, and white is the response from the wifi module)

In the serial monitor, observe whether the AT+CWMODE, AT+RST, AT+CWJAP and other commands are printed in sequence along with their returned OK, and finally WiFi Connected and the IP address are printed. Observe the AT command interaction

6. Key Code Explanation

static bool send_at_command(const char *cmd, TickType_t timeout)
{
    char response[AT_RESPONSE_MAX] = {0};
    SendData(cmd);
    SendData("\r\n");
    uart_read_response(response, AT_RESPONSE_MAX, timeout);
    return strstr(response, "OK") != NULL;
}

send_at_command() sends the command text followed by the required carriage-return/line-feed terminator. It collects the reply through uart_read_response() and treats a response containing OK as success. The fixed 512-byte buffer limits the amount of returned text stored by one command.

while ((xTaskGetTickCount() - start) < timeout && total < len - 1) {
    read_bytes = uart_read_bytes(UART_NUM_2, (uint8_t *)(buffer + total),
                                 len - total - 1, 20 / portTICK_PERIOD_MS);
    if (read_bytes > 0) total += read_bytes;
}
buffer[total] = '\0';

The response may arrive in several UART fragments. This loop accumulates fragments until the overall timeout expires or the destination buffer is full, then adds the string terminator required by strstr() and logging functions.

send_at_command("AT+CWMODE=3", pdMS_TO_TICKS(1000));
send_at_command("AT+RST", pdMS_TO_TICKS(2000));
...
send_at_command("AT+CIFSR", pdMS_TO_TICKS(1000));
send_at_command("AT+CIPMUX=1", pdMS_TO_TICKS(1000));
send_at_command("AT+CIPSERVER=1,80", pdMS_TO_TICKS(1000));

After uart_init(), the task selects AP+STA mode, resets the external Wi-Fi module, and retries AT+CWJAP up to five times. Once connected, it queries the IP address, enables multiple TCP connections, and opens a server on port 80. If all connection attempts fail, the task deletes itself instead of continuing with an unavailable network.

7. Experimental Observations

After the program is flashed and reset, the serial monitor should sequentially output the AT command send content, the module response, and the network connection status.

If the WiFi module is wired correctly and the router is reachable, the serial port should print WiFi Connected and the IP address obtained by the module within a few seconds. If the AT Response is always empty or does not contain OK, first check whether TX/RX are reversed and whether the module baud rate is 115200; if WiFi connection fails, check whether the SSID and password are correct, and whether the module supports 2.4 GHz networks.