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();
}
}