Connecting Due R3 to Ethernet Shield R3

Hello everybody!

I am having the following problem – I am trying to use a Due together with an ethernet shield, but no luck.

Everything works fine when I use the said ethernet shield with an Uno. The following code:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,101,177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:

}

Runs just fine and returns the correct IP address over the serial console. When I try to run the same thing on the Due, I get 0.0.0.0 as the IP address of the server, which clearly means things are not working correctly.

Could this be because of insufficient power supplied to the board or is there something else that I am missing? I have a DC power supply lying around that does 5V, but I think that’s not compatible, right?

Please let me know if any additional information is required. Thank you all in advance!

Nobody has an idea?

I will guess. The SPI is different for the Due. You must specify the slave select pin. Try this addition to your setup. No guarantees tho.

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

// add this
  SPI.begin(10);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

Hi SurferTim, thanks for the advice.

Unfortunately that did not help. However, what helped was changing the way I program the device – I went from the programming port to the native port and things started to work as expected. However, I lost support for the serial console – I no longer get any messages. I am guessing there is some fundamental gap in my understanding of how things work. Could you recommend somewhere where I can read up on this, or better yet quickly explain?

Thanks!

UPDATE:
Things work well as long as I try not to use the serial console. If I use Serial, but don’t connect to it – things work. If I use SerialUSB, I can ping the board, but nothing else (I am trying to run the UDP message example). For completeness here is my code (if you either comment out the SerialUSB commands, or switch them over to Serial and not connect over the serial console the functionality is there):

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,101,177);

unsigned int localPort =  8888; // local listen port

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer for incoming packet
char replyBuffer[] = "acknowledged"; //buffer for outgoing packet

EthernetUDP Udp; //instantiate UDP

void setup() {
  // put your setup code here, to run once:
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  
  //SPI.begin(10);
  
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  SerialUSB.begin(9600);
  while (!SerialUSB) ;
  SerialUSB.print("Server is on: ");
  SerialUSB.println(Ethernet.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    SerialUSB.print("Received packet of size: ");
    SerialUSB.println(packetSize);
    SerialUSB.print("From: ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      SerialUSB.print(remote[i], DEC);
      if (i<3)
      {
          SerialUSB.print(".");
      }
    }
    SerialUSB.print(", port ");
    SerialUSB.println(Udp.remotePort());
  
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    SerialUSB.println("Contents: ");
    SerialUSB.println(packetBuffer);
    
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyBuffer);
    Udp.endPacket();
  }
}

Did you solve your issue?

I had similar problems. By accident I added the code

  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

after the ethernet and udp initialization. For me this worked great - I don't know why.

Would you like to give it a try?

Oliver