TCP-IP AT example¶
This document mainly introduces detailed examples of running the TCP/IP AT command set command on the device.
The device establishes a single connection as a TCP client¶
Set the Wi-Fi mode to station.
order:
AT+WMODE=1,1
respond:
OK
connect to router.
order:
AT+WJAP="aithinker","1234567890"
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explanation:
The SSID and password you entered may be different from the above command. Please use your router’s SSID and password.
Query the device IP address.
order:
AT+WJAP?
respond:
+WJAP:3,aithinker,1234567890,14:de:39:06:86:e4,WPA2 TKIP, 7c:b9:4c:1d:c6:39,6,192.168.3.142,192.168.3.1 OK
explanation:
The results of your query may differ from the responses above.
The PC and the device are connected to the same router.
Use the network debugging tool on the PC to create a TCP server. For example, the IP address of the TCP server is
192.168.3.162, and the port is6666.As a client, the device connects to the TCP server through TCP, the server IP address is
192.168.3.162, and the port is6666.order:
AT+SOCKET=4,192.168.3.162,6666
respond:
connect success ConID=1 OK
Send 9 bytes of data.
order:
AT+SOCKETSEND=1,9
respond:
>
Input 9 bytes of data, for example, the input data is
aithinker, then AT will output the following information.OK
Receive 9 bytes of data.
Suppose the TCP server sends 9 bytes of data (the data is
aithinker), the system will prompt:+EVENT:SocketDown,1,9
At this time, send AT+SOCKETREAD=<ConID> command to read data from the specified connection
order:
AT+SOCKETREAD=1
respond:
+SOCKETREAD,1,9,aithinker
The device acts as a TCP server to establish multiple connections¶
The following is an example of establishing a TCP server with the device as a softAP; if the device is used as a station, the server can be established in the same way after connecting to the router.
Set the Wi-Fi mode to softAP.
order:
AT+WMODE=2,1
respond:
OK
set softAP。
order:
AT+WAP="aithinker778","12345678",6,3,0
respond:
OK
Query softAP information.
order:
AT+WAP?
respond:
+WAP:aithinker778,12345678,WPA/WPA2 AES,6,3,0,7c:b9:4c:1d:c6:39,1.168.43.1,192.168.43.1 Client Num: 0 OK
explanation:
The address you queried may be different from the one in the above response.
The PC needs to connect to the Wi-Fi hotspot created by the device to ensure that it is on the same network segment.
Use the network debugging tool to create a TCP client on the PC, and connect to the TCP server created by the device.
Create a TCP server.
order:
AT+SOCKET=3,6666
respond:
OK
1.Send 9 bytes of data to link with network connection ID 2.
order:
AT+SOCKETSENDLINE=2,9,aithinker
respond:
OK
explanation:
If the number of bytes input exceeds the length (n) set by
AT+SOCKETSENDLINEcommand, only the first n bytes of data will be sent, andOKwill be responded after sending.
Receive 9 bytes of data from link with network connection ID 2.
Suppose the TCP server sends 9 bytes of data (the data is
aithinker), the system will prompt:+EVENT:SocketSeed,2,1
At this time, send AT+SOCKETREAD=<ConID> command to read data from the specified connection
order:
AT+SOCKETREAD=2
respond:
+SOCKETREAD,1,9,aithinker
turn off the TCP connection.
order:
AT+SOCKETDEL=2
respond:
OK
2.Send 9 bytes of data to link with network connection ID 3.
order:
AT+SOCKETSENDLINE=3,9,aithinker
respond:
OK
explanation:
If the number of bytes input exceeds the length (n) set by
AT+SOCKETSENDLINEcommand, only the first n bytes of data will be sent, andOKwill be responded after sending.
Receive 9 bytes of data from link with network connection ID 3.
Suppose the TCP server sends 9 bytes of data (the data is
aithinker), the system will prompt:+EVENT:SocketSeed,3,1
At this time, send AT+SOCKETREAD=<ConID> command to read data from the specified connection
order:
AT+SOCKETREAD=3
respond:
+SOCKETREAD,1,9,aithinker
turn off the TCP connection.
order:
AT+SOCKETDEL=3
respond:
OK
The device acts as a UDP server to establish a single connection to realize UART Wi-Fi transparent transmission¶
Set the Wi-Fi mode to station.
order:
AT+WMODE=1,1
respond:
OK
Connect to the router.
order:
AT+WJAP="aithinker","1234567890"
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explanation:
The SSID and password you entered may be different from the above command. Please use your router’s SSID and password.
Query the device IP address.
order:
AT+WJAP?
respond:
+WJAP:3,aithinker,1234567890,14:de:39:06:86:e4,WPA2 TKIP, 7c:b9:4c:1d:c6:39,6,192.168.3.142,192.168.3.1 OK
explanation:
The results of your query may differ from the responses above.
Your query results may differ from those in respond above.
Using a network debugging tool on the PC, create a UDP transport. For example, the IP address of the PC is
192.168.3.162, and the port is6666.Use the network debugging tool on the PC as the client to connect to the UDP server of the device through UDP. The server IP address is
192.168.3.142, and the port is6666.order:
AT+SOCKET=1,6666
respond:
connect success ConID=1 OK
Send 9 bytes of data.
order:
AT+SOCKETTT
respond:
>
Input 9 bytes of data, for example, the input data is
aithinker, then AT will output the following information.OK
Receive 9 bytes of data.
Suppose the TCP server sends 9 bytes of data (the data is
aithinker), the system will prompt:+EVENT:SocketDown,1,9
At this time, send AT+SOCKETREAD=<ConID> command to read data from the specified connection
order:
AT+SOCKETREAD=1
respond:
+SOCKETREAD,1,9,aithinker
turn off the UDP connection.
order:
AT+SOCKETDEL=1
respond:
OK
The device establishes a single connection as a UDP client¶
Set the Wi-Fi mode to station.
order:
AT+WMODE=1,1
respond:
OK
Connect to the router.
order:
AT+WJAP="aithinker","1234567890"
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explanation:
The SSID and password you entered may be different from the above command. Please use your router’s SSID and password.
Query the device IP address.
order:
AT+WJAP?
respond:
+WJAP:3,aithinker,1234567890,14:de:39:06:86:e4,WPA2 TKIP, 7c:b9:4c:1d:c6:39,6,192.168.3.142,192.168.3.1 OK
explanation:
The results of your query may differ from the responses above.
The PC and the device are connected to the same router.
Use the network debugging tool on the PC to create a UDP server. For example, the IP address of the UDP server is
192.168.3.162, and the port is6666.The device connects to the UDP server via UDP as a client.
order:
AT+SOCKET=2,192.168.3.162,6666
respond:
connect success ConID=1 OK
Send 9 bytes of data.
order:
AT+SOCKETSEND=1,9
respond:
>
Input 9 bytes of data, for example, the input data is
aithinker, then AT will output the following information.OK
Receive 9 bytes of data.
Suppose the UDP server sends 9 bytes of data (the data is
aithinker), the system will prompt:+EVENT:SocketDown,1,9
At this time, send AT+SOCKETREAD=<ConID> command to read data from the specified connection
order:
AT+SOCKETREAD=1
respond:
+SOCKETREAD,1,9,aithinker
turn off the UDP connection.
order:
AT+SOCKETDEL=1
respond:
OK
The device acts as a TCP client, establishes a single connection, and realizes UART Wi-Fi transparent transmission¶
Set the Wi-Fi mode to station.
order:
AT+WMODE=1,1
respond:
OK
Connect to the router.
order:
AT+WJAP="aithinker","1234567890"
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explanation:
The SSID and password you entered may be different from the above order. Please use your router’s SSID and password.
Query the device IP address.
order:
AT+WJAP?
respond:
+WJAP:3,aithinker,1234567890,14:de:39:06:86:e4,WPA2 TKIP, 7c:b9:4c:1d:c6:39,6,192.168.3.142,192.168.3.1 OK
explanation:
Your query results may differ from those in respond above.
The PC is connected to the same router as the device.
Use the network debugging tool on the PC to create a TCP server. For example, the IP address is
192.168.3.162and the port is6666.As a client, the device connects to the TCP server through TCP, the server IP address is
192.168.3.162, and the port is6666.order:
AT+SOCKET=4,192.168.3.162,6666
respond:
OK
Enter UART Wi-Fi transparent mode and send data.
order:
AT+SOCKETTT
respond:
>
stop sending data
In the process of sending data through transparent transmission, if a single packet of data
+++is recognized, the system will exit the transparent transmission. At this point, please wait at least 1 second before sending another AT order. Please note that if you type+++directly with the keyboard, it may not be recognized as three consecutive+because the time is too slow.Important
Use
+++to exit transmission mode and return to receive mode, and the TCP connection is still valid at this time. You can also useAT+SOCKETTTorder to resume transparent transmission.turn off the TCP connection.
order:
AT+SOCKETDEL=1
respond:
OK
The device acts as a TCP server, establishes a single connection, and realizes UART Wi-Fi transparent transmission¶
Set the Wi-Fi mode to station.
order:
AT+WMODE=1,1
respond:
OK
Connect to the router.
order:
AT+WJAP="aithinker","1234567890"
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explanation:
The SSID and password you entered may be different from the above order. Please use your router’s SSID and password.
Query the device IP address.
order:
AT+WJAP?
respond:
+WJAP:3,aithinker,1234567890,14:de:39:06:86:e4,WPA2 TKIP, 7c:b9:4c:1d:c6:39,6,192.168.3.142,192.168.3.1 OK
explanation:
Your query results may differ from those in respond above.
The PC is connected to the same router as the device.
Use the network debugging tool on the PC to create a TCP client. For example, the IP address is
192.168.3.142and the port is6666. Query by AT+WJAP? commandAs a client, the network debugging tool connects to the device through TCP. The IP address of the device is
192.168.3.142, and the port is6666.order:
AT+SOCKET=3,6666
respond:
connect success ConID=1 OK
Enter UART Wi-Fi transparent mode and send data.
order:
AT+SOCKETTT
respond:
>
stop sending data
In the process of sending data through transparent transmission, if a single packet of data
+++is recognized, the system will exit the transparent transmission. At this point, please wait at least 1 second before sending another AT order. Please note that if you type+++directly with the keyboard, it may not be recognized as three consecutive+because the time is too slow.Important
Use
+++to exit transmission mode and return to receive mode, and the TCP connection is still valid at this time. You can also useAT+SOCKETTTorder to resume transparent transmission.Close the TCP connection, fill in the parameters here to fill in the link added by the client, you need to add 1
order:
AT+SOCKETDEL=2
respond:
OK
Implement large text request method through TCP SSL(HTTPS) (This example needs to remove the serial port tool return feed function)¶
Set the Wi-Fi mode to station.
command:
AT+WMODE=1,1\r\n
respond:
OK
Connect to router.
command:
AT+WJAP="aithinker","1234567890"\r\n
respond:
+EVENT:WIFI_CONNECT OK +EVENT:WIFI_GOT_IP
explain:
The SSID and password you enter may differ from those in the command above. Please use your router’s SSID and password.
Set the socket receiving mode.
command:
AT+SOCKETRECVCFG=1\r\n
respond:
OK
Set the socket(in SSL mode) connection to be created.
command:
AT+SOCKET=7,httpbin.org,443\r\n
respond:
OK
explain:
The domain name or IP address of the server you want to connect to is different. Please enter the domain name or IP address of the server you want to connect to.
Set the socket to send data of a specified length.
command:
AT+SOCKETSEND=1,237\r\n
respond:
OK
explain:
Your ID is different in length from the bytes you want to send. Please use your actual connection ID and length.
Length Indicates the number of bytes in the CRLF format
Send the specified length data when “>” appears.
command:
POST /post HTTP/1.1\r\nHost: httpbin.org\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 123\r\n\r\n{"key1": 123,"key2": 123,"key3": 123,"key4": 123,"key5": 123,"key6": 123","key7": 123,"key8": 123,"key9": 123,"key10": 123}respond:
{ "args": {}, "data": "", "files": {}, "form": { "{\"key1\": 123,\"key2\": 123,\"key3\": 123,\"key4\": 123,\"key5\": 123,\"key6\": 123\",\"key7\": 123,\"key8\": 123,\"key9\": 123,\"key10\": 123}": "" }, "headers": { "Content-Length": "123", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-64e576bf-30fc9a971f2aace64ef99f95" }, "json": null, "origin": "120.234.24.230", "url": "https://httpbin.org/post" } OK
explain:
Your request field is different. Please refer to your actual requested field.
The MCU sends an escape character.
