Example Code for Arduino-TCP Communication (AT Commands)

Last revision 2026/01/24

This article offers a comprehensive guide on setting up Arduino for TCP communication using AT commands. It includes hardware and software preparations, sample code for data exchange, and detailed instructions on using AT commands for managing TCP connections with SIM808 modules.

Hardware Preparation

Software Preparation

Preparations

  1. Install the SIM card in the sim slot on the back of the board
    • Make sure your SIM card is available, and you can put the SIM card into the socket or get it out by sliding the metal lock.
  2. Install the GPS & GSM antennas on the board
  3. Connect with PC and power supply(there are three ways to supply the power)
    • Through 7~23V power supply jack
    • Through VIN pin on board
    • Remove the jumper on NO BAT, and connect a 3.7V Lithium Battery to BAT jack
    • After connecting the board to PC by an USB cable, the board shows up as Leonardo automatically if you have installed the Arduino by .exe file, or you will need to install the driver manually
      DFR0355_Leonardo
  4. BOOT SIM808 by pressing the BOOT white button for 2 seconds if the NET indicator LED is OFF.
  5. Upload the debugging code below to Leonardo.

Sample Code

/*
 * The sketch here will make Leonardo exchange
 * data between the USB serial port and SIM808 module.
*/

void setup() {
  Serial.begin(115200); //initialize Serial(i.e. USB port)
  Serial1.begin(115200); //initialize Serial1
}

void loop() {
  //If Serial1 receive data, print out to Serial
  while (Serial1.available()) {
    Serial.write(Serial1.read());
  }
  //If Serial receive data, print out to Serial1
  while (Serial.available()) {
    Serial1.write(Serial.read());
  }
  delay(1);  //delay for a short time to
  // avoid unstable USB communication
}

Other Preparation Work(AT command)

In order to do this you may first need to know how to get an available site's IP address. Follow these steps to do so:

  1. Press Windows+r, type cmd, click OK.

  2. Type a site and press Enter. e.g.www.baidu.com

  3. You will get some info including the IP address. e.g. 115.239.211.112

    get_ip_address_by_ping_in_windows
    Once you are able to do this you can continue:

NOTE: Do NOT tick the checkbox hex until Step 4 which is to send 0x1A in HEX.

  1. Send AT+CIPSTART="TCP","115.239.211.112","80", and you receive OK. After a while, you should see CONNECT OK. If it failed to connect, try a more common website, or powercycle the SIM808 module to reboot
  2. Send AT+CIPSEND to get the permission to send data to the site
  3. After you get a notation >, you can send your message
  4. Send 0x1A in HEX as an end, after a while, if the site received your data, you will see SEND OK
  5. After a while, the connection with the site will break off automatically if no more data is transmitted. You can also send AT+CIPCLOSE to break the connection.

Result

DFR0355_TCP_Communication

Was this article helpful?

TOP