1

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

  • Post the results of all the AT commands for one iteration of loop(). Is this an example provided by Sparkfun? Did you deliberately omit the public and private keys in you sketch? How are you powering the SIM900? – SoreDakeNoKoto Apr 8 '16 at 0:03
  • Heya. The remaining AT commands all returned OK. nothing else.The SIM is powered with 12v and 3A. The example was pieced together from these: forum.arduino.cc/index.php?topic=116867.0 seeedstudio.com/wiki/GPRS_Shield_V1.0 – Generalno1 Apr 8 '16 at 13:10
  • Use an interactive sketch instead and enter commands and HTTP requests with the serial monitor. It is possible that one of the delays wasnt long enough. I also asked if you intentionally omitted the private and public keys in your request. – SoreDakeNoKoto Apr 8 '16 at 13:19
  • Hm, alright, thanks. And Yea, I did. I use the public and private keys when I run it. – Generalno1 Apr 9 '16 at 19:15
  • This is what i mean by an interactive sketch: arduino.stackexchange.com/questions/19041/…. Try the code in the answer. – SoreDakeNoKoto Apr 9 '16 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.