Zoomkat,
First of all, thank you, (and everyone else for that matter), for the excellent code.
I believe I have found a small bug:
In your demo code, you wrote:
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600); <<--- In the wrong place?
Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
You place the Serial.begin statement just prior to where you print your banner title.
Shouldn't it be placed just after the declaration of the "setup" method? AFAIK, placed after the Ethernet.begin reference, the error code within it cannot print. Right?
I had some fun with your code, moved that statement around, snooped out the Ethernet.cpp file, snipped some code from the "DhcpAddressPrinter" Ethernet example. . . . and added some code to give the user some feedback, and display the characteristics of his connection before it moves on to your test.
Here it is:
//zoomkat 9-22-12
//Updated 09/29/2012 by Jim Harris (JR) to provide a "confidence" message and IP info after connect
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;
//////////////////////
void setup(){
/*
Added by JR on 9/29/2012
Moved Serial.begin so that it appears prior to any serial message reference.
Added startup confidence message - as the sketch appears to hang when seeking an IP address.
*/
Serial.begin(9600);
Serial.println("Attempting to retrieve an IP address via DHCP\n");
// Added a "timing" message to help manage the users expectations
Serial.println("This might take a little while, so why not go and");
Serial.println("get a cuppa coffee while you wait\n");
// end add =========================================================
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
/*
Added by JR on 09/29/2012
Added an "answer" to the "acquiring" message printed above.
*/
Serial.println("Ahhh . . Got one!");
/*
Here I snipped the central code block from the
"DhcpAddressPrinter" example found within the Ethernet library
folder, duplicated it four times, scrounged the Ethernet source,
and added the display of the IP address obtained, the subnet mask,
the gateway IP address, and the address of the assigned DNS server.
*/
// print your local IP address:
Serial.print("My IP address is: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.print("\n"); // Added to space the various entries.
// print your local subnet mask:
Serial.print("My Subnet Mask is: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.subnetMask()[thisByte], DEC);
Serial.print(".");
}
Serial.print("\n");
// print your local gatewaay:
Serial.print("My Local Gateway is: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.gatewayIP()[thisByte], DEC);
Serial.print(".");
}
Serial.print("\n");
// print your local DNS server IP:
Serial.print("My Local DNS Server is: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.dnsServerIP()[thisByte], DEC);
Serial.print(".");
}
Serial.print("\n\n"); // Added to provide spacing from my text and zoomkat's original text.
// End add ===============================================================
Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
client.println("Host: web.comporium.net");
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
Take a look and critique it.
Feel free to mess with it as I have done.
A question:
On my Windows 7 system (that I am using as the development system), ipconfig returns the following information about my hard-wired Ethernet connection:
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : vgorilla.com
Description . . . . . . . . . . . : Realtek PCIe FE Family Controller
Physical Address. . . . . . . . . : 00-1E-33-FB-62-21
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 172.31.100.100(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, September 30, 2012 4:52:01 PM
Lease Expires . . . . . . . . . . : Monday, October 01, 2012 7:44:37 PM
Default Gateway . . . . . . . . . : 172.31.100.1
DHCP Server . . . . . . . . . . . : 172.31.100.1
DNS Servers . . . . . . . . . . . : 172.16.250.1
66.189.0.100
24.159.64.23
24.247.24.53
NetBIOS over Tcpip. . . . . . . . : Enabled
Note that there are three external DNS servers returned by the DHCP configuration request, (the first one is hard-coded so that I can use a particular VPN connection more easily.)
When I run my updated code and display the DNS IP address, it only retrieves and displays ONE DNS IP address. (The 66.189.0.100 address)
Even if I increase the loop count for gathering the DNS IP address to 8, it still displays only the one. The second comes back as 194.0.0.0
Viz.:
Attempting to retrieve an IP address via DHCP. . . .
Ahh . . Got one!
My IP address is: 172.31.100.104.
My Subnet Mask is: 255.255.255.0.
My Local Gateway is: 172.31.100.1.
My Local DNS Server is: 66.189.0.100.194.0.0.0. <-- yes, I know the formatting stinks
Better client test 9/22/12
Send an e in serial monitor to test
Why is that? Can it retrieve more than one DNS address? Would it even be useful in the Arduino context?
I have also been having fun with the serial library and a 16x2 LCD display. When I get time, I'll see if I can get this thing to print to the external display instead of the built-in serial terminal.
What say ye?
Jim (JR)