Go Down

Topic: Arduino parsing webcode not working? (Read 388 times) previous topic - next topic

mike390

Oct 26, 2011, 10:53 pm Last Edit: Oct 27, 2011, 04:04 pm by mike390 Reason: 1
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.

Code: [Select]
#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#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()
{
  Serial.begin(9600);
  pinMode (2,INPUT); //test pin
  Serial.println("Attempting to obtain a DHCP lease...");
 

  EthernetDHCP.begin(mac);

  // 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.println(ip_to_str(ipAddr));
 
  Serial.print("Gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));
 
  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));
 
  // Start the server
   Serial.println("Starting Server.");
   //server.begin();
}

void loop()
{

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

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

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


                              }
    }
      Serial.println("Gate Status is");
      Serial.println(gatestatus);
     
     
      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
    delay(500);
    // close the connection:
    client.stop();
  }


// 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");
    while(1)
    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("Host: cs14.sheridanc.on.ca");
    // client.println("Connection: close");
    client.println();
   //delay(7);
    while (client.connected()){
      if (client.available()) {
      Serial.println("client is avaliable printing page");
      char c = client.read();
      Serial.println("parsed page, C=");
      Serial.println(c);
      delay(100);
      s = (int)c;
      return s;
      }
    }
    }
   
   
    if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
   
   
    }
}


PaulS

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

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

Code: [Select]
void loop()
{

  EthernetDHCP.maintain();

oldvar1 = readvalues();
}

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

mike390

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

PaulS

Code: [Select]
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?

mike390

#4
Oct 27, 2011, 04:04 pm Last Edit: Oct 27, 2011, 08:14 pm by mike390 Reason: 1
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

Go Up