Go Down

Topic: Need help troubleshooting ethernet shield (simple client script) (Read 866 times) previous topic - next topic

I have an Arduino uno and the W5100 Ethernet shield.
I cannot seem to get it to work properly. Below is my code, it's output and my troubleshooting.

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {   172,16,200,57 };
byte nameserver[] = { 8,8,8,8 };
byte gateway[] = {  172,16,200,2};
byte subnet[] = {  255,255,255,0};
byte server[] = { 72,125,239,9 };

EthernetClient client;

void setup() {
  // start the Ethernet connection:
    Ethernet.begin(mac, ip, nameserver, gateway, subnet);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server,80)>0) {
    Serial.println("connected");
    client.println("GET");
    client.println();
  }
  else {
    Serial.println(client.status());
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}


And my results are:

Code: [Select]
connecting...
0
connection failed

disconnecting.


I have attempted to connect it strait to my PC and get wireshark to listen to all traffic. My arduino is just not even attempting to send a single packet out. I don't see any attempts whatsoever at trying to reach google or anything.
What troubleshooting shooting steps can I take? I have confirmed the IP's and all are correct. Can I see what ether.begin() retuns? Can I see why client.connect() fails? I'm not sure what else I can do to find the problem. Any help out there?

zoomkat

Client test code. This code needs the arduino be connected to a router and not a pc.

Code: [Select]

//zoomkat 9-22-12
//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(){

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

}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

#2
Sep 24, 2012, 06:56 am Last Edit: Sep 24, 2012, 07:31 am by yumology Reason: 1
zoomkat,
Absolutely nothing is displayed in the serial output. I figured out that is probably because Serial.begin is below the first println, so I moved it up. I still got no response, so I added a simple 'initializing' string to the print which does in fact show up, but I only see that as the output. I then took out the while(true) statement thinking it was causing and issue but still the only thing on my screen is "initalizing". See my updated code here:

Code: [Select]
void setup(){
 Serial.begin(9600);
 Serial.println("initializing");
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:
   //while(true);
 }


A few added notes. I am in Arduino 1.0.1. I have tested my DHCP port by plugging a laptop in and getting an IP/gateway/DNS and it works perfect.
From the looks of it, the Ethernet.begin() function is not working and hanging forever. What can I do to investigate this function further?

SurferTim

Do you have a memory card in the microSD slot?
Are the ethernet leds on the shield and the router indicating a connection?

The lights do turn on at the arduino on the ethernet shield. Power, 100M, link and fullD lights are all on. At the router the lights is on too and I can even see that the port negotiated at 100/Full and the status of the port is up.
I do not have a memory card in this setup. I simply have an arduino uno with an ethernet shield sitting right on top of it. Then I have a USB cable for power connected to my laptop and an ethernet connection to my router.
I have also tried 2 different routers and different cables and different ports. None work for the arduino, yet my laptop can get DHCP just fine on any of those cables/ports/routers.

SurferTim

Insure all shield pins are inserted fully into the Uno. The ICSP pins are important. That is the SPI lines between the two devices.

Try this sketch. You can change the ip to fit your subnet, but for this test it really doesn't matter.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

void setup() {
  Serial.begin(9600);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);
  Serial.println(Ethernet.localIP());
}

void loop() {
}

If the serial monitor shows the ip as 192.168.2.2, the Uno/w5100 SPI link is ok. If it shows 0.0.0.0, that is not ok.

FIXED!
Thanks surfertim! Because the two boards don't fit together well I added some headers between them so they fit better. Because of this the ICSP pins were not connected to each other at all. Now I have a working, but ugly project because the two boards don't fit on top of each other just right. The USB socket is too tall for the ethernet shield to fit flush.

Go Up