TCP/IP with SM5100B GSM/GPRS module

More fun with this board http://www.sparkfun.com/commerce/product_info.php?products_id=9607.

Following these app notes :
http://www.sparkfun.com/datasheets/CellularShield/SM5100B%20TCPIP%20App%20Note.pdf, I am trying to connect to a server with this module. But I'm not having much (any!) luck with it.

What I have below (apologies for the length, but it is the relevant parts) should open a socket and connect to google.com.

However, it never gets to the connecting a socket part.

What I see in the serial monitor is this :

"Booting up...
GPRS Registered
GPRS AT Ready
AT+CGATT=?

AT+CGDCONT=1,'IP','telenor'

AT+CGPCO=0,'dj','dj',1

AT+CGACT=1,1

at+sdataconf=1,"TCP","google.com",80

at+sdatastart=1,1

at+sdatastatus=1
Socket Status to Follow"

So it seems to not be

  1. making the connection
  2. not printing out what it receives

Any ideas?

#include <NewSoftSerial.h>  //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h>         //Used for string manipulations

int sockConnect = 0; //checks to see if the sockets is connected
int dataIn = 0; //is there data in the buffer?
char incoming_char=0;      //Will hold the incoming character from the Serial Port.
String statusRegistered="\r\n+SIND: 4";

NewSoftSerial cell(2,3);  //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

void gprsinit(){
  cell.print("AT+CGATT=?");
  Serial.println("AT+CGATT=?");
  cell.print(0x0D); 
  waitForOK();
  cell.print("AT+CGDCONT=1,'IP','telenor'");
  Serial.println("AT+CGDCONT=1,'IP','telenor'");
  cell.print(0x0D); 
  waitForOK();
  cell.print("AT+CGPCO=0,'dj','dj',1");
  Serial.println("AT+CGPCO=0,'dj','dj',1");
  cell.print(0x0D); 
  waitForOK();
  cell.print("AT+CGACT=1,1");
  Serial.println("AT+CGACT=1,1");
  cell.print(0x0D);  
  waitForOK();
}

void datasendandreceive(){
  cell.print("at+sdataconf=1,\"TCP\",\"www.google.com\",80"); 
  Serial.println("at+sdataconf=1,\"TCP\",\"www.google.com\",80"); 
  cell.print(0x0D);  
  waitForOK();
  cell.print("at+sdatastart=1,1");
  Serial.println("at+sdatastart=1,1");
  cell.print(0x0D); 
  waitForOK();
  cell.print("at+sdatastatus=1"); 
  Serial.println("at+sdatastatus=1"); 
  cell.print(0x0D);  
  Serial.println("Socket Status to Follow");
  waitForSockStatus();
  cell.print("at+sdatasend=1,10");  // where 10 is how many bytes to send
  cell.print("1");
  cell.print(0x1A);
  waitForOK();
  //check to see if data has been received +STCPD:1
  hasDataBeenReceived();
  if(dataIn==1){
    cell.print("AT+SDATATREAD=1");  // read the data
    cell.print(0x0D);  
    getMessage();
  }
  else{
    //start over
  }

  cell.print("AT+SDATASTART=1,0");  // close the connection 
  waitForOK();
}

//reads through the buffer until it reaches the letter 'K'
void waitForOK(){
  while(cell.available())
  {
    incoming_char=cell.read();    //Get the character from the cellular serial port.
    Serial.print(incoming_char);
    delay(2);
  }
  Serial.println();
}


//waits to connect to the socket
void waitForSockStatus(){
  //Patterns to look for
  String prefix="+SOCKSTATUS: 1,";
  String input="";
  char value;

  while(cell.available()<1){
    //wait for a cycle
  }

  while(cell.available()>0){
    incoming_char=cell.read();  //read the buffer
    Serial.println(incoming_char);
    input+=incoming_char;      //add it to the string
    if(input.substring(0)==prefix)  //if it matches our expected string
    {
      break;
    }
  }

  incoming_char=cell.read();
  //Serial.println(incoming_char);
  if(incoming_char=='1'){
    Serial.println("Socket connected");
    sockConnect=1;
  }
  else{
    Serial.println("Socket NOT connected");
    sockConnect=0;
  }
  //clear the buffer
  while(cell.available()>0){
    incoming_char=cell.read(); 
  }
}

void getMessage(){
  //Patterns
  String prefix="+SDATA: 1, ";
  String input="";  
  String ourM="\r\n";
  boolean y=false;
  boolean m=false;

  //print the message
  cell.print('AT+SDATATREAD=1');
  cell.print(0x0D); 

  while(cell.available()<1){
    //wait for a cycle
  }

  //First, find the prefix
  while(cell.available()>0){
    incoming_char=cell.read();  //read the buffer
    input+=incoming_char;      //add it to the string
    if(input.substring(0)==prefix)  //if it matches our expected string
    {
      //Data is present
      y=true;
      break;
    }
  }

  //read message until we reach the comma, which is the start of the new message
  while(cell.available()&&y==true) 
  {
    if(incoming_char!=','){
      incoming_char=cell.read();
    }
    else{
      break;
    }
  } 

  //get our message
  while(cell.available()>0){  
    incoming_char = cell.read(); //read the buffer
    messageToSign.concat(incoming_char); //add it to the string 
    if(messageToSign.endsWith(ourM))
    {     
      break;
    }
  }
}  

