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
- DFR0181 Arduino UNO x1
- TEL0097 SIM808 Expansion Shield x1
- External Power Supply x1
- SIM Card x1
Software Preparation
-
Download and install the Arduino IDE: Click to download Arduino IDE
-
Download DFRobot_SIM808 library Click to Download DFRobot_SIM808 library
Steps
-
Insert a SIM card in to the SIM slot on the SIM808 expansion shield
-
Stack the expansion shield on to an Arduino UNO
-
Connect an external power source to the Arduino
-
Set the function switch to
None -
Upload the sample code
-
Set the function switch to
Arduinoand make sure SIM808 could communicate with Arduino board -
Press the Boot power button
-
Wait for the SIM card to register the network, the Net indicator LED will slowly flash every 3 seconds
-
Open the
SIM808_TCPConnectionexample or copy the code to your project -
Download and set the function switch to
Arduino -
Open the serial terminal
-
Wait for until
Connect mbed.org successis printed in the terminal -
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?
