Lesson 04: UART1 Serial Communication and AT Commands¶
1. Course Introduction¶
Connect the ESP8266/Crowtail Serial Wi-Fi module via UART1, send AT commands, read responses, and establish Wi-Fi/TCP services.
2. Learning Objectives¶
After completing this lesson, you should be able to:
- Correctly cross-connect the TX/RX lines between UART1 and the Wi-Fi module.
- Understand timeout-based reading, the response buffer, and
OKdetermination. - Follow the task flow of "initialization → reset → connection → TCP service startup."
3. What You Need to Prepare¶
-
Prepare the CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display development board and a USB-C cable capable of data transfer, as required for this lesson.
-
Peripheral: ESP8266/Crowtail Serial Wi-Fi module.
This lesson focuses on how to use the serial (UART) component and how to configure and program the serial port in code. The course uses a serial Wi-Fi module as an example to demonstrate, helping you master the basic development workflow of serial communication. If you are using a different serial module, you can also refer to the programming approach in this lesson and write corresponding code based on the module's communication protocol and functions to implement the features you need.
- The Wi-Fi module must use 115200, 8N1 parameters consistent with the code; replace the example SSID/password with your own configuration.
Code reference: 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¶
Double-click to open the Lesson 4 code. (.ino file)
Configure the following options accordingly.
- Board:
ESP32P4 Dev Module - Core Debug Level:
Info - Flash Frequency / Mode / Size:
80MHz/QIO/16MB (128Mb) - Partition Scheme:
16M Flash (3MB APP/9.9MB FATFS) - PSRAM:
Enabled - USB Mode:
Hardware CDC and JTAG - Port: After connecting the USB data cable to the board's UART0, select the newly appeared COM port under "Tools → Port."
5. Hardware Operation Steps¶
With the power off, connect the Wi-Fi serial module: connect UART1's VCC to the module's VCC, GND to GND, GPIO47 (TX) to the module's RX, and GPIO48 (RX) to the module's TX. TX/RX must be cross-connected.
Connect the USB-C cable that supports data transfer to the board's port labeled UART0, then connect it to the computer. The board's power indicator should light up; a corresponding COM port should appear in the Arduino IDE.
Switch the board's mode switch to the UART1 position. The UART1 interface can only connect to an external serial module when the switch is on the UART1 side.
This is the design on the hardware side.
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.)
In the Arduino IDE, confirm that the board, COM port, and upload parameters are correct, then click Upload. After the upload completes, open the Serial Monitor and set the baud rate to 115200.
For details on how to open the Serial Monitor and how to set the baud rate, please see Lesson 1.
Observe whether the Serial Monitor sequentially displays the sent AT commands and the OK returned by the module, and finally shows that Wi-Fi is connected along with the IP address. If there is no response, first check whether TX/RX are cross-connected, whether GND is shared (common ground), and the position of the mode switch.
6. Key Code Explanation¶
6.1 UART Initialization and Response Reading¶
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1,
UART1_EXTRA_GPIO_RXD,
UART1_EXTRA_GPIO_TXD);
setup() starts the debug UART with Serial.begin(115200) and the external UART with Serial1.begin(115200, SERIAL_8N1, UART1_EXTRA_GPIO_RXD, UART1_EXTRA_GPIO_TXD). uart_read_response() polls Serial1 until the timeout expires or the buffer is full, yields with vTaskDelay() when no byte is available, and appends a terminating \0 before the buffer is treated as a C string.
6.2 AT Command Encapsulation¶
SendData(cmd);
SendData("\r\n");
uart_read_response(response, AT_RESPONSE_MAX, timeout);
return strstr(response, "OK") != NULL;
SendData() writes the command bytes to Serial1. send_at_command() sends the command and \r\n, reads the response, and returns whether strstr(response, "OK") succeeds. connect_wifi() formats the AP credentials with snprintf() before passing the command to this wrapper.
6.3 FreeRTOS Wi-Fi Task¶
send_at_command("AT+CWMODE=3", ...);
send_at_command("AT+RST", ...);
for (int i = 0; i < 5; ++i) {
if (connect_wifi()) break;
}
wifi_task() initializes both serial ports, configures AP+STA mode with AT+CWMODE=3, resets the module, and retries connect_wifi() up to five times. After a successful join it queries the address and enables multiple connections and the TCP server with AT+CIFSR, AT+CIPMUX=1, and AT+CIPSERVER=1,80.
7. Experimental Observations¶
- The log shows UART initialization, AT response, successful Wi-Fi connection, and TCP service startup messages.
8. Common Issues and Troubleshooting¶
- If there is no response, first confirm whether TX/RX are cross-connected; if only
ATappears withoutOK, check the module's firmware baud rate. - If the connection fails, check the SSID/password, antenna, and module power supply.






