[SM5100] Problem receiving HTML pages

I have an Arduino Mega (2560) board connected to a Sparkfun GPRS shield http://www.sparkfun.com/products/9607 and tried to request HTML pages.

I’m pretty sure the HTTP request is correct (I receive answers from the server) however, the page isn’t usually correctly received. I managed to observe three different kinds of behavior:

  • page correctly received (normally pages with very small pieces of HTML code);

  • page partially received (mainly pages with an huge HTML content eg, google page);

  • pages which I get an “+CME ERROR 21” response eg, checkip.dyndns.org .

Since I’m trying to solve this problem for a few days, can anyone check the attached piece of code and give me any hints to receive correctly any web page?

Thanks in advance.

#define BAUD_GPRS 9600
#define BAUD_TERMINAL 115200
#define CMD_SIZE 50
#define SERIAL_DELAY 30
#define DELAY_LONG 40000
#define DELAY_SHORT 6500
#define DELAY_INIT 10000
#define DELAY_SEND 100
#define USER_AGENT "Mozilla/5.0"
#define SEND_STRING_SIZE 300

/* AT commands definition */
#define AT_GPRS_REBOOT "AT+CFUN=1,1" /* p.23 */
#define AT_GPRS_PDP_CONTEXT_PARAMS "AT+CGDCONT=1,\"IP\",\"internet\"" /* p.90 */
#define AT_GPRS_PDP_USER_PASS "AT+CGPCO=0,\"internet\",\"internet\", 1" /* p.95 */
#define AT_GPRS_PDP_ACTIVATION "AT+CGACT=1,1" /* p.96 */

/* included libraries */
#include <string.h>

char incoming_char = 0, cmd[CMD_SIZE], dump[CMD_SIZE];
char incoming_str[CMD_SIZE];
char ip[20];

char send_string[SEND_STRING_SIZE];


void setup()
{
  memset(send_string, '\0', SEND_STRING_SIZE);
  Serial.begin(BAUD_TERMINAL);
  Serial.println("Starting");
  
  Serial1.begin(BAUD_GPRS);
  delay(DELAY_INIT);
  
  char_rx_from_GPRS(0, DELAY_SHORT);
  
  
  create_PDP_context();
  
  Serial.println("Ready to receive commands!"); 
  
}

void loop()
{  
  int i=0;
  
  if(Serial.available() >0)
  {
    delay(100);

    while (Serial.available() != 0)
    {
      cmd[i] = Serial.read();
      i++;
    }
    cmd[i] = '\0';
    i = 0;
  
    if (strcmp (cmd, "!gp") == 0)
    {
      Serial.println("Receiving a page.");
      
      Serial.println("Configuring the remote host and port, to open a TCP connection");
      Serial1.println("AT+SDATACONF=1,\"TCP\",\"checkip.dyndns.org\",80"); /* p.107*/
      char_rx_from_GPRS(0,DELAY_SHORT);
      Serial1.println("AT+SDATARXMD=1,1,1"); /* ASCII format p.111 */
      Serial1.println("AT+TRT=12"); /* number of retries p.113 */
      char_rx_from_GPRS(0,DELAY_SHORT);
      
      memset(send_string, '\0', SEND_STRING_SIZE);
      strcpy(send_string, "GET /");
      strcat(send_string, " HTTP/1.1\r\n");
      strcat(send_string, "HOST: ");
      strcat(send_string, "checkip.dyndns.org\r\n");
      strcat(send_string, "Connection: Keep-Alive\r\n");
      strcat(send_string, "User-Agent: ");
      strcat(send_string, USER_AGENT);
      strcat(send_string, "\r\n");
      strcat(send_string, "\r\n");
      
      Serial.println("Starting TCP/UDP connection...");
      Serial.println(send_string);
      Serial1.println("AT+SDATASTART=1,1"); /* enabling GPRS service p.108 */
      char_rx_from_GPRS(0, 1000); 
      
      delay (1000);
      char_rx_from_GPRS(0,100);
      
      Serial1.print("AT+SDATATSEND=1,");
      Serial1.println(strlen(send_string));
      
      delay(1000);
      while(Serial1.available() > 0)
        Serial.print( (char) Serial1.read() );
      Serial.println("");
      
      for(i=0; i < strlen(send_string); i++)
        Serial1.print( (byte) send_string[i]);
  
      Serial1.print(26, BYTE);
      
      delay(1000);
      int j = 0;
      do
      {
        Serial1.println("AT+SDATAREAD=1");
        delay(1000);  
        char_rx_from_GPRS(0, 10);
        j++;
      } while (j < 100);
      
      
     
      char_rx_from_GPRS(1,100);
      
      Serial.println("Ready to receive commands!");
      memset(cmd, '\0', CMD_SIZE);
      
    }
  
  
    else
    {
      Serial.println("Unknown command!");
      memset(cmd, '\0', CMD_SIZE);
    }
  }
}

void char_rx_from_GPRS(int mode, int delay_time)
{
  memset(incoming_str, '\0', CMD_SIZE);
  int i=0;
  bool received = true;
  while(true)
  {
    if(Serial1.available() > 0 && mode == 0) /* data to be received */
    {
      incoming_char = Serial1.read();
      Serial.print(incoming_char);
      received = true;
      incoming_str[i] = incoming_char;
      i++;
    }
    else if(Serial1.available() > 0 && mode == 1) /* data to be received */
    {
      incoming_char = Serial1.read();
      Serial.print('\0');
      received = true;
      incoming_str[i] = incoming_char;
      i++;
    }
    
    
    else if(!received)
      return;
    else
    {
      received = false;
      delay(delay_time);
    }
  }
}

void create_PDP_context()
{
  
  Serial.println("Creating PDP context...");
  Serial1.println(AT_GPRS_PDP_CONTEXT_PARAMS);
  delay(1000);
  char_rx_from_GPRS(0,DELAY_SHORT);
  Serial1.println(AT_GPRS_PDP_USER_PASS);
  delay(1000);
  char_rx_from_GPRS(0,DELAY_SHORT);
  Serial1.println(AT_GPRS_PDP_ACTIVATION);
  delay(DELAY_SHORT);
  char_rx_from_GPRS(0,DELAY_SHORT);
  Serial.println("Done!"); 
}

9600 baud is too slow to read large pages. you’d need to up the baud rate to read data before its overwritten in the 5100s rx buffer. Change to 115200.