3
votes

I have been playing around with an arduino ethernet shield, trying to get basic examples to work, to no avail. Here is my setup:

The Arduino Mega 2560 is connected to the computer via usb and the ethernet shield is stacked upon it. I have tried many variations of the examples that come with the arduino software, and none seemed to work properly.After lots of debugging with wireshark, I figured that:

  • I can't use DHCP, because it just hangs at the Ethernet.begin(mac) call.

  • When I try with a static ip, the Ethernet.localIP() function returns 0.0.0.0. However, I can ping my device from my computer using the ip I have set and the device seems to receive and send packets properly.The problem now is that for some reason it drops the tcp connections.E.g here is the code I run that comes the closest to working:

    #include <SPI.h>
    #include <Ethernet.h>
    
    
    byte mac[] = {  
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192,168,2,27);
    
    IPAddress server(192,168,2,52); 
    
    EthernetClient client;
    
    void setup() {
      // start the Ethernet connection:
      Ethernet.begin(mac, ip);
     // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
      Serial.println("a");
    
    
    
      delay(1000);
      Serial.println("connecting...");
    
    
      if (client.connect(server, 23)) {
        Serial.println("connected");
      } 
      else {
    
        Serial.println("connection failed");
      }
      Serial.println(Ethernet.localIP());
    }
    
    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);
      }
    
      // as long as there are bytes in the serial queue,
      // read them and send them out the socket if it's open:
      while (Serial.available() > 0) {
        char inChar = Serial.read();
        if (client.connected()) {
          client.print(inChar); 
        }
      }
    
      // if the server's disconnected, stop the client:
      if (!client.connected()) {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
        // do nothing:
        while(true);
      }
    }
    

Its basically the Ethernet/TelnetClient example. I have set up a telnet server on my computer. Now this is the arduino/computer exchange: screenshot

The arduino sends a RST packet, but my server goes on to send it the greeting and login prompt. I have tried the same with an arduino uno, and have also tried disconnecting the usb and using another power supply. So, what could be the issue?

2
Have you tried the basic DHCP Address Printer from the Arduino examples?Manos
yes, and as I have explained in my question, it will hang in the Ethernet.begin(mac) call forever.byrondrossos

2 Answers

0
votes

The problem is in the connection to the shield, sometimes this shield if it is chinese version, It might have small short-circuit.

I tried disconnect the shield and connect with wire as like as arduino website indicate for arduino uno (the connections with arduino mega aren't correct, so you need connect like arduino uno)

If it don't work , try change the arduino shield. I have a similar problem with the same shield , and normally the problem is the connection to the arduino. The example should work if the arduino is correctly connected

0
votes

Strangely, I found that Ethernet fails to initialize if an SD card is inserted and not initialized. So, either take the SD card out, or initialize the SD card:

Sd2Card card;
card.init(speed, pinSelect);

I have some Chinese (SunFounder) version of Ethernet Shield, so it might not be relevant to cards from other manufacturers.