Hi!
I'm using Arduino GSM v2 shield to connect to web page and pull info- basically one integer.
This whole page consists of example - #4
So im using these two functions below to connect and parse result (get the number 4).
the problem is: sometimes when reconnecting to web page, to get the integer 4 or whatever it's displaying, GSM module fails to connect!
it's not always but sometimes, and i don't know why.. What should I do about it?
I'm guessing client.stop() and client.flush() is needed after i have received needed data or before new connection. The thing is, these client.stop() and client.flush() are kind of slow, and blocks main loop() while it flushes or stops client.
void startGSM()
{
// /////////START GSM/////////////
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
Serial.println(F("Starting GSM"));
//gsmSerial.write("AT+GSN\r\n");
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
{
Serial.println("PIN correct = GSM READY");
}
else
{
Serial.println("PIN error");
delay(500);
}
if (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)
{
Serial.println("APN correct = GPRS READY");
}
else
{
Serial.println("APN error");
delay(500);
}
// /////////START GSM/////////////
delay(500);
}
void updateHashtag()
{
// if you get a connection, report back via serial:
Serial.println(F(""));
Serial.println(F("Updating HT..."));
Serial.println(F("Connecting..."));
// if ((client.available() > 0))
// {
// client.flush();// flush before connect
// }
if (client.connect(server, port))
{
Serial.println("Searching for hashtag: " + hashtag);
client.print("GET ");
client.print(path + hashtag);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
if (client.connected())
{
Serial.println(F("CLIENT IS CONNECTED"));
HTparse = true; // start ht parser
}
}
else
{
// if you didn't get a connection to the server:
Serial.println(F("!!!!!!!!!!!!!!!!!!!!!!!!!!!connection failed"));
Serial.println(F("Restarting GSM module"));
HTparse = false; // disable ht parser
startGSM(); // restart GSM module..
}
}
// ////////////////parser start'/////////////
// ////////////////parser start'/////////////
unsigned long GPRSparser()
{
// hashtag_result_int = 0;
// //Serial.println("HT client connected.");
fullDataReceived = false;
if ((client.available() > 0))
{
// && (client.connected())
// Serial.println(F("GPRS client conected."));
cP = client.read();
// Serial.print(cP);
if (cP == '#')
{
writeBool = true;
// Serial.println("I see #");
}
if (writeBool == true)
{
// Serial.print(c );
hashtag_buffer[HTindex] = cP; // appending characters to buffer array
HTindex++;
if (cP == '\n')
{
hashtag_buffer[0] = '0'; // removing # character
hashtag_buffer[HTindex - 2] = '\0';
writeBool = false;
fullDataReceived = true;
}
if (fullDataReceived)
{
fullDataReceived = false;
HTindex = 0; // reset hashtag_buffer
hashtag_result_int = atol(hashtag_buffer);
Serial.print(F("Hashtag_result_int: "));
Serial.println(hashtag_result_int);
HTparse = false; // end ht parser
return hashtag_result_int;
}
}
}
}