I have just been testing this, and have come to the conclusion that if the wait while ready loop in gsm.begin doesn't get a ready response after the first loop, it never seems to get one in subsequent loops. Each retry of the loop takes several minutes if there is no available signal, so I have reduced the loop count to 1 with no discernible ill effects.
So my version the begin function in GSM3ShieldV1AccessProvider.cpp looks like this;
// Begin
// Restart or start the modem
// May be synchronous
GSM3_NetworkStatus_t GSM3ShieldV1AccessProvider::begin(char* pin, bool restart, bool synchronous)
{
// Serial.println("gsm.begin()");
pinMode(__RESETPIN__, OUTPUT);
// If asked for modem restart, restart
if (restart)
{
HWrestart();
}
else
{
HWstart();
}
unsigned loopCnt = 1; // Try just once, takes a long time
theGSM3ShieldV1ModemCore.gss.begin(9600);
// Launch modem configuration commands
ModemConfiguration(pin);
// If synchronous, wait till ModemConfiguration is over
if(synchronous)
{
// if we shorten this delay, the command fails
while(ready()==0 && loopCnt--)
{
delay(1000);
// Serial.println("gsm.begin() waiting for ready");
}
}
return getStatus();
}
and the code I am running is;
/*
SendTemp30mins_v2
This sketch connects to the foobar website through a GSM shield.
Circuit:
* GSM shield attached to an Arduino
* SIM card with a data plan
* Temperature sensor on Pin A0
*/
// libraries (Use with modified library!)
#include <GSM.h>
// APN data
#define GPRS_APN "internet.com" // your GPRS APN
#define GPRS_LOGIN "wapuser1" // your GPRS login
#define GPRS_PASSWORD "wap" // your GPRS password
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
void setup()
{
// initialize serial communications
Serial.begin(9600);
}
void loop()
{
// read temperature sensor
String sensorValueString = String(analogRead(A0), DEC);
// put reading into path
String path = String("/data/test.php?temperature_1=" + sensorValueString + "&temperature_2=0");
// send result to server
String uploadResult;
uploadResult = uploadReading(path);
Serial.println(uploadResult);
// wait for half an hour
if (uploadResult == "OK")
{
Serial.println("30 minutes delay....");
delay(1800000); // 30 minutes
}
else
{
Serial.println("10 minutes delay....");
delay(600000); // retry in 10 minutes
}
}
String uploadReading(String path)
{
String uploadReturnStatus;
// URL and port
char server[] = "foobar.com";
int port = 80; // port 80 is the default for HTTP
// Start the modem with GSM.begin()
Serial.println("Connecting GSM.");
if(gsmAccess.begin()== GSM_READY)
{
Serial.println("GSM connected.");
}
else
{
Serial.println("GSM connection failed.");
Serial.println("Terminating GSM");
gsmAccess.shutdown();
uploadReturnStatus = "GSM_FAIL";
return uploadReturnStatus;
}
// attach the shield to the GPRS network with the APN, login and password
Serial.println("Connecting GPRS.");
// Retry 5 times before giving up
unsigned loopCntGPRS = 5;
while(loopCntGPRS--)
{
if(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY)
{
Serial.println("GPRS connected.");
break;
}
else
{
Serial.println("GPRS not connected");
delay(1000);
}
if(!loopCntGPRS)
{
Serial.println("GPRS connection failed.");
Serial.println("Terminating GSM");
gsmAccess.shutdown();
uploadReturnStatus = "GPRS_FAIL";
return uploadReturnStatus;
}
}
// Connect to the server and send readings
if (client.connect(server, port))
{
Serial.println("Connected to server.");
// Make a HTTP request:
client.print("GET ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
else
{
Serial.println("Server connection failed");
Serial.println("Terminating GSM");
gsmAccess.shutdown();
uploadReturnStatus = "SERVER_FAIL";
return uploadReturnStatus;
}
// Read incoming server response
String outputString = "";
while (client.available() || client.connected())
{
// if there are incoming bytes available from the server, read them
if (client.available())
{
char c = client.read();
outputString = outputString + c;
if (outputString.length() > 15)
{
// stop the client.
Serial.println("Disconnecting from server.");
client.flush();
client.stop();
break;
}
}
}
Serial.println("Server response;");
Serial.println(outputString);
Serial.println("Terminating GSM");
gsmAccess.shutdown();
// check server response for 200 OK
if (outputString.startsWith("HTTP/1.1 200 OK"))
{
uploadReturnStatus = "OK";
return uploadReturnStatus;
}
uploadReturnStatus = "RESPONSE_FAIL";
return uploadReturnStatus;
}
So I think I have finally got something that sends data if there is a signal, and fails in a reasonable time if there isnt...
So far has been running for 48 hours without locking up - fingers crossed!