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:
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:
// 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!