Pages: [1] 2   Go Down
Author Topic: Ethernet Shield Not Connecting  (Read 3222 times)
0 Members and 1 Guest are viewing this topic.
Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been working this problem for the past several days and getting nowhere. This issue has come up before in the forum but none of the solutions are working for me. I have an ethernet shield mounted on an UNO. The micro-SD card section works fine but the ethernet section is another story. It is unable to establish a connection in the program below. The output from the program is:

Better client test 4/04/12
Send an e in serial monitor to test
read an 'e'
connection failed

disconnecting.
================== 

I ran IPCONFIG to determine the correct addresses.
The "byte dns[]" line has been commented out because it produces the following error:
   "'byte dns []' redeclared as different kind of symbol"
although I've read that this line may not be necessary.
Also, there was no MAC address sticker on the ethernet card but I understand that this may also not be necessary.
Would appreciate any help you can offer on this.

Code:
//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
//the arduino lan IP address { 192, 168, 1, 102 } may need
//to be modified modified to work with your router.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; //physical mac address
byte ip[] = {
  192, 168, 1, 105 }; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 1, 1}; // internet access via router
//byte dns[] = {  192, 168, 1, 1 }; //use router ip
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = {
//  208, 104, 2, 86 }; // zoomkat web page server IP address
    209, 191, 122, 70 }; // yahoo
EthernetClient client;
//////////////////////

void setup(){

  pinMode(10, OUTPUT);//sets w5100chip
//  digitalWrite(10,HIGH); // disables w5100
  pinMode(4, OUTPUT); // sets SD chip
  digitalWrite(4,HIGH);//disables SD chip
 
  //Ethernet.begin(mac, ip);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Serial.begin(9600);
 
  Serial.println("Better client test 4/04/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
    {
      Serial.println("read 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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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
// add counter as an attempt to bail out if too much input comes in
//
  int counter = 0;
  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
    counter = counter + 1;
    if (counter >= 500 ) {
      exit; 
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In that code you have the myServer set to "209, 191, 122, 70" which appears to be a yahoo IP, but doesn't resolve correctly as-is.  Are you trying to access a web site (virtual host), that is hosted on a yahoo server?  If so, you need to add the http-header "host".  E.g.

client.println("Host: example.com");
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, I'm just trying to verify that my ethernet card is working and able to connect the network. Right now it appears that the only connection is via the USB cable and not the network cable.

I'm not sure how I would fit the code you suggested into the program. I'm hesitant to make any change to the code in this area as others in the forum have got it working as is. I did change the Yahoo address to a valid one, 209.191.122.60, but still no luck.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you verified that your local network's subnet is 192.168.1.x?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was successfully able to ping 192.168.1.1 and 192.168.1.105.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like there is a problem with your Shield, the Cable, or your router.  I uploaded your code as-is in your original post.  The only thing I changed was the subnet. 

Here's the response I got:
Code:
read an 'e'
connected
HTTP/1.0 302 Redirect
Date: Sat, 07 Jul 2012 01:36:45 GMT
Connection: close
Server: YTS/1.20.10
Cache-Control: no-store
Content-Type: text/html
Content-Language: en
Location: http://failsafe.fp.yahoo.com/404.html
Content-Length: 227

<HEAD><TITLE>Redirect</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica,Arial"><B>
 "<em>http://failsafe.fp.yahoo.com/404.html</em>".<p></B></FONT>

<!-- default "Redirect" response (302) -->
</BODY>
(strange character repeated here, but breaking up post)
disconnecting.
==================

This is the same behavior I saw with my browser.

For the record, the is the code I used:
Code:
//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
//the arduino lan IP address { 192, 168, 1, 102 } may need
//to be modified modified to work with your router.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; //physical mac address
byte ip[] = {
  10, 0, 1, 105 }; // ip in lan assigned to arduino
byte gateway[] = { 10, 0, 1, 1}; // internet access via router
//byte dns[] = {  192, 168, 1, 1 }; //use router ip
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = {
//  208, 104, 2, 86 }; // zoomkat web page server IP address
    209, 191, 122, 70 }; // yahoo
EthernetClient client;
//////////////////////

void setup(){

  pinMode(10, OUTPUT);//sets w5100chip
//  digitalWrite(10,HIGH); // disables w5100
  pinMode(4, OUTPUT); // sets SD chip
  digitalWrite(4,HIGH);//disables SD chip
 
  //Ethernet.begin(mac, ip);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Serial.begin(9600);
 
  Serial.println("Better client test 4/04/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
    {
      Serial.println("read 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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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
// add counter as an attempt to bail out if too much input comes in
//
  int counter = 0;
  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
    counter = counter + 1;
    if (counter >= 500 ) {
      exit; 
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

James, thanks for testing this.
I replaced the cable but it's still not working. I'll order another ethernet shield and hopefully have better luck.
Lawren5
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9636
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some simple server test code you can try to see if it works with your ethernet shield.

Code:
// zoomkat meta refresh server test code
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

#include <SPI.h>
#include <Ethernet.h>

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test IDE 1.0");
          client.println("<br />");
                   
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("<br />");
          client.println("<br />");
         
          client.print("Zoomkat's arduino analog input values:");
          client.println("<br />");
          client.println("<br />");
         
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Logged

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

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried running the zoomkat program but nothing happened. One thing I did notice was that each time I loaded the program, a message appeared for about two seconds saying "Local Area Connection, A network cable is unplugged".
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Where are you plugging in the other end of the cable?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Into the network port of my laptop.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Into the network port of my laptop.
And is your laptop setup to do internet connection sharing?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9636
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Into the network port of my laptop.

You may need an ethernet crossover cable to make a connection directly to your laptop.
Logged

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

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, it was not set up for internet sharing so I made the change. However, the program is still not working and continues to give the "unplugged" message.

I'm not familiar with an ethernet crossover cable is. My laptop is an older model running XP if that indicates anything.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, it was not set up for internet sharing so I made the change. However, the program is still not working and continues to give the "unplugged" message.
Well, your next step is to modify the IP settings for the Ethernet Shield based on whatever the ICS is going to provide.  You aren't getting your internet from your router, you are getting it from your laptop.  The best thing to do would be to plug the Ethernet shield directly into a router.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Pages: [1] 2   Go Up
Jump to: