2

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().

But, it is not working. Where is the problem in my code.

#include <GSM.h>

#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();
}
}
3
  • 2
    What's not working? Commented May 15, 2014 at 12:38
  • The server is connected, but it does not gives the server response for the requested text file.
    – Manihatty
    Commented May 15, 2014 at 12:50
  • 1
    It looks like your sketch will only ever read the first character from the server. Is that deliberate? Commented May 15, 2014 at 13:51

1 Answer 1

0

Its working as long as the connection doesn't break or the server doesn't stall. We must wait till the response .

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.