Make HTTP requests from SIM300 GSM module
SIM300 is a GSM module by Simcom.
And believe me it’s no less than a piece of shit. I was unlucky enough to have my hands on it. I spent 3-4 days figuring out and testing the correct sequence of AT commands that must be used to make HTTP request via this module. In the following post I’m going to explain how to use it with the Arduino to send and receive data to a web server over HTTP.
Let’s start with the code :
Fist we need to create an object of the SoftwareSerial class. Let’s call it GPRS. The object must be created at the start of the program. The code for this is :
1 2 3 4 5 6 7 8 |
#include <softwareSerial.h>; SoftwareSerial GPRS(2, 3); void setup() { GPRS.begin(9600); Serial.begin(9600); } |
The SoftwareSerial library ships as a default library with the Arduino IDE. Here, 2 and 3 are the pin numbers on the Arduino board where we need to connect our GSM module. Here pin 2 is being used as the Rx pin and pin 3 is being used as the Tx pin. Rx of Arduino should be connected with the Tx of the module and Tx of Arduino with the Rx of the GSM module. The line GPRS.begin(9600)
initialises the serial communication with the module with setting a baud rate of 9600. This is the default baud rate for SIM300. The line Serial.begin(9600)
initialises the Arduino’s default hardware Serial communication. The Rx and Tx of this default Serial are at pins 0 and 1 of the Arduino. The output that you see in the Serial monitor is also via this default hardware Serial communication.
Upto this point we have done our initialisation part.
Let’s setup our HTTP request with the following code. I’ve provided comments at each line to tell what it does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
GPRS.write("AT+CGATT=1"); //Attach a GPRS Service GPRS.write("\n\r"); GPRS.write("AT+CGDCONT=1,\"IP\",\"airtelgprs.com\""); //Define PDP Context GPRS.write("\n\r"); GPRS.write("AT+CDNSCFG=\"208.67.222.222\",\"208.67.220.220\""); //Configure Domain Name Server. You can use Google public DNS as well GPRS.write("\n\r"); GPRS.write("AT+CSTT=\"airtelgprs.com\",\"\",\"\""); //Set Access point, User ID, and password GPRS.write("\n\r"); GPRS.write("AT+CIICR"); //Bring up wireless connection with GPRS P.S. Time consuming GPRS.write("\n\r"); delay(1000); GPRS.write("AT+CIFSR"); // Get Local IP address. No actually needed though. GPRS.write("\n\r"); GPRS.write("AT+CIPSTATUS"); // Get Connection Status P.S. It should be 'IP STATUS'. // This can be used as a check point. GPRS.write("\n\r"); GPRS.write("AT+CIPHEAD=1"); // Add headers to the HTTP request. GPRS.write("\n\r"); GPRS.write("AT+CDNSORIP=1"); //Indicates whether connection request will be using IP address (0), or domain name (1) GPRS.write("\n\r"); GPRS.write("AT+CIPSTART=\"TCP\",\"www.omerjerk.in\",\"80\""); //Start up TCP connection (mode, IP address/name, port) P.S. if returns 'CONNECT OK' then you're lucky GPRS.write("\n\r"); |
Upto this point we’ve initialized our connection to the web server. The following code makes the actual HTTP request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
GPRS.write("AT+CIPSEND"); //Telling the GSM module that we're going to send the data GPRS.write("\n\r"); GPRS.write("GET /yourphpfile.php?key=value HTTP/1.1"); //And finally here comes the actual HTTP request //The following are the headers that must be set. GPRS.write("\n"); GPRS.write("Host: www.omerjerk.in"); GPRS.write("\n"); GPRS.write("Connection: keep-alive"); GPRS.write("\n"); GPRS.write("Accept: */"); GPRS.write("*"); GPRS.write("\n"); GPRS.write("Accept-Language: en-us"); GPRS.write("\n\r"); GPRS.write("\n"); GPRS.write(ctrlZ); //It tells the GSM module that we're not going to send data anymore // char ctrlZ = 0x1A; GPRS.write("\n\r"); |
In the above post you’ll have to change the domain name and put the domain name of your server. Update the line yourphpfile.php?key=value
as per your needs. You can add extra parameters if you want.
You can browse the whole code on my Github : https://github.com/omerjerk/Netra/blob/master/sketch_apr19a.ino
And at last I would say if you’ve a choice then better go for SIM900 GSM Module. SIM900 is way better and more reliable than SIM300.
4 thoughts on “Make HTTP requests from SIM300 GSM module”
M using tera term to connect to google.com, can u tell me about correct sequence of AT commands
@Haris
If you’re using the same GSM module, then just use the commands mentioned in the code and in the same sequence.
Revert to me at which command your implementation is going wrong.
Thanks.
#include
SoftwareSerial mySerial(5,6);
int val;
int tempPin = 1;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
delay(2000);
}
void loop()
{
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float cel = mv/10;
float farh = (cel*9)/5 + 32;
delay(100);
mySerial.println(“AT”);
// set the SMS mode to text
delay(1000);
mySerial.println(“AT+CGATT=1”);//
delay(1000);
mySerial.print(“AT+CSTT=”);
mySerial.write(34);
mySerial.print(“airtelgprs.com”);
mySerial.write(34);
mySerial.write(13);
mySerial.write(10);
delay(1000);
mySerial.println(“AT+CIICR”);
delay(4000);
mySerial.print(“AT+CIPSTART=”);
mySerial.write(34);
mySerial.print(“TCP”);
mySerial.write(34);
mySerial.print(“,”);
mySerial.write(34);
mySerial.print(“117.203.55.209”);
mySerial.write(34);
mySerial.print(“,”);
mySerial.write(34);
mySerial.print(“80”);
mySerial.write(34);
mySerial.write(13);
mySerial.write(10);
delay(3000);
mySerial.println(“AT+CIPSEND”);
delay(1000);
mySerial.print(“get /receive.php?value0=”); //9876543210987″);
//mySerial.print(“TEMPRATURE = “);
mySerial.print(cel);
// Serial.print(“*C”);
// Serial.println();
mySerial.write(13);
mySerial.write(10);
mySerial.write(26);
while(1);
}
Hello there,
Im doing a project which is like it uses the temparature sensor to sense temparature and send data(temparature) and the data need to be sent to an online storage unit like a wamp server where in we could access the data. The whole process is to be done using arduino and along with gsm module sim 300!
The sim i’m using is idea and the above code i have mentioned has a line like “”airtelgprs.com””. Do i need to change it to idea settings and what that might be ?
Could you help me out if possible!!
Thanks in advance.
airtelgprs.com is the name of access point in case of Airtel.
You need to change that to the access point provided by your career.
A quick Google search reveals that, in case of Idea, it should be set to “internet”.