Transfer from uno+eth shield to leonardo eth

Hi
I’m trying to make a WOL device that with a press of a button wakes several computers. I found a sketch that almost did the job and just added some extra magic packets and the button function. Works very well on my arduino uno with an ethernet shield. I would like to get it to work on a leonardo eth due to the price and physical space. However it doesn’t work.
First issue was the seriel monitor not working but it was fixed by adding ( while (!Serial) ; ) in void setup. But it won’t send anything. Could it be that the librarys that isn’t compatible with leonardo eth or? I would really appreciate if somebody could point a complete noob like me in the right direction.

#include <SPI.h>
#include <Ethernet.h>
#include <Utility\Socket.h>

// Make up a mac Address & an IP address. Both should be globally unique or
// at least unique on the local network. 
static byte g_abyMyMacAddress[] = {0x90,0xA2,0xDA,0x11,0x3A,0xB8};
static IPAddress g_MyIPAddress(192,168,0,10);

// The machine to wake up. WOL should already be configured for the target machine. 
static byte g_TargetMacAddress[] = {0x00,0x24,0x7E,0x12,0x1A,0x8D};
static byte g_TargetMacAddress1[] = {0x00,0x24,0x7E,0x12,0x1A,0x22};
static byte g_TargetMacAddress2[] = {0x00,0x24,0x7E,0x12,0x1A,0x33};
static byte g_TargetMacAddress3[] = {0x00,0x24,0x7E,0x12,0x1A,0x44};
static byte g_TargetMacAddress4[] = {0x00,0x24,0x7E,0x12,0x1A,0x55};
static byte g_TargetMacAddress5[] = {0x00,0x24,0x7E,0x12,0x1A,0x66};
static byte g_TargetMacAddress6[] = {0x00,0x24,0x7E,0x12,0x1A,0x77};

const int buttonPin = 2; //number of keyswitch pin

int buttonState = 0;
bool hasSendData = false;



void setup () 
{
  Ethernet.begin(g_abyMyMacAddress, g_MyIPAddress);

  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  while (!Serial) ;
  Serial.println("Wake on Lan (WOL) Demo");
  Serial.println("Send 'Waiting for button to be pressed' to initiate WOL"); 
}
 
void loop() 
{
 
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH && hasSendData == false)
  {
    Serial.println("Magic packets sent");
    delay(50);
    SendWOLMagicPacket(g_TargetMacAddress);
    SendWOLMagicPacket(g_TargetMacAddress1);
    SendWOLMagicPacket(g_TargetMacAddress2);
    SendWOLMagicPacket(g_TargetMacAddress3);
    SendWOLMagicPacket(g_TargetMacAddress4);
    SendWOLMagicPacket(g_TargetMacAddress5);
    SendWOLMagicPacket(g_TargetMacAddress6);
    Serial.println("Magic packets sent");
    hasSendData = true;
  }

  if(buttonState == LOW)
  {
    hasSendData = false;
  }
}

void SendWOLMagicPacket(byte * pMacAddress)
{
  // The magic packet data sent to wake the remote machine. Target machine's
  // MAC address will be composited in here.
  const int nMagicPacketLength = 102;
  byte abyMagicPacket[nMagicPacketLength] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  byte abyTargetIPAddress[] = { 255, 255, 255, 255 }; // don't seem to need a real ip address.
  const int nWOLPort = 7;
  const int nLocalPort = 8888; // to "listen" on (only needed to initialize udp)

  
  // Compose magic packet to wake remote machine. 
  for (int ix=6; ix<102; ix++)
    abyMagicPacket[ix]=pMacAddress[ix%6];
  
  if (UDP_RawSendto(abyMagicPacket, nMagicPacketLength, nLocalPort, 
  abyTargetIPAddress, nWOLPort) != nMagicPacketLength)
    Serial.println("Error sending WOL packet");
}

int UDP_RawSendto(byte* pDataPacket, int nPacketLength, int nLocalPort, byte* pRemoteIP, int nRemotePort)
{
      Serial.println("Seending ..");

  int nResult;
  int nSocketId; // Socket ID for Wiz5100

  // Find a free socket id.
  nSocketId = MAX_SOCK_NUM;
  for (int i = 0; i < MAX_SOCK_NUM; i++) 
  {
    uint8_t s = W5100.readSnSR(i);
    if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) 
	  {
      nSocketId = i;
      break;
    }
  }

  Serial.println("Socket .." + String(nSocketId));

  if (nSocketId == MAX_SOCK_NUM)
    return 0; // couldn't find one. 

  if (socket(nSocketId, SnMR::UDP, nLocalPort, 0))
  {
    Serial.println("created " );
    nResult = sendto(nSocketId,(unsigned char*)pDataPacket,nPacketLength,(unsigned char*)pRemoteIP,nRemotePort);
    close(nSocketId);
  } else
    nResult = 0;

  return nResult;
}

You need to use the Ethernet2 library with the Leonardo ETH. The Ethernet library is not compatible with that board.

"The Leonardo ETH has the same Wiznet Ethernet interface as the Ethernet Shield 2."

"Ethernet library (Ethernet.h) manages the W5100 chip, while Ethernet2 library (Ethernet2.h) manages the W5500 chip;"

So change "Ethernet.h" to "Ethernet2.h".

Thanks guys
I guess that was the first step. Much appreciated.
After change to ethernet2.h I get a compile error:
Arduino: 1.8.5 (Windows 7), Board: "Arduino Leonardo ETH"

C:\Users\lak\Documents\Arduino\wol\WOL-leonardo\WOL1\WOL1\WOL1.ino: In function 'int UDP_RawSendto(byte*, int, int, byte*, int)':

WOL1:95: error: 'W5100' was not declared in this scope

uint8_t s = W5100.readSnSR(i);

^

exit status 1
'W5100' was not declared in this scope

I have tried to change w5100 to w5500 but I still get the same error but with w5500 instead.

Any ideas?

best regards
lak

Hi

Solved the problem by #include <utility\w5500.h> in the top and change W5100 to w5500 in the line:
uint8_t s = w5500.readSnSR(i);

Ty guys for the help :o)

best regards

Lak