I've been trying to upload data to data. Sparkfun's servers using HTTP commands, but I've hit errors every time I try.
This is my code:
#include <String.h>
void setup()
{
Serial1.begin(9600); // the GPRS baud rate
Serial.begin(9600); // the GPRS baud rate
delay(500);
}
void loop()
{
if (Serial.available())
switch(Serial.read())
{
case 'h':
SubmitHttpRequest();
break;
case 'j':
http();
break;
}
if (Serial1.available())
Serial.write(Serial1.read());
}
///SubmitHttpRequest()
///this function is submit a http request
///attention:the time of delay is very important, it must be set enough
void SubmitHttpRequest()
{
Serial1.println("AT+CSQ");
delay(100);
ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
Serial1.println("AT+CGATT?");
delay(100);
ShowSerialData();
Serial1.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
ShowSerialData();
Serial1.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");//setting the APN, the second need you fill in your local apn server
delay(4000);
ShowSerialData();
// Serial1.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
//delay(2000);
ShowSerialData();
Serial1.println("AT+HTTPINIT"); //init the HTTP request
delay(4000);
ShowSerialData();
Serial1.println("AT+HTTPPARA=\"URL\",\"http://data.sparkfun.com/input/[public key]?private_key=[private key]&gas=24\"");// setting the httppara, the second parameter is the website you want to access
delay(10);
ShowSerialData();
Serial1.print(" HTTP/1.1\r\n");
delay(500);
ShowSerialData();
delay(1000);
ShowSerialData();
Serial1.println("AT+HTTPACTION=1");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
ShowSerialData();
Serial1.println("AT+HTTPREAD");// read the data from the website you access
delay(300);
ShowSerialData();
Serial1.println("");
delay(100);
}
void http()
{
Serial1.println("AT+CGATT?");
delay(300);
ShowSerialData();
Serial1.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");//setting the APN, the second need you fill in your local apn server
delay(1000);
ShowSerialData();
Serial1.println("AT+CIFSR");//get local IP adress
delay(2000);
ShowSerialData();
Serial1.println("AT+CIPSPRT=0");
delay(3000);
ShowSerialData();
Serial1.println("AT+CIPSTART=\"TCP\",\"http://data.sparkfun.com\",\"80\"");//start up the connection
delay(2000);
ShowSerialData();
Serial.println();
Serial1.println("AT+CIPSEND");//begin send data to remote server
delay(4500);
ShowSerialData();
Serial1.print("PUT /input/[public key]?private_key=[private key]&gas=4");
delay(10);
ShowSerialData();
Serial1.print(" HTTP/1.1\r\n");
delay(500);
ShowSerialData();
Serial1.println("Host: http://data.sparkfun.com\r\n");
delay(500);
ShowSerialData();
Serial1.print("Connection: keep-alive"); //working
Serial1.print("\r\n");
Serial1.print("\r\n");
Serial1.println();
delay(500);
ShowSerialData();
Serial1.println((char)26);
Serial1.write(0x1A);
delay(500);//waitting for reply, important! the time is base on the condition of internet
Serial1.println();
ShowSerialData();
Serial1.println("AT+CIPCLOSE");//close the connection
delay(100);
ShowSerialData();
Serial1.println("AT+CIPSHUT=0");
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(Serial1.available()!=0)
Serial.write(Serial1.read());
}
For the first function (httprequest) I don't get a response on the server, even though the at commands all work. The second function(http) At At+CIPSEND, I get this:
ERROR
STATE: IP STATUS
CONNECT FAIL
And I've changed the URL but no luck. I still get the same response.
I'm using an Arduino Mega and a GSM SIM900