I am trying to shorten my code by using an IF statement inside one of the function calls to avoid having seperate function calls.
However, when the PHP is called using the below function, I get an error in the serial debug window and the PHP fails to be called:
Response from server: HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Sat, 17 Sep 2011 14:03:35 GMT
Connection: close
Content-Length: 35Bad Request (Invalid Verb)
disconnecting from server Disconnected...
void alarm(){
unsigned long startTime = millis();
if (client.connect()) { //connect to server
Serial.println("connected to server");
// Make a HTTP request:
if (pinNum == startPin)
{
client.println("GET /alarm_states/alarm.php?alarm=1"); // location of ProwlPHP script
}
if (pinNum == stopPin)
{
client.println("GET /alarm_states/alarm.php?alarm=2"); // location of ProwlPHP script
}
client.println("Host: xxxxxxxx.homeserver.com"); // domain name
client.println("Connection: close"); // this line is not related to your problem but I recommend it ;)
client.println();
delay(1000);
}
else {
Serial.println("failed to call PHP");
}
delay(1000);
Serial.print("Response from server: ");
while ((!client.available()) && ((millis() - startTime ) < 5000));
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println("disconnecting from server");
client.stop();
client.flush();
Serial.println("Disconnected...");
}
}
However, removing the IF statements and still call the PHP using the same request, it works fine (working code below) ?
Can't seem to figure this one out, any ideas anyone please ?
void alarm(){
unsigned long startTime = millis();
if (client.connect()) { //connect to server
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /alarm_states/alarm.php?alarm=1"); // location of ProwlPHP script
client.println("Host: xxxxxxxx.homeserver.com"); // domain name
client.println("Connection: close"); // this line is not related to your problem but I recommend it ;)
client.println();
delay(1000);
}
else {
Serial.println("failed to call PHP");
}
delay(1000);
Serial.print("Response from server: ");
while ((!client.available()) && ((millis() - startTime ) < 5000));
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println("disconnecting from server");
client.stop();
client.flush();
Serial.println("Disconnected...");
}
}