Calling PHP within IF statement error

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: 35

Bad 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...");
    }
}

Is there any chance that pinNum could be equal to something other than startPin or stopPin?

maybe "if else" instead of "if if"?

Unfortunatly not, pinNum is either 5 or 6.

I get the same results if i replace the startPin & stopPin with the numbers 5 & 6.

I get the same error therefore with this being the modified section:

      if (pinNum == 5)
      {
        client.println("GET /alarm_states/alarm.php?alarm=1");           // location of ProwlPHP script
      } 
      else if (pinNum == 6)
      {
        client.println("GET /alarm_states/alarm.php?alarm=2");           // location of ProwlPHP script
      }

Have you tried this?

      if (pinNum == 5)
      {
        client.println("GET /alarm_states/alarm.php?alarm=1");           // location of ProwlPHP script
      } 
      else
      {
        client.println("GET /alarm_states/alarm.php?alarm=2");           // location of ProwlPHP script
      }

Bloody hell, simple as that !!

Many thanks, seems to work fine now :slight_smile:

Obvioulsy need to get to grips with my IF statements !

Thank you for taking the time to help.

I am hoping for you it is working.

Can you request both of those urls? What concerns me is if you thought pinNum was only 5 or 6, your original code should have worked.

Indeed, there was an issue with pinNum, but using your suggestion it highlighted the error I had.

I have now made the necessary alteration and the code is now working.

Thanks again.