Arduino parsing webcode not working?

I am trying to get the arduino to send the new status (var1) to the server if it does not equal the old status (oldvar1). It uses a php script to put var1 into the database (WORKING), however to get the old status it has to read another php script headgateget.php. I cannot seem to get it to read it and set the value to oldvar1. Can someone check my code and see if i made an error? It should be the last function that is called.

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>

// MAC Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

const char* ip_to_str(const uint8_t*);
const char* hostName();
int setHostName(const char* newHostName);
// Initialize the Ethernet server library
//Server server(8080);

int localstatus = 7; //status

int gatestatus = 4; //old status
int s;

byte serverip[] = { 142, 55, 49, 114 };
Client client(serverip, 80); //apache web server running on port 80

void setup()
  pinMode (2,INPUT); //test pin
  Serial.println("Attempting to obtain a DHCP lease...");

  // Since we're here, it means that we now have a DHCP lease, so we print
  // out some information.
  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
  Serial.println("A DHCP lease has been obtained.");

  Serial.print("My IP address is ");
  Serial.print("Gateway IP address is ");
  Serial.print("DNS IP address is ");
  // Start the server
   Serial.println("Starting Server.");

void loop()

Serial.println("Comparing Server Status"); 
gatestatus = readvalues(); 

 if (localstatus!= gatestatus){
         if (client.connect()) {
          client.print("GET /headgatetestset.php?Status=");     // Path only, not the complete URI

          client.print( millis() );          // prevent caching, like justjed said a post before
          client.println(" HTTP/1.0");                // no need for 1.1
          client.println("Connection: close");
          Serial.println("posted to db");

      Serial.println("Gate Status is");
      if (digitalRead(2) == HIGH)
        Serial.println("status 1");
        localstatus = 1;
      if (digitalRead(2) == LOW)
      Serial.println("Status 0");
        localstatus = 0;
    // give the web browser time to receive the data
    // close the connection:

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;

int readvalues()
    Serial.println("In Read Values");
    if (client.connect()) {
    Serial.println("Readvalues client connected");
    client.println("GET /headgateget.php");
    client.print( millis() );          // prevent caching, like justjed said a post before
    client.println("HTTP/1.0");                // no need for 1.1
    // client.println("Connection: close");
    while (client.connected()){
      if (client.available()) {
      Serial.println("client is avaliable printing page");
      char c =;
      Serial.println("parsed page, C=");
      s = (int)c;
      return s;
    if (!client.connected()) {

Can someone check my code and see if i made an error?

Yes, you have. That code won't even compile.

void loop()


oldvar1 = readvalues(); 

The code that follows this is not inside a function, and will cause all sorts of compiler errors.

oh sorry messed a parenthesis up while i was troubleshooting, i edited the first post to show the revised code

oldvar1 = readvalues();

So, oldvar1 is the new value that just arrived?

Instead of var1 and oldvar1, give variables meaningful names, like pinState and phpState.

You have Serial.print(ln)() statements that print useless information. Why not add some that print useful information? Printing pinState after reading the pin state would be useful. Printing phpState after reading it from the connection would be useful. Serial.println("Comparing Server Status"); simply tells us that loop started. Well, we know that happens. A lot.

You are not enabling the internal pullup resistor for pin 2. This implies that you have an external pullup or pulldown resistor wired with the switch. Do you? How IS the switch wired?

The switch is wired with a pulldown resistor to maintain ground and pressing it sends pin 2 +5 V to give a high. I fixed some coding errors last night and it seems to work better, however from the serial prints it does not seem to me pulling the right value out of the function readvalues(). in it c is equal to 0 but the after readvalues() returns a value, gatestatus (made the variables have more meaningful names) does not change to what c was equal to, instead it changes to 48 and stays at 48. This seems to be because when i try to convert c to in integer and store it in s, it turns into the value 48. In fact no matter what value c is ( 0 or 1) , s is always equal to 48. :S

Solved the problem, 48 is the ascii code for 0 so i just did s = c - '0'

Testing it later tonight, ill let you guys know how it goes