Contributed by 黎明将至!, organized by Ai-Thinker
Ai-WB2-32S AT+SOCKET TCP Server Setup (Communication Between Two Modules)
Introduction
Hello everyone! This article introduces how to set up a TCP server on the Ai-WB2-32S using AT+SOCKET commands plus AP mode. You will need two Ai-WB2-32S modules: module 1 acts as the TCP server, and module 2 acts as the TCP client.
Method 1 (One Module Hosts a Hotspot, the Other Connects to It)
Prepare two WiFi-capable modules (one as the server, one as the client).
Step 1: Configure the Server
AT+WMODE=2 (Enable AP mode)
AT+WAP=SDT,12345678,1,3,0 (Configure AP mode parameters)
AT+SOCKET=3,8001 (Configure the server and set the port number)

Step 2: Configure the Client
AT+WMODE=1 (Enable STA mode)
AT+WJAP=SDT,12345678 (Connect to the WiFi broadcast by the server)
AT+WJAP? (Check the server's IP address)
AT+SOCKET=4,192.168.43.1,8001 (Connect to the server; a ConID will be returned)
AT+SOCKETSENDLINE=ConID,3,123 (Send data to the server via the ConID)


Step 3: Check the Server
When the client connects, a ConID is assigned. When the client sends data, you will see the following:
+EVENT:SocketDown,2,3 At this point, use AT+SOCKETREAD=2 (the 2 here is the ConID) to view the actual data;
AT+SOCKETSENDLINE=2,3,456 (Send data to the client)

Step 4: The Client Views the Data
AT+SOCKETREAD=1 (the 1 here is the ConID obtained when connecting to the server)

Method 2 (Both Modules Connect to the Same Hotspot)
Step 1: Create the TCP Server (Commands Run on Module 1)
AT+WMODE=1,1 (Set to STA mode)
AT+WJAP=WiFi account, WiFi password
AT+SOCKET=3,8001 (Create the TCP server and set the port number to 8001)

Step 2: Create the TCP Client (Commands Run on Module 2)
AT+MODE=1,1 (Set to STA mode)
AT+WJAP=WiFi account, WiFi password (connect to the same hotspot as the server)
AT+WJAP?(注意这里为模组一的AT指令,查询TCPServer的IP地址)
AT+SOCKET=4,Server IP address,Server port number (the server port is the one set earlier; after connecting to the TCP server, a ConID will be returned)
AT+SOCKETSENDLINE=ConID,3,123 (ConID obtained from the previous command; 3 is the maximum data length; 123 is the actual data)

Step 3: Read Data from the Client and Send Data to the Client
AT+SOCKETREAD=ConID(这里的指令在模组一里面执行,ConID is the ConID generated when the client connects, i.e., the ConID on the left side of the figure above)
AT+SOCKETSENDLINE=ConID,3,345(模组一执行,ConID is the ConID generated when the client establishes the connection, i.e., the ConID on the left side of the figure above; 3 is the maximum data length to send to the client; 345 is the actual data to send)
AT+SOCKETREAD=ConID(模组二执行,the ConID here is the one generated when the connection to the server was established; read the actual data sent by the server)

3. Notes

Original link: <https://blog.csdn.net/qq_54193285/article/details/135896373?spm=1001.2014.3001.5501>

