Go Down

Topic: Connecting to GSM/GPRS unstable, eventually hangs (Read 11552 times) previous topic - next topic

JaanH

Apr 14, 2014, 10:37 am Last Edit: Apr 14, 2014, 11:14 am by JaanH Reason: 1
Hi,
I have a project with a goal to reliably upload data from multiple xbees to a server. For the last week I've run into issues with the GSM connection becoming unresponsive and the Arduino MEGA2560 hanging infinitely. During this time I have rewritten my method of connecting to the network multiple times. The most reliable way so far ran for 36 hours, uploading dummy data every 10 minutes. This was accomplished using this code (personal details have been omitted):

Code: [Select]

#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN       "XXX" //GPRS APN
#define GPRS_LOGIN     "XXX"    //GPRS login
#define GPRS_PASSWORD  "XXX" //GPRS password

// Initialize the library instances
GSMClient client;
GPRS gprs;
GSM gsmAccess(true);

//Server details
char server[] = "XX.XXX.XXX.XXX";
int port = XXXX;

int trynr = 0;
int oknr = 0;

void setup(){
 Serial.begin(9600); //Start serial connection
 Serial.println("Starting up...");
}

void loop(){
 trynr++;
 Serial.println("Connecting to GSM network...");
 
 if(gsmAccess.begin(PINNUMBER)==GSM_READY){
      Serial.println("PIN OK");
      delay(1000);
   
   if(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY){
       Serial.println("GPRS OK");
       delay(1000);
       
       if(client.connect(server, port)){
         oknr++;
         client.print("Test ");
         client.print(oknr);
         client.print("/");
         client.print(trynr);
         client.flush();
         client.stop();
         delay(60000);
      }
   }
 }
}


I don't have the debug log on hand at the moment, but the script is running with Serial Monitor attached right now. I will add the failing debug log as soon as I get them.

First hitch:
Code: [Select]

Connecting to GSM network...
AT%13%
89 103>%13%%10%Call Ready%13%%10%
Connecting to GSM network...
AT%13%

And it just hangs on this.

Before this, I had a prototype that normally ran between 8-20 hours, with the occasional hangs much sooner. As this code is a lot more messy, I uploaded it to codepad: http://codepad.org/gRNffniX

When failing, I either get stuck on AT%13% and it never moves forward or I start getting a really long line of %240%-s. Sometimes I start connecting and as soon as it gets a connection with GPRS it starts connecting again. I don't have antennas on hand to post a full debug report for this one yet, but I can tomorrow.

I just ran a test with this code as well and I got stuck on this after one successful upload:
Code: [Select]

Connecting to GSM network...
GSM Access begin
AT%13%





The Arduino MEGA2560 and the Arduino GSM Shield have been running from a 2A power supply since day one.

JaanH

#1
Apr 14, 2014, 11:34 am Last Edit: Apr 14, 2014, 11:39 am by JaanH Reason: 1
Here are debug logs of successful connections. I have two Mega2560 and GSM Shield combos and two SIM cards from the same provider but with different contracts. The lower one is my personal SIM the other upper one is a data only SIM card.

This is what a successful connection and upload look like with the simpler code (personal details omitted):
Code: [Select]

Starting up...
Connecting to GSM network...
AT%13%
0 9>AT%13%%13%%10%OK%13%%10%
AT+CGREG?%13%
9 40>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
40 71>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
71 102>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
102 5>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
5 36>AT+CGREG?%13%%13%%10%+CGREG: 0,1%13%%10%%13%%10%OK%13%%10%
AT+IFC=1,1%13%
36 53>AT+IFC=1,1%13%%13%%10%OK%13%%10%
AT+CMGF=1%13%
53 71>%19%%17%AT+CMGF=1%13%%13%%10%OK%13%%10%
AT+CLIP=1%13%
71 87>AT+CLIP=1%13%%13%%10%OK%13%%10%
ATE0%13%
87 98>ATE0%13%%13%%10%OK%13%%10%
AT+COLP=1%13%
98 104>%13%%10%OK%13%%10%
PIN OK
AT+CGATT=1%13%
104 110>%13%%10%OK%13%%10%
AT+QIFGCNT=0%13%
110 116>%13%%10%OK%13%%10%
AT+QICSGP=1,"[MY APN]","[LOGIN]","[PASSWORD]"%13%
116 122>%13%%10%OK%13%%10%
AT+QIMUX=0%13%
122 0>%13%%10%OK%13%%10%
AT+QIMODE=1%13%
0 6>%13%%10%OK%13%%10%
AT+QINDI=1%13%
6 12>%13%%10%OK%13%%10%
AT+QIREGAPP%13%
12 18>%13%%10%OK%13%%10%
AT+QIACT%13%
18 24>%13%%10%OK%13%%10%
GPRS OK
AT+QIDNSIP=0%13%
24 30>%13%%10%OK%13%%10%
AT+QIOPEN="TCP","[SERVER IP]",[PORT]%13%
30 36>%13%%10%OK%13%%10%

