Hi !
I recently bought an Arduino Shield SM5100B.
I’ve been programing a simple code to open an URL and then read the information but I found an inestable response of the shield.
I download examples to work with and adapt to my telephone company and WEB page to read.
Some times the module doesn’t gets the GPRS working. Some times doesn’t get respons. Some times it restars the module.
I worked with arduino 1.0.3 and arduino 1.0.4 with the same instability !
With this code :
#include <SoftwareSerial.h>
SoftwareSerial cell(2,3); // We need to create a serial port on D2/D3 to talk to the GSM module
const boolean DEBUG = true;
const String ip = "2.138.140.67"; // IP address of oscarjofre.no-ip.info
const String host = "oscarjofre.no-ip.info"; // required in HTTP 1.1 - what's the name of the host at this IP address?
const String request = "GET /arduino/arduino_sensor.php?type=Celsius&value=29 HTTP/1.1";
//const String request = "GET /arduino/arduino_sensor.php HTTP/1.1";
const String useragent = "Mozilla/5.0"; // for our purposes the user agent doesn't matter - if I understand correctly it's helpful to use something generic the server will recognize
const String apn = "telefonica.es"; // access-point name for GPRS
String s;
/*****************************************
* Buffers
*/
const int BUFFSIZE = 180;
char at_buffer[BUFFSIZE];
//char buffidx;
char incoming_char = 0;
char buffer[BUFFSIZE];
/*****************************************
* Registers
*/
boolean firstTimeInLoop = true;
boolean GPRS_registered = false;
boolean GPRS_AT_ready = false;
boolean continueLoop = true;
boolean bIsConected = false;
boolean GPRS_Error = false;
/*********************************************************
* GPRS Functions
*********************************************************/
/**
* store the serial string in a buffer until we receive a newline
*/
static int readATString(boolean watchTimeout = false) {
char c;
int buffidx = 0;
int time = millis();
while (1) {
int newTime = millis();
if (watchTimeout) {
// Time out if we never receive a response
if (newTime - time > 30000) { GPRS_Error=true; return -1; }//error(ERROR_GSM_FAIL);
}
if (cell.available() > 0) {
c = cell.read();
//Serial.print("-");Serial.print(c);
if (c == -1) {
at_buffer[buffidx] = '\0';
return 0;
}
if (c == '\n') {
continue;
}
if ((buffidx == BUFFSIZE - 1) || c == '\r') {
at_buffer[buffidx] = '\0';
return 0;
}
at_buffer[buffidx++] = c;
}
}
}
/**
* Send an AT command to the cell interface
*/
static int sendATCommand(const char* ATCom) {
cell.println(ATCom);
Serial.print("COMMAND: ");
Serial.println(ATCom);
while (continueLoop) {
if (readATString(true)==-1) return -1;
ProcessATString();
//Serial.print("RESPONSE: ");
//Serial.println(at_buffer);
}
continueLoop = true;
delay(500);
}
/*
* Handle response codes
*/
static void ProcessATString() {
Serial.print("RESPONSE: ");
Serial.println(at_buffer);
if (strstr(at_buffer, "+SIND: 1" ) != 0) {
firstTimeInLoop = true;
GPRS_registered = false;
GPRS_AT_ready = false;
// GPS unit pulls enough current to keep the GSM shield from starting
// So we wait until it's initialized and then power it via a digital pin
//digitalWrite(GPS_RELAY, HIGH);
}
if (strstr(at_buffer, "+SIND: 10,\"SM\",0,\"FD\",0,\"LD\",0,\"MC\",0,\"RC\",0,\"ME\",0") != 0
|| strstr(at_buffer, "+SIND: 0") != 0) {
Serial.println("ERROR SIM invalid");
GPRS_Error=true;//error(ERROR_SIM_UNAVAIL);
}
if (strstr(at_buffer, "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1") != 0) {
Serial.println("SIM card Detected");
//successLED();
}
if (strstr(at_buffer, "+SIND: 7") != 0) {
Serial.println("ERROR NETWORK FATAL");
GPRS_Error=true;//error(ERROR_NETWORK_FAIL);
}
if (strstr(at_buffer, "+SIND: 8") != 0) {
GPRS_registered = false;
Serial.println("GPRS FAIL");
GPRS_Error=true;//error(ERROR_NETWORK_FAIL) //error(ERROR_GPRS_FAIL);
}
if (strstr(at_buffer, "+SIND: 11") != 0) {
GPRS_registered = true;
Serial.println("GPRS Registered");
//successLED();
}
if (strstr(at_buffer, "+SIND: 4") != 0) {
GPRS_AT_ready = true;
Serial.println("GPRS AT Ready");
//successLED();
}
if (strstr(at_buffer, "+SOCKSTATUS: 1,0") != 0) {
//GPRS_Error=true;
//error(ERROR_HOST);
continueLoop = false;
}
if (strstr(at_buffer, "+CME ERROR: 29") != 0) {
GPRS_Error=true;
continueLoop = false;
return;
}
if (strstr(at_buffer, "+CME ERROR") != 0) {
GPRS_Error=true;//error(ERROR_GPRS_UNKNOWN);
}
if (strstr(at_buffer, "OK") != 0 || strstr(at_buffer, "NO CARRIER") != 0) {
continueLoop = false;
//successLED();
}
if (strstr(at_buffer, "+SOCKSTATUS: 1,1") != 0) {
continueLoop = false;
}
}
static bool establishNetwork() {
while ((GPRS_registered == false || GPRS_AT_ready == false) && GPRS_Error==false) {
readATString(true);
ProcessATString();
}
return !GPRS_Error;
}
// keep reading characters until we get char c
void waitFor(char c) {
int time = millis();
char r;
while(1) {
if(cell.available()>0) {
r=cell.read();
//Serial.print("c=(");Serial.print(c);Serial.print(") - "); Serial.print("r=(");Serial.print(r);Serial.println(")");
if (r == c) {
delay(100);
return;
}
// Time out if we never receive a response
}
int newTime = millis();
if (newTime - time > 30000) { GPRS_Error=true; return ; }//error(ERROR_GSM_FAIL);
}
}
// for eating a single message we expect from the module
// prints out the next message from the module. if it's not the expected value, die
static int waitFor(const char* s,unsigned int timeout=1000) {
int iReturn;
Serial.println("RESPONSE WaitFor wiring");
readATString(true);
//Serial.print("RESPONSE WaitFor (");
//Serial.print(s);
//Serial.print(") - (");
//Serial.print(at_buffer);
//Serial.println(")");
if (strstr(at_buffer, s) != 0) {
iReturn=0;
}else{
iReturn=-1;
}
delay(100); // wait for a tiny bit before sending the next command
return iReturn;
}
// keep spitting out messages from the module til we get the one we expect
static int waitTil(const char* s) {
int iSeguir=0;
while (iSeguir!=-1) {
iSeguir=readATString(true);
if (strstr(at_buffer, s) != 0){
delay(1000); // cause we're probably about to send another command
return 0;
}else{
Serial.print("RESPONSE waiting("+String(s)+"): (");
Serial.print(at_buffer);
Serial.println(") iSeguir=(" +String(iSeguir) + ")");
delay(1000);
}
}
return -1;
}
static int sendData(const char* data) {
Serial.println("Attaching GPRS...");
sendATCommand("AT+CGATT=1"); if (GPRS_Error) return -1;
delay(1000);
Serial.println("Setting up PDP Context");
sendATCommand("AT+CGDCONT=1,\"IP\",\"telefonica.es\""); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Activate PDP Context");
sendATCommand("AT+CGACT=1,1"); if (GPRS_Error) return -1;
delay(2000);
// Change 0.0.0.0 to reflect the server you want to connect to
Serial.println("Configuring TCP connection to server");
sendATCommand("AT+SDATACONF=1,\"TCP\",\"2.138.140.67\",80");
delay(2000);
//Serial.println("Configure APN");
//sendATCommand("AT+CGPCO=0,\"\",\"\", 1"); if (GPRS_Error) return -1;
//delay(2000);
/*Serial.println("Socket 1, ASCII 1, TPC 1"); // ASCII format p.111
sendATCommand("AT+SDATARXMD=1,1,1"); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Set Reintents 12"); //number of retries p.113
sendATCommand("AT+TRT=12"); if (GPRS_Error) return -1;
delay(2000);*/
Serial.println("Starting TCP Connection");
sendATCommand("AT+SDATASTART=1,1"); if (GPRS_Error) return -1;
delay(7000);
Serial.println("Getting status ok=+SOCKSTATUS: 1,1,0102,0,0,0");
sendATCommand("AT+SDATASTATUS=1"); if (GPRS_Error) return -1;
delay(4000);
while (strstr(at_buffer, "+SOCKSTATUS: 1,1,0102,0,0,0") == 0)
{
waitTil("OK"); //OK
delay(1000);
Serial.println("Socket NOT connected");
sendATCommand("AT+SDATASTATUS=1"); if (GPRS_Error) return -1;
delay(4000);
}
waitTil("OK"); //OK //OK
delay(2000);
Serial.println("Socket connected");
Serial.println("Sending data");
// we're now connected and can send HTTP packets!
int packetLength = 26+host.length()+request.length()+useragent.length(); // 26 is size of the non-variable parts of the packet, see SIZE comments below
Serial.println("Sending HTTP packet...");
cell.print("AT+SDATATSEND=1,"+String(packetLength)+"\r");
waitFor('>'); // wait for GSM module to tell us it's ready to recieve the packet
cell.print(request+"\r\n"); // SIZE: 2
cell.print("Host: "+host+"\r\n"); // SIZE: 8
cell.print("User-Agent: "+useragent+"\r\n\r\n"); // SIZE: 16
cell.write(26); // ctrl+z character: send the packet
waitFor("OK");
delay(2000);
Serial.println("Wait Til OK...");
waitTil("OK");
Serial.print("RESPONSE: ");
Serial.println(at_buffer);
if (GPRS_Error) return -1;
Serial.println("Getting status");
sendATCommand("AT+SDATASTATUS=1"); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Getting data 1");
sendATCommand("AT+SDATAREAD=1"); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Getting data 2");
sendATCommand("AT+SDATAREAD=1"); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Wait Til +SDATA...");
waitTil("SDATA"); if (GPRS_Error) return -1;
Serial.println("Close connection");
sendATCommand("AT+SDATASTART=1,0"); if (GPRS_Error) return -1;
delay(2000);
Serial.println("Disable PDP Context");
sendATCommand("AT+CGACT=0,1"); if (GPRS_Error) return -1;
delay(2000);
// Clear string and flash LED
// myStr.begin();
//successLED();
return 0;
}
void setup()
{
Serial.begin(9600);
Serial.println("Arduino GPRS ++ Inicialitzant");
cell.begin(9600);
}
void loop()
{
Serial.println("Inici loop ...");
if (!bIsConected) {
Serial.println("Establint connexio ...");
bIsConected=establishNetwork();
}
if (bIsConected && !GPRS_Error) {
Serial.println("Inici enviament dades ...");
// Send data to cell network
sendData("");
}
if (GPRS_Error) {
Serial.println("Loop... Fi enviar data ERROR REINICIAR...");
GPRS_registered = false;
GPRS_AT_ready = false;
continueLoop = true;
bIsConected = false;
GPRS_Error = false;
//Reiniciar Modul
Serial.println("COMMAND: AT+CFUN=1,1");
delay(2000);
cell.println("AT+CFUN=1,1");
waitTil("OK");
Serial.print("RESPONSE AT+CFUN: ");Serial.println(at_buffer);
delay(2000);
//ProcessATString();
}else{
Serial.println("Loop... Fi enviar dades TOT OK...");
delay(5000);
}
}
i have this test result in the same place with 2 minuts delay:
TEST 1
Arduino GPRS ++ Inicialitzant
Inici loop ...
Establint connexio ...
RESPONSE:
RESPONSE: +SIND: 1
RESPONSE:
RESPONSE: +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
SIM card Detected
RESPONSE:
RESPONSE: +SIND: 11
GPRS Registered
RESPONSE:
RESPONSE: +SIND: 3
RESPONSE:
RESPONSE: +SIND: 4
GPRS AT Ready
Inici enviament dades ...
Attaching GPRS...
COMMAND: AT+CGATT=1
RESPONSE:
RESPONSE: OK
Setting up PDP Context
COMMAND: AT+CGDCONT=1,"IP","telefonica.es"
RESPONSE:
RESPONSE: OK
Activate PDP Context
COMMAND: AT+CGACT=1,1
RESPONSE:
RESPONSE: OK
Configuring TCP connection to server
COMMAND: AT+SDATACONF=1,"TCP","2.138.140.67",80
RESPONSE:
RESPONSE: OK
Starting TCP Connection
COMMAND: AT+SDATASTART=1,1
RESPONSE:
RESPONSE: OK
Getting status ok=+SOCKSTATUS: 1,1,0102,0,0,0
COMMAND: AT+SDATASTATUS=1
RESPONSE:
RESPONSE: +SOCKSTATUS: 1,1,0102,0,0,0
Arduino GPRS ++ Inicialitzant
Inici loop ...
Establint connexio ...
TEST 2
Arduino GPRS ++ Inicialitzant
Inici loop ...
Establint connexio ...
RESPONSE:
RESPONSE: +SIND: 1
RESPONSE:
RESPONSE: +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
SIM card Detected
RESPONSE:
RESPONSE: +SIND: 11
GPRS Registered
RESPONSE:
RESPONSE: +SIND: 3
RESPONSE:
RESPONSE: +SIND: 4
GPRS AT Ready
Inici enviament dades ...
Attaching GPRS...
COMMAND: AT+CGATT=1
RESPONSE:
RESPONSE: OK
Setting up PDP Context
COMMAND: AT+CGDCONT=1,"IP","telefonica.es"
RESPONSE:
RESPONSE: OK
Activate PDP Context
COMMAND: AT+CGACT=1,1
RESPONSE:
RESPONSE: OK
Configuring TCP connection to server
COMMAND: AT+SDATACONF=1,"TCP","2.138.140.67",80
RESPONSE:
RESPONSE: +SIND: 1
RESPONSE:
RESPONSE: +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
SIM card Detected
TEST3
Arduino GPRS ++ Inicialitzant
Inici loop ...
Establint connexio ...
RESPONSE:
RESPONSE: +SIND: 1
RESPONSE:
RESPONSE: +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
SIM card Detected
RESPONSE:
RESPONSE: +SIND: 11
GPRS Registered
RESPONSE:
RESPONSE: +SIND: 1
RESPONSE:
RESPONSE: +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
SIM card Detected
RESPONSE:
RESPONSE: +SIND: 11
GPRS Registered
RESPONSE:
RESPONSE: +SIND: 3
RESPONSE:
RESPONSE: +SIND: 4
GPRS AT Ready
Inici enviament dades ...
Attaching GPRS...
COMMAND: AT+CGATT=1
RESPONSE:
RESPONSE: OK
Setting up PDP Context
COMMAND: AT+CGDCONT=1,"IP","telefonica.es"
RESPONSE:
RESPONSE: OK
Activate PDP Context
COMMAND: AT+CGACT=1,1
RESPONSE:
RESPONSE: OK
Configuring TCP connection to server
COMMAND: AT+SDATACONF=1,"TCP","2.138.140.67",80
RESPONSE:
RESPONSE: OK
Starting TCP Connection
COMMAND: AT+SDATASTART=1,1
RESPONSE:
RESPONSE: OK
Getting status ok=+SOCKSTATUS: 1,1,0102,0,0,0
COMMAND: AT+SDATASTATUS=1
RESPONSE:
RESPONSE: +SOCKSTATUS: 1,1,0102,0,0,0
Arduino GPRS ++ Inicialitzant
Inici loop ...
Establint connexio ...
RESPONSE:
RESPONSE: +STCPC:1
Seems to be inestable the response of the module.
…
Any suggest ?
Thank’s.