Unable to read text file from server

I saw the Web client Example , In the example, the server connection is initialized in setup(). But I want the server to update the text file every time. So, I need the server connection to be in loop().

The server is connected, but it does not gives the server response for the requested text file. Where is the problem in my code.

    // libraries

    #include <GSM.h>


    // PIN Number

    #define PINNUMBER ""


    // APN data

    #define GPRS_APN       "GPRS_APN" // replace your GPRS APN

    #define GPRS_LOGIN     "login"    // replace with your GPRS login

    #define GPRS_PASSWORD  "password" // replace with your GPRS password


    // initialize the library instance

    GSMClient client;

    GPRS gprs;

    GSM gsmAccess; 


    // URL, path & port (for example: arduino.cc)

    char server[] = "yourdomain.com";

    char path[] = "/current.txt";

    int port = 80; // port 80 is the default for HTTP



    void setup()

    {
  
    // initialize serial communications and wait for port to open:
  
    Serial.begin(9600);

    Serial.println("SMS connected");
 
  
    // 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("GPRS connected...");
 
    Serial.println(" Connecting to Server ");

    }


    void loop()

    {


    
    if (client.connect(server, port))
  
    {

 
    client.print("GET /current.txt");
 
    Serial.print("GET /current.txt");


    client.println(" HTTP/1.1");
 
    Serial.println(" HTTP/1.1");
 
    client.println("Host: www.yourdomain.com");
 
    Serial.println("Host: www.yourdomain.com");
 
    client.println("User-Agent: Arduino");
 
    Serial.println("User-Agent: Arduino");

    client.println("Accept: text/html");
 
    Serial.println("Accept: text/html");
 
    client.println("Connection: close");
 
    Serial.println("Connection: close");
 
    client.println();
 
    Serial.println();

 
    Serial.println("\nCOMPLETE!\n");

     //client.stop();
 
  
    }


    else

    {
 
    Serial.println("connection failed");
 
    Serial.println("\n FAILED!\n");
 

    }

 
  
    if (client.available())
  
    {
   
    
    char c = client.read();
   
     Serial.print(c);
  
     }
  
  
    else

    {
  
    Serial.println();
    
    Serial.println("disconnecting.");
    
    client.stop();

    }

    }

What's

with

all

the

blank

lines?

Fix your code!

What is the problem with it ? Do you have idea to the question ??

You are not waiting for the server to respond in this section of your code.

    if (client.available())  {
     char c = client.read();
      Serial.print(c);
    }
    else {
     Serial.println();
     Serial.println("disconnecting.");
     client.stop();
    }

Use this instead. This is the "Perfect World" version, but it should work ok as long as the connection doesn't break or the server doesn't stall.

while (client.connected()) {
  while(client.available()) {
    char ch = client.read();
    Serial.print(ch);
  }
}
client.stop();

Fantastic brother .. Thank you

Now for the "Real World" version. This has a timeout to prevent the while(client.connected()) loop from becoming an endless loop if the connection breaks or the server stalls.

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      char ch= client.read();
      Serial.write(ch);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();
  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

Thank you brother for the details. Actually I have a textfile in the server which contains (* followed by 1 or 2 words). The response form the server is

GET /current.txt HTTP/1.1
Host: www.yourdomain.com
User-Agent: Arduino
Accept: text/html
Connection: close


COMPLETE!

HTTP/1.1 200 OK
Date: Thu, 15 May 2014 17:02:05 GMT
Server: Apache mod_fcgid/2.3.10-dev
Last-Modified: Thu, 15 May 2014 16:40:23 GMT
ETag: "7f201d1-7-4f972f413c45a"
Accept-Ranges: bytes
Content-Length: 7
Content-Type: text/plain
Expires: Thu, 15 May 2014 17:06:25 GMT
Connection: close

*America
place = America
                                                                           // Current.txt contains *London
GET /current.txt HTTP/1.1
Host: www.yourdomain.com
User-Agent: Arduino
Accept: text/html
Connection: close

COMPLETE!


HTTP/1.1 200 OK
Server: Apache mod_fcgid/2.3.10-dev
ETag: "7f201d1-7-4f972f413c45a"
Accept-Ranges: bytes
Content-Length: 7
Content-Type: text/plain
Age: 6
Date: Thu, 15 May 2014 17:02:05 GMT
Last-Modified: Thu, 15 May 2014 16:40:23 GMT
Expires: Thu, 15 May 2014 17:06:25 GMT
Connection: close

*America
place = America                    // // Here the current.txt contains London but still shows America

The code is given below :
Here in the code I am using z= '' as a delimiter to identify the important text coming after '' to be stored in to place string.

  // libraries

    #include <GSM.h>


    // PIN Number

    #define PINNUMBER ""


    // APN data

    #define GPRS_APN       "GPRS_APN" // replace your GPRS APN

    #define GPRS_LOGIN     "login"    // replace with your GPRS login

    #define GPRS_PASSWORD  "password" // replace with your GPRS password


    // initialize the library instance

    GSMClient client;

    GPRS gprs;

    GSM gsmAccess; 
    
    //Variables Declaration
    char k[30];
    
    char z = '*';
    
    int s=0;
    
    String place;


    // URL, path & port (for example: arduino.cc)

    char server[] = "manihattysemiconductors.com";

    char path[] = "/currentlocation.txt";

    int port = 80; // port 80 is the default for HTTP



    void setup()

    {
  
    // initialize serial communications and wait for port to open:
  
    Serial.begin(9600);

    Serial.println("SMS connected");
 
  
    // 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("GPRS connected...");
 
    Serial.println(" Connecting to Server ");

    }


    void loop()

    {


    
    if (client.connect(server, port))
  
    {

 
    client.print("GET /current.txt");
 
    Serial.print("GET /current.txt");


    client.println(" HTTP/1.1");
 
    Serial.println(" HTTP/1.1");
 
    client.println("Host: www.yourdomain.com");
 
    Serial.println("Host: www.yourdomain.com");
 
    client.println("User-Agent: Arduino");
 
    Serial.println("User-Agent: Arduino");

    client.println("Accept: text/html");
 
    Serial.println("Accept: text/html");
 
    client.println("Connection: close");
 
    Serial.println("Connection: close");
 
    client.println();
 
    Serial.println();

 
    Serial.println("\nCOMPLETE!\n");

     //client.stop();
 
  
    }


    else

    {
 
    Serial.println("connection failed");
 
    Serial.println("\n FAILED!\n");
 

    }

 
  
 while (client.connected()) {
  while(client.available()) {
    char c = client.read();
    Serial.print(c);
      if (z==c)
    {
      s=1;
   
    }

    if(s == 1 && c!='*')
     {
       int i=0;
      k[i] = c;
      i=i+1;
     // Serial.print(k);
      }
   
place +=k;
  
  }

}
   Serial.print("place = ");  
   Serial.print(place); 
client.stop();


    }

Here the problem is, the place value should display London as it was updated in the server but it still shows America. Thanks in advance brother ...

It is a text file. Why would the place change to London? You would need to modify the "/current.txt" file contents. Is that what you are doing?

The Arduno ethernet shield/library does not have a cache or anything like that, so the file has not been changed to the content you think it should have in the Apache server.

Perhaps the file content has been cached within the web server. Restarting the web server ought to take care of that.