esp32-uart2udp is a project based on the ESP-IDF framework that implements bidirectional data forwarding between UART and UDP on ESP32 development boards. This project enables forwarding data received from UART interfaces to a specified UDP server and vice versa, making it suitable for scenarios requiring data communication between serial devices and networks.
- Bidirectional Data Forwarding: Supports data transmission from UART to UDP and from UDP to UART.
- Multi-UART Support: Configurable to enable UART1 and/or UART2 (controlled via compilation options).
- WiFi Connectivity: Uses STA mode to connect to a specified WiFi network.
- mDNS Service Discovery: Built-in mDNS service for easy device discovery in the network.
- Configurable Parameters: Customize WiFi information, UART baud rate, pins, etc., via the menu configuration tool.
- Multi-Task Concurrency: Implements asynchronous communication based on FreeRTOS tasks and message buffers to ensure efficient data processing.
- ESP32 series development board (e.g., ESP32-WROOM-32, ESP32-C3, etc.)
- External serial devices (such as sensors, MCUs) connected according to the configured UART pins.
- Stable WiFi network environment.
- ESP-IDF 5.0 or later (with version adaptation logic in the code).
- mDNS component (
espressif/mdns, version ^1.0.3).
Ensure the ESP-IDF development environment is installed. Refer to the ESP-IDF Official Documentation for setup instructions.
git clone https://github.com/your-repo/esp32-uart2udp.git
cd esp32-uart2udpUse the ESP-IDF menu configuration tool to set parameters:
idf.py menuconfigKey configuration items:
- WiFi Configuration: Set WiFi SSID and password (
Component config -> WiFi -> WiFi STA Settings). - UART Configuration:
- Enable UART1 and/or UART2 (
Component config -> UART Configuration). - Configure UART baud rate, TX/RX pins (e.g.,
UART1 BAUD RATE,UART1 TX GPIO, etc.).
- Enable UART1 and/or UART2 (
- mDNS Configuration: Set mDNS hostname (
Component config -> mDNS -> Hostname).
# Compile the project
idf.py build
# Flash to the device (replace COMx with the actual port)
idf.py -p COMx flash
# View logs
idf.py -p COMx monitor- Initialization: On startup, initialize NVS, WiFi (STA mode), UART interfaces, and mDNS service.
- Task Creation:
udp_listener: Listens on the specified UDP port, receives data, and forwards it to the corresponding UART's transmit buffer.udp_client: Reads data from the UART receive buffer and sends it to the specified UDP server.uartX_rx: Reads data from the UART interface and writes it to the UART receive buffer (X is 1 or 2).uartX_tx: Reads data from the UART transmit buffer and sends it via the UART interface (X is 1 or 2).
- Data Forwarding: Uses FreeRTOS message buffers for inter-task data transfer to ensure thread safety.
| Configuration Item | Description | Configuration Path |
|---|---|---|
| WiFi SSID | Target WiFi name | Component config -> WiFi -> SSID |
| WiFi Password | Target WiFi password | Component config -> WiFi -> Password |
| UART1 Enable | Whether to enable UART1 interface | Component config -> UART Configuration -> Enable UART1 |
| UART1 Baud Rate | UART1 communication baud rate (e.g., 115200) | Component config -> UART Configuration -> UART1 BAUD RATE |
| UART1 TX/RX Pins | UART1 transmit/receive pin numbers | Component config -> UART Configuration -> UART1 TX GPIO / UART1 RX GPIO |
| mDNS Hostname | mDNS hostname of the device in the network | Component config -> mDNS -> Hostname |
This project is open-source under the GNU General Public License v3.0. See the LICENSE file for details.