Pages: 1 2 3 [4]   Go Down
Author Topic: Ethernet shield - webClient example  (Read 5539 times)
0 Members and 1 Guest are viewing this topic.
San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I came across Georg Kaindl's recent Arduino Ethernet libraries and gave a shot at using his EthernetDHCP library to get my WebClient sketch to work.

I get this output:
Code:
Discovering servers....................
Requesting lease.
Obtained lease!
My IP address is 192.168.2.3
Gateway IP address is 192.168.2.1
DNS IP address is 192.168.2.1
hostName is Arduino

connecting...
Trying client.connect() #1.....    connection failed
Trying client.connect() #2.....    connected
Trying client.connect() #3.....    connection failed
Trying client.connect() #4.....    connection failed
Trying client.connect() #5.....    connection failed
Trying client.connect() #6.....    connection failed
Trying client.connect() #7.....    connection failed
Trying client.connect() #8.....    connection failed
Trying client.connect() #9.....    connection failed
Trying client.connect() #10.....    connection failed

...when I run this code:
Code:
//  Copyright (C) 2010 Georg Kaindl
//  http://gkaindl.com
//
//  This file is part of Arduino EthernetDHCP.
//
//  EthernetDHCP is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  EthernetDHCP is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with EthernetDHCP. If not, see
//  <http://www.gnu.org/licenses/>.
//

//  Illustrates how to use EthernetDHCP in polling (non-blocking)
//  mode.

#include <Ethernet.h>
#include <EthernetDHCP.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 66, 102, 7, 147 }; // Google

const char* ip_to_str(const uint8_t*);

Client client(server, 80);



void setup()
{
  Serial.begin(9600);
  
  // Initiate a DHCP session. The argument is the MAC (hardware) address that
  // you want your Ethernet shield to use. The second argument enables polling
  // mode, which means that this call will not block like in the
  // SynchronousDHCP example, but will return immediately.
  // Within your loop(), you can then poll the DHCP library for its status,
  // finding out its state, so that you can tell when a lease has been
  // obtained. You can even find out when the library is in the process of
  // renewing your lease.
  EthernetDHCP.begin(mac, 1);
}

void loop()
{
  static DhcpState prevState = DhcpStateNone;
  static unsigned long prevTime = 0;
  
  // poll() queries the DHCP library for its current state (all possible values
  // are shown in the switch statement below). This way, you can find out if a
  // lease has been obtained or is in the process of being renewed, without
  // blocking your sketch. Therefore, you could display an error message or
  // something if a lease cannot be obtained within reasonable time.
  // Also, poll() will actually run the DHCP module, just like maintain(), so
  // you should call either of these two methods at least once within your
  // loop() section, or you risk losing your DHCP lease when it expires!
  DhcpState state = EthernetDHCP.poll();

  if (prevState != state) {
    Serial.println();

    switch (state) {
      case DhcpStateDiscovering:
        Serial.print("Discovering servers.");
        break;
      case DhcpStateRequesting:
        Serial.print("Requesting lease.");
        break;
      case DhcpStateRenewing:
        Serial.print("Renewing lease.");
        break;
      case DhcpStateLeased: {
        Serial.println("Obtained lease!");

        // 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.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));

        //Serial.println();
        
        delay(2000);
        
        Serial.print("hostName is ");
        Serial.println(EthernetDHCP.hostName());
        Serial.println();
        
        Serial.println("connecting...");
  
        for (int i = 0; i < 10; i++){
          Serial.print("Trying client.connect() #");  //added line
          Serial.print(i+1);
          Serial.print(".....  ");
          delay(3000);
          if (client.connect()) {
            Serial.println("  connected");
            client.println("GET /search?q=arduino HTTP/1.0");
            client.println();
          } else {
            Serial.println("  connection failed");
          }
        }
        
        break;
      }
    }
  } else if (state != DhcpStateLeased && millis() - prevTime > 300) {
     prevTime = millis();
     Serial.print('.');
  }

  prevState = state;
}

// 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;
}

Any guesses from the tubes as to why no output from the GET command? Any guesses as to why the second client.connect() outputs "connected" but not the others?

Many many thanks!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had a few issues getting the WebClient to work too via the Ethernet example. Haven't tried the DHCP sketch yet but will give that a go later.

Initially I connected via my BT HomeHub 1 (http://www.filesaveas.com/bthomehub.html), however when I connect....nothing.

One thing I did notice was that with all other devices laptops, phones etc. is that the router captures their IP and stores it automatically.

With the ethernet board connected I had to reuse/manually reconfigure an IP that already existed in the routers memory (the 'old' device was not connected) as the ethernet shield did not 'show' up when originally connecting. I could though ping the board no problem.

I then tried my trusty old BT 240 Voyager Router http://www.voyager.bt.com/wired_routers/voyager_240/product_info.htm, hooked up the ethernet board and connected straight away no problem.

So it looks like its down to the routers, their software(?) and how it manages the connections....bit of a pain though in having to swap routers just to get it working.....but at least it is!

G

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well if anyone is keeping score --

Quote
Assuming .1 gateway:  Works for the majority
Requiring a gateway: Works for everyone
---------------------------------------
Assuming a subnet:  Works for the majority
Requiring a subnet:  Works for everyone

I spent some time with wireshark to figure out the request was going out for a dot one gateway.  I'm new to the Ard and Ether shield and I learned more about the code than I wanted to.  The default idea of a dot one bit me so I'm siding up with the unpopular linux-works point of view.  I was thinking about rewriting the example code to prevent other newbies from falling into the same problem and then I found this thread while searching about DHCP.  So how can I help with the dot one gateway example code? I also found the google address is stale but that was easy to figure out.
Logged

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