30 47>%13%%10%OK%13%%10%%13%%10%CONNECT%13%%10%
Test 0/1+++AT+QICLOSE%13%
65 65>

Test 1/1


And this is what it looks like with the longer one:

Code: [Select]

Starting up...
AT%13%%10%ATE0%13%%10%Starting connections!

Connecting to GSM network...
GSM Access begin
AT%13%
44 69>AT%13%%13%%10%OK%13%%10%%13%%10%+CPIN: READY%13%%10%
AT+CGREG?%13%
69 100>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
100 3>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
3 34>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
34 65>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
65 96>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
96 127>AT+CGREG?%13%%13%%10%+CGREG: 0,1%13%%10%%13%%10%OK%13%%10%
AT+IFC=1,1%13%
127 16>AT+IFC=1,1%13%%13%%10%OK%13%%10%
AT+CMGF=1%13%
16 34>%19%%17%AT+CMGF=1%13%%13%%10%OK%13%%10%
AT+CLIP=1%13%
34 50>AT+CLIP=1%13%%13%%10%OK%13%%10%
ATE0%13%
50 75>ATE0%13%%13%%10%OK%13%%10%%13%%10%Call Ready%13%%10%
AT+COLP=1%13%
75 81>%13%%10%OK%13%%10%
...in while...
else
AT+CSQ%13%%10%while->while
AT+CGATT=1%13%
101 107>%13%%10%OK%13%%10%
AT+QIFGCNT=0%13%
107 113>%13%%10%OK%13%%10%
AT+QICSGP=1,"[APN]","[LOGIN]","[PASSWORD]"%13%
113 119>%13%%10%OK%13%%10%
AT+QIMUX=0%13%
119 125>%13%%10%OK%13%%10%
AT+QIMODE=1%13%
125 3>%13%%10%OK%13%%10%
AT+QINDI=1%13%
3 9>%13%%10%OK%13%%10%
AT+QIREGAPP%13%
9 15>%13%%10%OK%13%%10%
AT+QIACT%13%
15 21>%13%%10%OK%13%%10%
Connected to GPRS.
Connecting to server...
AT+QIDNSIP=0%13%
21 27>%13%%10%OK%13%%10%
AT+QIOPEN="TCP","[IP]",[PORT]%13%
27 33>%13%%10%OK%13%%10%

27 44>%13%%10%OK%13%%10%%13%%10%CONNECT%13%%10%
Connected


Buffer:
<Test1;Test2><13;21;440>(0/1)Sent!
Nr of tries: 1
Nr of successseseses: 1
+++AT+QICLOSE%13%
62 62>
Disconnected from server.
GSM shutdown.


This one won't even start connecting the second time. It just stays on:
Code: [Select]
Connecting to GSM network...
GSM Access begin
AT%13%


This might be because I shut the Shield down to save power, as the final design demands this. Then again, the same thing works with another Arduino Mega2560 and GSM Shield quite reliably.

markusdnd

Aaah, the infamous AT%13%

no reply and hanging. Lets see if http://forum.arduino.cc/index.php?topic=232991.new;topicseen#new gets something resembling that.

I have the same kind of problem.

JaanH

OK, I figured it out. Following http://forum.arduino.cc/index.php?topic=180374.0, it seems the problem occurs when starting communication with the GSM module and a reply is never recieved. So, to fix the stuck on AT%13% bug, simply change this in GSM3ShieldV1AccessProvider.cpp:

Code: [Select]
GSM3_NetworkStatus_t GSM3ShieldV1AccessProvider::begin(char* pin, bool restart, bool synchronous)
{
pinMode(__RESETPIN__, OUTPUT);

// If asked for modem restart, restart
if (restart)
HWrestart();
else
HWstart();
 
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)
delay(1000);
}
return getStatus();
}


To this:
Code: [Select]
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 = 30;
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();
}


