web request (ethernet shield) only works one time?

Hi,

Im making a web request that fetches a value online. That value is put into a servo motor.
When the chip runs the very first time, it all works. However, the second time … it just fetches 0.
I probably need to reset some data, but im new to arduio so any help would be appreciated:

#include <Servo.h> 
#include <Ethernet.h>
#include <SPI.h>


byte server[] = { 00,00,00,00 }; //took away the ip address of my host :)
String location = "/get.html HTTP/1.0";
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4C, 0x7D };
int pos = 0;


EthernetClient client;
char inString[32];
int stringPos = 0;
boolean startRead = false;

Servo s1;



void setup(){
  Ethernet.begin(mac);
  Serial.begin(9600);
  s1.attach(9);
}

void loop(){
  
  String pageValue = connectAndRead();
 
  int value_as_int = pageValue.toInt();
  Serial.println(value_as_int);
  
  if (value_as_int > 1){  //Attempted this as a bug fix... but it doesnt fix the fact that 0 is returned..
    
      s1.write(value_as_int);  

  }
  delay(5000);
  
  
}

String connectAndRead(){


  Serial.println("connecting...");


  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();
    return readPage();
  }else{
    return "connection failed";
  }

}

String readPage(){

  stringPos = 0;
  memset( &inString, 0, 32 );

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { 
        startRead = true; 
      }else if(startRead){

        if(c != '>'){ 
          inString[stringPos] = c;
          stringPos ++;
        }else{
          
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;

        }

      }
    }

  }

}

The serial output is as followed:

connecting...
connected
disconnecting.
50
connecting...
0
connecting...
0
connecting...
0
connecting...
0
connecting...
0
connecting...
  int value_as_int = pageValue.toInt();

occurs after:

    return "connection failed";

How do you expect that string to be converted to an int?

Why are you using String at all? The readPage() function writes to a global variable, then creates a String to wrap that char array. A waste of memory...

  if (value_as_int > 1){  //Attempted this as a bug fix... but it doesnt fix the fact that 0 is returned..

Of course it doesn't.

The question is why you fail to connect after the first succeeds. That may have more to do with your server than anything else. Is it setup to reject connections too close together?

Why is the Arduino polling the web site for a value? If the web site is trying to control the Arduino, the Arduino should be serving the page, as a form. Then, the client can ask the Arduino to reprocess the page when the form is submitted.

I convert it to int since im using that value to drive a servo. And to be frank , I modified bildr.orgs code as best as I could. Still new to c-style programming.

And the value from the webserver is progress on projects at work. So that the servo will show progress in a visual form by pointing the servo tip towards good/bad. So your assumption that arduino should serve the page will not work for me. The value is derived from a financial system at my word place. But for debugging purposes I changed it to a flat text file, just to verify that it is indeed an arduino problem .. not a web server problem.

So your assumption that arduino should serve the page will not work for me.

It wasn't an assumption. It was a question. And, you have a good answer.

I convert it to int since im using that value to drive a servo.

"How can you convert "connection failed" to an int?" was the question.

But for debugging purposes I changed it to a flat text file, just to verify that it is indeed an arduino problem

If the Arduino can connect once, it should be able to connect over and over. So, the failure to connect a second time is a problem on the server end.

Perhaps the server doesn't want to be pestered every 5 seconds. You'll need to look at the server configuration, I think.

When the request failes, it does not even try to send any packets to the server. Hence, not server error. I sniffed the traffic. I see the first request fine, but not the second one. Im guessing either something to do with the DHCP negotiation , and / or code.

Tries sleeping after the init. of the ethernet module to ensure that it had received a valid IP. Valid IP is delivered.

So is there anything in the code that does not reset? That I just cant seem to spot ??

Client test code you can try, and it works, you can modify to try with your server.

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway);
  Serial.begin(9600); 
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}