Inconsistent HTTP Connnection

Yeah, that's exactly what I was thinking it did... XD

Thanks so much! I'll keep you posted if I run into errors with this, but so far so good.

The others were not mine.

I can't beleve this code is yours! It doesn't contain the magic pixie dust (below) that makes all things work! :slight_smile: I notice the code does have a fat 2000ms delay that has been used in older code.

void setup()
{
   Serial.begin(9600);

   // disable the SD SPI interface
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);

   // rest of your setup
}

zoomkat:

The others were not mine.

I can't beleve this code is yours! It doesn't contain the magic pixie dust (below) that makes all things work! :slight_smile: I notice the code does have a fat 2000ms delay that has been used in older code.

void setup()

{
   Serial.begin(9600);

// disable the SD SPI interface
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);

// rest of your setup
}

You did not pay attention, because it does have the magic pixie dust! :slight_smile:

You did not pay attention, because it does have the magic pixie dust!

Oh, my bad! Just as a test, I removed the pixie dust from your code like below, and ate some salted peanuts instead. The code made 17 successful passes, so salted peanuts work too! :slight_smile:

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(74,125,227,16); // Google

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

void setup() {
  Serial.begin(9600);

  //pinMode(4,OUTPUT);
  //digitalWrite(4,HIGH);

  Ethernet.begin(mac);
  delay(2000);
  Serial.println("Ready");
}

I used to think that too. Here is an old thread that we both were involved in. Read reply #28.
http://arduino.cc/forum/index.php/topic,75324.0.html
I don't think salted peanuts would have helped there. The magic pixie dust did.

The OP's question was why mine was working and the others weren't. This is part of the reason why.

edit: If you want to read the posts where I was being you and eating salted peanuts, read replies 7,20,and 26.

One question? How can we be sure that the program will never stop on the line

while(client.connected()

If the client connection does not end we will never go out of the while loop

cantore:
One question? How can we be sure that the program will never stop on the line

while(client.connected()

If the client connection does not end we will never go out of the while loop

Good question. There is the possibility of a lockup there if the connection breaks. Here is the thread with the discussion and the patch for a 10 second timeout.

thanks

SurferTim:
This loads www.google.com every 10 seconds. Don't do that to Google too long. Besides, it is a big page and takes a while to display at 9600 baud. Change the server ip to yours.

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(74,125,227,16); // Google

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

void setup() {
  Serial.begin(9600);

pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready");
}

void loop()
{
  if(loopCount < 10)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;

if(!getPage(server,"/")) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }

loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[64];

Serial.print("connecting...");

if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
    }
  }

Serial.println();

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

return 1;
}

The server I'm trying to reach is in a virtual hosting environment so in addition to the ip address, I need to specify the host I want to connect to. Do you have any advice on where that would be included if one wanted to make that tweak?

It is sent on a line by itself after the GET, followed by a double CR-LF.

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");
    // Insert it here. 
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);
  }

edit: Insure outBuf has sufficient memory allocated to hold all that.

SurferTim:
It is sent on a line by itself after the GET, followed by a double CR-LF.

  if(client.connect(ipBuf,80))

{
    Serial.println("connected");
    // Insert it here.
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);
  }




edit: Insure outBuf has sufficient memory allocated to hold all that.

Thank you! Have to head to a Memorial Day event but will hopefully get to tinker with that upon return this evening!

This format is a bit different that you will see in examples. The reason I use this is it is easier to pass GET variables attached to the page. The sprintf function makes it easy to format a character array.

You could use the standard three client.println() calls to do that, but bear in mind each call to client.print(), client.println, or client.write() is sent in its own packet.

client.println("GET / HTTP/1.0");
client.println("Host: www.mydomain.com");
client.println();

Why send three packets? The client.write(outBuf) sends all that in one packet. Clean.

SurferTim:
This format is a bit different that you will see in examples. The reason I use this is it is easier to pass GET variables attached to the page. The sprintf function makes it easy to format a character array.

You could use the standard three client.println() calls to do that, but bear in mind each call to client.print(), client.println, or client.write() is sent in its own packet.

client.println("GET / HTTP/1.0");

client.println("Host: www.mydomain.com");
client.println();




Why send three packets? The client.write(outBuf) sends all that in one packet. Clean.

Ah! So you're saying that I'd use:

char outBuf[1000];
sprintf(outBuf,"GET / HTTP/1.0 Host: www.mydomain.com");
// This call sends the whole thing as one packet
client.write(outBuf);

...?

Ah! So you're saying that I'd use:

A 1000 character buffer is using half the SRAM on a 328 based Arduino. Not likely to succeed in too many sketches. Are you really writing 1000 characters to the buffer?

Doing so won't result in a single packet, as packets are not that large.

The below might also work for a single packet.

client.println("GET / HTTP/1.0\r\nHost: www.mydomain.com\r\n");

Thanks very much Zoomkat & PaulS. Both helpful comments.

It turns out that my issue was something much more elementary (and embarrassing). I reviewed SurferTim's code in Reply #17 back in April, and noticed something in his code that mine didn't have:

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

Once I added that to mine, it's worked 100% of the time. :slight_smile:

Priot to adding that, I was creating connections but never closing them, so when subsequent attempts to connect were made, they only worked once in a while (presumably after the original, successful connection had closed because it just timed out!).

Thanks guys!!

this code is working good, but my problem is i need to use domain as server address. how can i do that

Insure you have a reliable dns server selected in your static IP settings, or use dhcp to get your network settings. Then use this. A return value of 1 is the only success value. Any other return value is a fail.

char server[] = "www.mydomain.com";

 if(client.connect(server,80) == 1) {

zoomkat:
I can't beleve this code is yours! It doesn't contain the magic pixie dust (below) that makes all things work! :slight_smile: I notice the code does have a fat 2000ms delay that has been used in older code.

void setup()

{
  Serial.begin(9600);

// disable the SD SPI interface
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

// rest of your setup
}

The auto reconnection was fixed when use client.connect() in loop. Thank you SurferTim.....i can't believe too, i lost three day to find it. And ignored this.

You are welcome. Sometimes you just need a little magic pixie dust! :slight_smile: