Pages: 1 [2] 3   Go Down
Author Topic: Inconsistent HTTP Connnection  (Read 5271 times)
0 Members and 1 Guest are viewing this topic.
Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That was my bad.

Which code are you using now? Are you still trying your original code? Or zoomkat's?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't had a chance to work on this project for the past few days, but I tried using my original code (with a few modifications) as well as zoomkat's. Both seem to give me mixed results as far as connectivity. With zoomkat's, sometimes it connects and downloads a page immediately after I press 'e', other times it takes around 20 seconds before it says connection failed.

I originally had things set up in a for loop so that it would keep trying over and over again. This seemed to provide okay results, but also seemed like a work-around with addressing the actual problem.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm, well that seems to work so far. I've only had a few minutes this morning to play, but I haven't gotten any failed attempts (except for when I forgot to change the IP).

So what's different? Why is this one connecting fine whereas the others weren't?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm, well that seems to work so far. I've only had a few minutes this morning to play, but I haven't gotten any failed attempts (except for when I forgot to change the IP).

So what's different? Why is this one connecting fine whereas the others weren't?

The others were not mine.  smiley

edit: There are some reasons mine works better. I know how tcp works. If you look at the getPage() function, you will notice the "while(client.connected()) while(client.available())" loops.

If you are downloading a big page (like Google's home page), you will get several packets. That loop will check client.available(), and for a while, it will be zero. Then it will be about 1400. Your goal is to empty that rx buffer as fast as you can. Get client.available() to zero. Then it may be zero again for a few checks, then it will be 1400 again (another packet).

The client.connected() will return true until you have emptied the rx buffer of the final packet, so as long as client.available() returns non-zero, you are connected.
« Last Edit: April 10, 2012, 03:31:08 pm by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, that's exactly what I was thinking it did...  smiley-lol

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

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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!  smiley  I notice the code does have a fat 2000ms delay that has been used in older code.

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

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

   // rest of your setup
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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!  smiley  I notice the code does have a fat 2000ms delay that has been used in older code.

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!  smiley
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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!  smiley

Code:
#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");
}
« Last Edit: April 10, 2012, 11:21:30 pm by zoomkat » Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 11, 2012, 06:50:02 am by SurferTim » Logged

italy
Offline Offline
Full Member
***
Karma: 2
Posts: 215
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One question? How can we be sure that the program will never stop on the line
Code:
while(client.connected()

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One question? How can we be sure that the program will never stop on the line
Code:
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.
http://arduino.cc/forum/index.php/topic,102879.msg778413.html#msg778413
Logged

italy
Offline Offline
Full Member
***
Karma: 2
Posts: 215
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is sent on a line by itself after the GET, followed by a double CR-LF.
Code:
 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.
« Last Edit: May 28, 2012, 04:01:48 pm by SurferTim » Logged

Pages: 1 [2] 3   Go Up
Jump to: