Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
techshopjim
 
Posts: 14
Joined: Wed Mar 11, 2009 4:29 am

Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by techshopjim »

Hi Everyone!

I'm using a Duemilanove with an ADAFRUIT Ethernet Shield and the WizNet module.

The Arduino and shield sends requests to our web server and gets back the response.

Everything is working fine...most of the time.

About every 10th time the Arduino and Ethernet shield sends a query to the server, I see on the serial monitor "Connecting...connected". After about 20 seconds, it times out and stops.

The server is completely available during this time when I hit it from a browser.

Is there a way to decrease the TIMEOUT of the WizNet to 1 second, or modify the code to stop trying to query the server after 1 second?


Here is my sketch, taken directly from the examples:

Code: Select all

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 2 }; // Arduino's IP address
byte gateway[] = { 192, 168, 0, 1 }; // gateway
byte subnet[] = { 255, 255, 255, 0 };  // subnet
byte server[] = { 192, 168, 0, 1 }; // Test Web Site

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);

  delay(1000);

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

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /public/index.html?val1=5566&val2=4112 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
  
}
Thank you in advance!

--TechShopJim

User avatar
macegr
 
Posts: 293
Joined: Fri Apr 04, 2008 4:46 pm

Re: Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by macegr »

Can you narrow down the line where the long delay is taking place? For example, is it the println of the GET request (how about a serial message right after the GET request), or is it stuck in a loop where it's trying to read()? Maybe you're sending a request before the server's done sending you bytes...try putting a flush() in there after a problem maybe? I wonder if they client.available() result is defined while the device is disconnected...perhaps check !client.connected() inside the read loop? I've only used XPorts so this is all speculation.

User avatar
techshopjim
 
Posts: 14
Joined: Wed Mar 11, 2009 4:29 am

Re: Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by techshopjim »

Hi macegr...

The delay seems to be taking place between the 'Serial.println("connecting...");' and 'if (client.connect()) {' lines.

It doesn't even seem to enter the 'if (client.connect()) {' conditional block at all because it never executes the 'Serial.println("connected");' line.

After the delay of 20 to 25 seconds, it enters the '} else {' of the 'if (client.connect()) {' conditional block. What is causing this delay??? Is the 'client.connect()' in the top of the conditional block being executed and hanging?

I misspoke in my original post (which I frequently do). I mistakenly thought it had displayed the "connected" message but it does not. It displays the "disconnecting." message after the timeout of 20 to 25 seconds.

Do you think adding 'client.flush()' would help? It seems like the 'client.connect()' is causing the failure somehow, so I don't see how a 'client.flush()' would help because it would never get executed.

This is really frustrating. I have now tried all 4 of the major Ethernet shields (the original Arduino Ethernet shield with WizNet from TINKER.IT, the Ethernet shield from NuElectronics that uses the Microchip ENC28J60 SPI Ethernet controller, Lady Ada's Ethernet shield with XPort Direct v1.1, and Lady Ada's Ethernet shield with the WizNet module v1.2). They all have pros and cons. Some of them have great features like DHCP (the XPort version of Lady Ada's Ethernet shield). Some of them have widely-known problems which have plagued me too. But none of them seem to be very robust...at least in my experience. I have to make a whole bunch of these Arduino-based devices, and they need to be rock-solid at communicating with the server quickly and reliably. Maybe it is my code, but I'm just running the example code at this point and still having problems. Is there another hardware solution that I should consider using in order to allow the Arduino connect to the server?

Thank you!

User avatar
macegr
 
Posts: 293
Joined: Fri Apr 04, 2008 4:46 pm

Re: Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by macegr »

Here's an idea: maybe you're trying to connect when it's already connected. Web servers sometimes take a while to close a connection, some allow multiple requests per connection. Perhaps you can check client.connected() and only connect if not true?

There was also a known bug in 0013 using the Wiznet modules to do repeat connections to the same server. Apparently this was fixed on 0014. Lots of discussion here: http://www.arduino.cc/cgi-bin/yabb2/YaB ... 34006420/0

But the upshot is to make sure you have 0014. If you're using 0014 already, you might want to re-download it...possibly away from Techshop, I had problems with downloads I tried there.

I might be at Techshop today, maybe we could do some brainstorming.

User avatar
macegr
 
Posts: 293
Joined: Fri Apr 04, 2008 4:46 pm

Re: Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by macegr »

Also, if you need a whole bunch of them...if you're using them for what I'm thinking, the cost is going to mount up pretty high. Have you considered RS485 again? If you use NewSoftwareSerial with an 80 cent part now you can communicate over a network stretching thousands of yards over UTP cable. A simple modified RBB Arduino board with the RS485 driver and terminals, now we're talking a node cost of $20 at the most. Maybe you have an Arduino with Ethernet for each major area and then have it manage a group of RS485 devices.

User avatar
techshopjim
 
Posts: 14
Joined: Wed Mar 11, 2009 4:29 am

Re: Can the TIMEOUT Be Set with the WIZNET / Ethernet Shield?

Post by techshopjim »

Hi macegr...

I'll check the client.connected() ...that might be it!

I am using 0014 but I will re-download and reinstall. I wonder why downloading it at TechShop didn't work?

Maybe I should check into RS485...that might work out.

I'd love to brainstorm at TechShop today if you have a bit of time!

Thank you!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”