This way, even if the hang occurs, it is timed out properly.

martinhagreen

hi! I have an similar issue
What do you refer saying that "it is timed out properly"?

JaanH

With the default library, the gsmAcces.begin() can hang on AT%13%, which means the modem isn't answering. If you add this timeout, if the modem hasn't successfully connected to the GSM network in 30s it tries again.

shanevanj

Yes, Yes, Yes !

It's fixed my problem - what a lot of time wasted before I checked the forum  :D

SO I am using MQTT library (PubSub) and it works really well (so far - feedback after a few hours - it has at least passed by 5min boundary that was failing before!)


@ JaanH - thanks!

shanevanj

Okay - so maybe not so solved yet .....

I modified the GSM3ShieldV1AccessProvider.cpp (in the GSM library) as suggested in another thread (http://forum.arduino.cc/index.php?topic=180374.0 and JaanH earlier post)

Code: [Select]

// SVJ - modified to introduce a timeout in the access loop (loopCnt)
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 = 15; // SVJ = Modified from suggested 30
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();
}


This was better but did not solve the constant data disconnect after 3 or 4 minutes once GPRS had been established and a connection to my server was made. I then also added this retry count into the GPRS provider (GSM3ShieldV1DataNetworkProvider.cpp) as well and this now at least it reconnects after my comms check routing restarts the board (previously the system would hang on trying to reconnect after a comms lost restart);

Code: [Select]

//Attach GPRS main function.
// SVJ - limit loop count below to stop locking up in a constant attach loop
GSM3_NetworkStatus_t GSM3ShieldV1DataNetworkProvider::attachGPRS(char* apn, char* user_name, char* password, bool synchronous)
{
user = user_name;
passwd = password;
// A sad use of byte reuse
theGSM3ShieldV1ModemCore.setPhoneNumber(apn);

theGSM3ShieldV1ModemCore.openCommand(this,ATTACHGPRS);
theGSM3ShieldV1ModemCore.setStatus(CONNECTING);

attachGPRSContinue();
       unsigned loopCnt = 15; // SVJ retry counter added
// If synchronous, wait till attach is over, or not.
if(synchronous)
{
// if we shorten this delay, the command fails
while(ready()==0 && loopCnt--) // SVJ retry counter added
delay(100);
}

return theGSM3ShieldV1ModemCore.getStatus();
}


It now looks a lot better ... I seem to now have something else causing an unexpected restart just after sending my 1st 5min data block........

shanevanj

:smiley-yell: :smiley-yell: :smiley-yell: :smiley-yell: :smiley-yell:

The GPRS data session I establish, randomly disconnects anywhere between 2 mins and 4 mins 30s after it is established. I am using a PubSub session which is sending heartbeat type information every 15s (I can change this value and it has no effect on the problem). This sketch works perfectly with the Arduino ethernet shield without any other mods than including the appropriate library, it is super reliable and stable - the GSM libraries really seem to have many issues!!

Any ideas anyone - I have spend a huge amount of time with this shield and it it the must temperamental arduino product I have used ( and I have used most of them so far) - I even bought a second one as a test and get the same issues. I have contacted the local Quectel agent and his comment was "that M10 is an old module and is superseded by the M95 module".

I am disappointed that Arduino can sanction such an unreliable product and not resolve it many issues - I can't believe I am the only one trying to maintain a constant on data session for monitoring purposes (kinda what the IoT is there for) ??

JaanH

I'm sorry, I can't help with your problem. I used it to send data every 30 minutes or so. Couldn't you gather a bunch of data and dump it at certain intervals? It's what I did and the device I worked on has worked well over a week at a time.

Gwyn

I too am having the issue with my shield hanging on gsm.begin().

The workaround by altering GSM3ShieldV1AccessProvider.cpp as suggested above does solve this problem, but introduces a new problem! I am checking for a returned 200 status, and the workaround breaks this - nothing is recieved.

This is the code I am using (with personal details removed)

Code: [Select]

/*
  Web client

This sketch connects to a website through a GSM shield.

Circuit:
* GSM shield attached to an Arduino
* SIM card with a data plan
* Temperature sensor on A0

*/

// libraries
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// 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()
{
}

void loop()
{
  // initialize serial communications
  Serial.begin(9600);

// URL, path & port
char server[] = "foobar.com";
// char path[] = "/data/test.php?temperature_1=1.5&temperature_2=1.9";
int port = 80; // port 80 is the default for HTTP



  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while(notConnected)
  {
    if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
      (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port))
  {
    // read temperature sensor
    String sensorValueString = String(analogRead(A0), DEC);
   
    // put reading into path
    String path = String("/data/test.php?temperature_1=" + sensorValueString + "&temperature_2=1.9");
   
    Serial.println("connected");
    // 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
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  // wait until there are no more bytes to be read, and the server disconnects
  String outputString = "";
  while (client.available() || client.connected())
  {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available())
    {
      char c = client.read();
     
      outputString = outputString + c;
    }
  }
  Serial.print(outputString);
 
  // check output for 200 OK
  boolean serverOK = false;
  if (outputString.startsWith("HTTP/1.1 200 OK"))
  {
    serverOK = true;
  }
 
  // stop the client:
  Serial.println("disconnecting.");
  client.stop();
  Serial.println("Terminating GSM");
  gsmAccess.shutdown();
  // digitalWrite(3,LOW); // power saving??

  // wait for half an hour (less 60s to account for communication)
  if (serverOK)
  {
    Serial.println("29 minutes delay....");
    // delay(1680000); // 28 minutes
  }
  delay(60000); // 1 minute
}


With the GSM libraries as is, I get;

Code: [Select]

Starting Arduino web client.
connecting...
connected
HTTP/1.1 200 OK
Date: Fri, 04 Jul 2014 03:44:44 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 0
Connection: close
Content-Type: text/html


disconnecting.
Terminating GSM
29 minutes delay....


but with the workaround in place I get;

Code: [Select]

Starting Arduino web client.
gsm.begin()
gsm.begin() waiting for ready
connecting...
connected


disconnecting.
Terminating GSM


The data still gets through to the server, but no response is recieved! Could anyone shed light on this please?

Gwyn

Managed to solve the problem - I think it was due to not having enough space to store a long String!
I have re-written the code to put everything in a function call and made things a bit neater;

Code: [Select]

/*
  SendTemp30mins_v2

This sketch connects to the foobar.com 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>

// PIN Number
#define PINNUMBER ""

// 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 (less 60s to account for communication)
  if (uploadResult == "OK")
  {
    Serial.println("29 minutes delay....");
    delay(1680000); // 28 minutes
  }
  delay(60000); // 1 minute
}

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.");
  // Retry 5 times before giving up
  unsigned loopCntGSM = 5;
  while(loopCntGSM--)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
    {
      Serial.println("GSM connected.");
      break;
    }
    else
    {
      Serial.println("GSM not connected.");
      delay(1000);
    }
    if(!loopCntGSM)
    {
      Serial.println("GSM connection failed.");
      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;
}


which gives the response;
Code: [Select]

Connecting GSM.
gsm.begin()
gsm.begin() waiting for ready
GSM connected.
Connecting GPRS.
GPRS connected.
Connected to server.
Disconnecting from server.
Server response;
HTTP/1.1 200 OK

Terminating GSM
OK
29 minutes delay....


I will leave this running for a few days and see if it is really working as it should.....

Gwyn

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;
Code: [Select]

// 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;
Code: [Select]

/*
  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!

AndersHedberg

I've been running my own modifed GSM shield for a year, and as people have discovered the libraries works in the lab. But once out there in the wild, the things fail. If you look really deeply into the M10 datasheets the reset line of the module should be connected to the Arduino. There are some network issues that simply hangs the modem in an unknown state. Typically if a GPRS connection is lost and not disconnected properly.

My take on this is to connect the reset line to an IO pin of the Arduino, then modify the libraries as been posted here, but also to insert a watchdog timer. The built in watchdog is to fast with just 8 seconds maximum so either a software interrupt that makes it longer or a hardware watchdog. I've used both so far...

If I were to do this project again I would not use the Arduino GSM libraries but just raw AT-commands. Every communication step can and will fail and I need to adjust the timeouts on these events.

msolomos

Is there a workaround yet for the gsm.begin() hang ?
Am having a really hard time with this problem which spoils the whole project. I wasn't able to find a complete solution and although I have try many things and read many posts the gsm shield eventually hangs on gsm.begin(). I post sensor data every 15 minutes.

At first, it might worked for 10-15 posts and then hangs. The 2nd time might work for 10-12 hours and then hangs. Still haven't figure out why this is happening.

Altough I altered the library GSM3ShieldV1AccessProvider.cpp as per previous comments didn't see any difference.

If anyone has find a workaround please let us know!



Go Up