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
- making the connection
- 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();
}