udp.endPacket() returns zero - but works fine after hardware reset

Bit of a weird one, but I've not been able to find an answer. Hopefully someone here can help me.

I'm using an Arduino Uno and a mega compatible ethernet shield. Code is straight out of the examples.

Everything works fine when the Arduino is connected to my PC.

However, on standalone power udp.endPacket() returns 0.

If I press the reset button on the ethernet shield, it works perfectly! Unfortunately for this project I won't have easy access to the shield, so I need to find a solution that gets it working on first boot.

Things I've tried: Moving the UDP.Write section to loop(). Adding long (30 second) delays between each line of code. Slamming my head on the desk.

Abridged code here:

void setup ()
{
  initLEDs(); //I have LEDs which allow me to error check without using Serial

  Ethernet.begin(mac, ip, dns, sub);

  delay(500);

  if (udp.begin(PORT) == 0)
  {
    setLEDs(255, 0, 255); //magenta
    while (true) delay(1); //do nothing
  }

  delay(500);

  if (udp.beginPacket(server, PORT) == 0)
  {
    setLEDs(255, 0, 0); //red
    while (true) delay(1); //do nothing
  }

  udp.write(MSG);

  if (udp.endPacket() == 0)
  {
    setLEDs(255, 128, 0); //orange
    while (true) delay(1); //do nothing
  }
}

void loop ()
{
  //udp.read and a bunch of other stuff
  delay(10);
}

As I said above, this code works fine after a reset, so I don't think it's a hardware issue. Feels more like a bug that I can't think how to solve.

All advice welcome :slight_smile:

Abridged code here:

Bad idea. Post exactly the code you upload which shows that behavior. Post a link to the hardware you're using (unless it's an original Arduino Ethernet Shield).
From your description my guess is a hardware that doesn't reset correctly at power up. The hardware I use doesn't show that behavior, so a link to the one you're using is mandatory.

Thanks for your reply.

Unfortunately I really don't know the make of the hardware. It's literally labelled "mega compatible". I got it on eBay.

However, I was able to track down the problem and it was as you suspected.

That website gave me the solution which worked perfectly. Hope it helps anyone else who comes across this post.