I am trying to send data (GPS coordinates and temperature from a sensor) to a server using the Arduino GSM Shield with Arduino Mega , GPS module EM-406a and DHT Temperature Sensor
I am able to connect the server once in the setup() function using GET request then receive a response from the server in the loop().
But that's it, when I try to send data again in the loop() it fails.
How can I keep the connection alive in the loop() function in order to send data whenever I want?
I used code from the example provided in the Arduino GSM library webpage (http://arduino.cc/en/Guide/ArduinoGSMShield)
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(4800);
dht.setup(7);
while (notConnected) {
Serial.println("Not Connected yet");
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
if(gsmAccess.begin(PINNUMBER)==GSM_READY){
if(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY){
notConnected = false;
Serial.println("Now It's Connected");
}
}
else{
delay(1000);
}
}
// if you get a connection, report back via serial:
if (client.connect(server, port))
{
Serial.println("connected");
// Make a HTTP request:
client.print("GET ");
client.print("mysite/index.php");
client.println(" HTTP/1.0");
client.println();
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
unsigned long chars;
unsigned short sentences,failed_checksum;
void loop ()
{
///////////////// GPS data
while(Serial.available()){ // check for gps data
if(gps.encode(Serial.read())){ // encode gps data
//grap location data
gps.get_position(&lat,&lon);
//grap temperature
temp=dht.getHumidity();
Serial.println("Sending Data to server");
if(client.connect(server, port)){
Serial.println("connected");
// Make a HTTP request:
client.print("GET ");
client.print("mysite/mypage.php?lat="+lat+"&lon="+lon+"&temp="+temp);
client.println(" HTTP/1.0");
client.println();
}
else
{
// if you didn't get a connection to the server
Serial.println("faild to send");
}
//exit Serial.available() loop once you have sent some data
break;
} // getting latitude and longitude
}//end while loop
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available())
{
char c = client.read();
Serial.print(c);
digitalWrite(ServerLED, HIGH);
}
// if the server's disconnected, stop the client:
if (!client.available() && !client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
I get the information saved in my homepage (just simple text) first. Then it's always failing to connect again and this is executed: Serial.println("faild to send");
Any idea ???