ESP32-WROOM-32 UDP unicast transparent transmission AT command routine

ESP32-WROOM-32

insert image description here

foreword

The last time I demonstrated three TCP connection and data transmission methods between ESP32 and mobile phone, now follow the previous article " ESP32-WROOM-32 AT command to configure TCP communication " to complete the UDP and unicast transparent transmission between ESP32 and mobile phone, and the preparation work can Go back to the previous article to check, all the preparations are the same, so I won’t go into details here.
Regarding the APP of the network debugging assistant, you can directly download it from the mobile app store or search for "Network Debugging Assistant" or "TCP Network Debugging Assistant" on the Internet. There should be one. The IOS system mobile phone used here is a demonstration, so there is no way Share APP installation package.

Firmware burning

This article needs to use the factory AT firmware of Espressif’s official ESP32-WROOM-32 to burn with Flash Download Tools. (If the ESP32 itself is burned with Espressif’s factory firmware, or it comes from the “TCP” article, then you can skip this step directly) This step is simply skipped.

Open Flash Download Tools and load it, the path in the firmware package is

X:XXX\ESP32-WROOM-32_AT_Bin_V2.4.0.0\ESP32-WROOM-32-V2.4.0.0\factory

The firmware named "factory_WROOM-32.bin" in it has a starting address of 0, check "DoNotChgBin", and select the serial port to start downloading.
insert image description here

Test AT commands

Use USB to TTL to connect to UART2 of ESP32-WROOM-32, power on,
open the serial port assistant
, the baud rate is 115200, the stop bit is 1, the data bit is 8, send "AT" without parity
, check whether the burned AT firmware is normal operation

ESP32 USB to TTL
RX2 TX
TX2 RX
GND GND
VCC 5V

insert image description here
AT firmware works fine.

UDP unicast communication\transparent transmission

Configure SoftAP on ESP32

Before implementing UDP unicast communication\transparent transmission between ESP32 and mobile phone, the two need to be connected through WIFI. The following uses SoftAP of ESP32 as an example.

  1. Then after the above AT command test, send
AT+CWMODE=2			//打开ESP32的SoftAP模式
/*
AT+CWMODE=<mode>[,<auto_connect>]
<mode>:模式
  0: 无 Wi-Fi 模式,并且关闭 Wi-Fi RF
  1: Station 模式
  2: SoftAP 模式
  3: SoftAP+Station 模式
<auto_connect>
是否启用自动连接 AP 的功能,参数缺省使用默认值,启用自动连接 AP 的功能
参数为0时禁用自动连接 AP 的功能
*/

The above command is used to enable the SoftAP mode of ESP32. The commands used later will not explain the specific content of the corresponding parameters (too long), and those who are interested can check the AT command set by themselves.

  1. Configure the specific parameters of the AP and send
AT+CWSAP="YouXin","1234567890",5,3
/*
SSID为YouXin
password为1234567890
可自行更改上面的参数
*/
  1. send
AT+CWDHCP=1,1
/*
开启DHCP,默认DHCP地址池为192.168.4.2-192.168.101,地址池可改
*/
  1. After enabling DHCP, the mobile phone downloads a "TCP network debugging assistant" (find it online by yourself), the mobile phone finds the WIFI of ESP32, and the SSID configured on it is YouXin, so find the WIFI named YouXin and connect. After connecting to the WIFI of the ESP32, you can write down the information returned by the serial port of the ESP32, which contains information such as the IP address assigned to the connected device, which will be used later in the connection.

UDP communication and transparent transmission between ESP32 and mobile phone

normal transfer mode

– Continued –

  1. After the mobile phone is connected to the WIFI of ESP32, open the "TCP Network Debugging Assistant".
  2. Select "UDP debugging" in the "TCP network debugging assistant" of the mobile phone, and different software may have different names.
  3. "TCP network debugging assistant" input UDP connection parameters, local IP, local binding port number, remote IP address, remote port number.
    Among them, I don’t know what the local IP is. You can see the information returned by the serial port when the mobile phone is connected to the WIFI of the ESP32; for the
    local bound port number, enter an unoccupied one; for the
    remote IP address, enter the IP address of the ESP32, because ESP32 is in SoftAP mode, and its address is generally 192.168.4.1; for
    the remote port number, you can enter a port number that is not occupied by ESP32. When ESP32 establishes a UDP unicast connection, the local port number should be the input port number.
    After entering the above parameters, click Bind. Next, establish a UDP unicast connection on the ESP32 side.
  4. Send commands to the serial port assistant to set the ESP32 communication to single connection mode
4.AT+CIPMUX=0		//设置为单连模式
  1. ESP32 establishes a UDP unicast connection and sends:
AT+CIPSTART="UDP","192.168.4.2",1000,1002,2		//建立UDP单播
/*
 *第一个参数为网络连接类型										*
 *第二给参数为远端IP(即是手机端的IP地址)						*
 *第三个参数为远端端口号(即是手机端绑定UDP时输入的本地端口号)		*
 *第四个参数为本地端口号(即是手机端绑定UDP时输入的远端端口号)		*
 *第五个参数为模式:											*
	0: 接收到 UDP 数据后,不改变对端 UDP 地址信息(默认)
	1: 仅第一次接收到与初始设置不同的对端 UDP 数据时,改变对端 UDP 地址信息为发送数据设备的IP地址和端口
	2: 每次接收到 UDP 数据时,都改变对端 UDP 地址信息为发送数据的设备的 IP 地址和端口

*/

After sending the above command to establish a UDP connection, if the connection to the serial port is successful, the assistant will return relevant information.
At this point, the mobile phone can send data to the ESP32 through the "TCP Network Debugging Assistant", and the ESP32 will print out the data on the serial port after receiving the data.
Although the ESP32 can receive the data sent by the mobile phone, if you want to send data from the ESP32 to the mobile phone at this time, the serial port assistant will return ERROR, and the data has not been sent, and then proceed to the next step.

  1. send
AT+CIPSEND=<Length>		//发送指定长度的数据请求
/*
由于这次实验ESP32充当的是TCP Client,所以是单连模式,这条指令就只有Length一个参数
Length:需要发的数据长度
*/

When the above command is successfully sent, ESP32 enters the state of sending data with a specified length, and can send data with a data length less than Length once. After sending, ESP32 will return to AT mode. Every time you send data, you must first send AT+CIPSEND=XX ( It’s too much trouble, isn’t it?), after entering the transparent transmission, there is no need to send a command to send a data request of a specified length.

demo

UDP unicast communication

insert image description here

UDP transparent transmission

  1. send
AT+CIPCLOSE		//断开UDP连接

Send after disconnecting the UDP connection

AT+CIPSTART="UDP","192.168.4.2",1000,1002,0		//ESP32重新连接,最后一个参数改成0

Re-establish the UDP connection

  1. send
AT+CIPMODE=1		//进入透传模式

Enter transparent transmission mode.

  1. After entering the transparent transmission mode, send the command to start the transmission to start the transparent transmission
AT+CIPSEND		//开始发送数据

After this step is successful, the ESP32 enters the state of transparently transmitting data, and sending AT commands during this period is invalid.

  1. Exit the transparent transmission. After the transparent transmission is completed, the transparent transmission is not needed, and the ESP32 returns to the AT command state to send
+++	

Notice发送退出透传的指令时,不要带换行

  1. send
17.AT+CIPCLOSE	//断开UDP连接

Disconnect ESP32 and mobile phone for UDP unicast communication/transparent transmission is over.

demo

UDP unicast transparent transmission

insert image description here

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/131235035