Usage Example for Arduino-Connect TCP and Send GET Requests

Last revision 2026/01/15

This article demonstrates the practical application of using the DFRobot_SIM808 GPS/GPRS/GSM Shield with Arduino to establish TCP connections and send GET requests, offering a detailed example for testing and understanding the system's capabilities.

Hardware Preparation

Software Preparation

Steps

  1. Insert a SIM card in to the SIM slot on the SIM808 expansion shield

  2. Stack the expansion shield on to an Arduino UNO

  3. Connect an external power source to the Arduino

  4. Set the function switch to None

  5. Upload the sample code

  6. Set the function switch to Arduino and make sure SIM808 could communicate with Arduino board

  7. Press the Boot power button

  8. Wait for the SIM card to register the network, the Net indicator LED will slowly flash every 3 seconds

  9. Open the SIM808_TCPConnection example or copy the code to your project

  10. Download and set the function switch to Arduino

  11. Open the serial terminal

  12. Wait for until Connect mbed.org success is printed in the terminal

  13. The serial terminal will print Hello world!

Sample Code


  #include <DFRobot_sim808.h>

  //make sure that the baud rate of SIM900 is 9600!
  //you can use the AT Command(AT+IPR=9600) to set it through SerialDebug

  DFRobot_SIM808 sim808(&Serial);

  char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
  char buffer[512];

  void setup(){
    //mySerial.begin(9600);
    Serial.begin(9600);

    //******** Initialize sim808 module *************
    while(!sim808.init()) {
        delay(1000);
        Serial.print("Sim808 init error\r\n");
    }
    delay(3000);

    //*********** Attempt DHCP *******************
    while(!sim808.join(F("cmnet"))) {
        Serial.println("Sim808 join network error");
        delay(2000);
    }

    //************ Successful DHCP ****************
    Serial.print("IP Address is ");
    Serial.println(sim808.getIPAddress());

    //*********** Establish a TCP connection ************
    if(!sim808.connect(TCP,"mbed.org", 80)) {
        Serial.println("Connect error");
    }else{
        Serial.println("Connect mbed.org success");
    }

    //*********** Send a GET request *****************
    Serial.println("waiting to fetch...");
    sim808.send(http_cmd, sizeof(http_cmd)-1);
    while (true) {
        int ret = sim808.recv(buffer, sizeof(buffer)-1);
        if (ret <= 0){
            Serial.println("fetch over...");
            break;
        }
        buffer[ret] = '\0';
        Serial.print("Recv: ");
        Serial.print(ret);
        Serial.print(" bytes: ");
        Serial.println(buffer);
        break;
    }

    //************* Close TCP or UDP connections **********
    sim808.close();

    //*** Disconnect wireless connection, Close Moving Scene *******
    sim808.disconnect();
  }

  void loop(){

  }

Was this article helpful?

TOP