void hasDataBeenReceived(){
  //Pattern
  String hopeItsThere= "+STCPD:1"; 
  String input="";  

  while(cell.available()<1){
    //wait for a cycle
  }

  while(cell.available()>0){
    incoming_char=cell.read();  //read the buffer
    input+=incoming_char;      //add it to the string
    if(input.substring(0)==hopeItsThere)  //if it matches our expected string
    {
      //Data is present
      dataIn=1;
    } 
    else{
      //no data
      dataIn=0;
    }
  }
}


void setup()
{
  //Initialize serial ports for communication.
  Serial.begin(9600);
  cell.begin(9600);
  delay(1000);
  //Let's get started!
  Serial.println("Booting up...");
}

void loop() {
  //typically there is code here that make sure we have connected to the network 
  gprsinit();
  datasendandreceive();
}

Bump, plus more refined code :

Now I'm seeing this :
Starting SM5100B Communication...
GPRS Network Not Available
GPRS AT Ready
Setting up PDP Context

OK
Sending null password
Activating PDP Context
Configuring TCP connection to TCP Server

OK
Checking Connection Status

+SOCKSTATUS: 1,0,0000,0,0,0

I should be seeing an "OK" after the "Activating PDP Context" Even when entering manually I'm getting no love. Any ideas why?

#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h>

#define BUFFSIZ 90

char at_buffer[BUFFSIZ];
char buffidx;
int firstTimeInLoop = 1;
int GPRS_registered;
int GPRS_AT_ready;
char incoming_char=0; //Will hold the incoming character from the Serial Port.
char buffer[60];
String myString="1";

NewSoftSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

// Do system wide initialization here in this function
void setup()
{
Serial.begin(9600);
cell.begin(9600);
//Let's get started!

Serial.println("Starting SM5100B Communication...");
delay(5000);
/* Currently GPRS is not registered and AT is not ready */
GPRS_registered = 0;
GPRS_AT_ready = 0;

}

/* Reads AT String from the SM5100B GSM/GPRS Module */

void loop() {

/* If called for the first time, loop until GPRS and AT is ready */

if(firstTimeInLoop) {
FunctionToCheckWeAreConnectedToNetwork();
}

delay(1500);

Serial.println("Setting up PDP Context");
cell.println("AT+CGDCONT=1,"IP","epc.tmobile.com"");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}

Serial.println("Sending null password");
// cell.println("AT+CGPCO=0,"none","none", 1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}

Serial.println("Activating PDP Context");
cell.println("AT+CGACT=1,1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}

Serial.println("Configuring TCP connection to TCP Server");
cell.println("AT+SDATACONF=1,"TCP","88.87.58.126",5000");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}

}

Serial.println("Checking Connection Status\n");
cell.println("AT+SDATASTATUS=1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}

}

I think that you can have a timing problem....
Try this with your company data:

//*************
// Inicia conexion GPRS y conecta con la web deseada
//*************
void GPRS_init()
{
// Llamar y conectarse a internet
cell.println("AT+CGATT?"); // Activar GPRS 1= si => AT+CGATT = 0 desactiva
EsperamosOK();

cell.println("AT+CGDCONT=1, "IP", "internet ""); // Setup PDP amena
EsperamosOK("Direccion: ");

cell.println("AT+CGPCO=0, "CLIENTE ", "AMENA", 1"); // Parametros PDP amena
EsperamosOK("Clave: ");

// Activamos conexion GPRS
cell.println("AT+CGACT=1,1"); // Activar GPRS
EsperamosOK("CGACT: ");

// Buscar una direccion en internet
cell.println("AT+SDATACONF=1, "TCP", "www.google.es", 80"); // Host y puerto
EsperamosOK("TCP: ");

// Iniciar comunicacion
cell.println("AT+SDATASTART=1,1"); // Iniciar comunicacion TCP/UDP
EsperamosOK("SDATASTART: ");

// Estado comunicaciones
cell.println("AT+SDATASTATUS=1"); // Consulta estado, hacer varias veces esta consulta.
EsperamosOK("SDATASTATUS: ");
// +SOCKSTATUS: 1,0,0104,0,0,0 (0 means socket not connected, 0104 means socket is connecting)
// +SOCKSTATUS: 1,1,0102,0,0,0 (1 means socket connected)

}

//*****************
// Esperar respuesta
//*****************
void EsperamosOK(const char* enviado){
unsigned long espera= millis();
Serial.println();
Serial.print(enviado);
while(cell.available()<1 && (millis()-espera) < 10000){} // Esperamos

while(cell.available()>0) {
incoming_char=cell.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char);
delay(2);
}

}

And you must recive:

+SIND: 1

+SIND: 10,"SM",1,"FD",1,"LD",1,"MC

+SIND: 11

+SIND: 3

+SIND: 4

+CGATT: 1

OK

Direccion:
OK

Clave:
OK

CGACT:
OK

TCP:
OK

SDATASTART:
OK

SDATASTATUS:
+SOCKSTATUS: 1,1,0102,0,0,0 // CONNECTED

OK

bye,
Pablo

I managed to get it connecting to a socket on my server. Once I clean up the code, I'll post it.

Appreciate the help.