This simple code fails

Hi, I bought a Wiznet ethernet shield, and I'm trying to run this code:

#include <SPI.h>         // needed for Arduino versions later than 0018
#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[] = { 
  0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = { 192,168,1, 9 }; // ip in lan assigned to arduino
IPAddress dns1(192, 168, 1, 1);
IPAddress gg(192, 168, 1, 1);
IPAddress smask(255, 255, 255, 0);
unsigned int localPort = 12345;    

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; 



EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  //Ethernet.begin(mac, ip, gg, smask);
  Ethernet.begin(mac,ip);
  
  Udp.begin(localPort);
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
    
}

void abrir_pchica(void)
{
  digitalWrite(7, LOW);
  delay(500);
  digitalWrite(7, HIGH);
  return;
}

void abrir_pcorredera(void) //COREGIR
{
  digitalWrite(6, LOW);
  delay(1000);
  digitalWrite(6, HIGH);
  
  return;
}


void loop() {
  // if there's data available, read a packet
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    if(strcmp(packetBuffer,"a1")==0) abrir_pchica(); //PUERTA CHICA
    if(strcmp(packetBuffer,"a2")==0) abrir_pcorredera(); //PUERTA CORREDERA
  }
  
  delay(10);
}

Explanation: I'm giving an IP and MAC to the shield, but it doesn't work: although the shield has a IP, when I try to ping from my pc to the shield, it fails. The code is simple: it receives a message via UDP, and activates two relays.

The funny thing is that it works when I send a broadcast packet, but it fails when sending to the specific IP the shield has.

I have already tried to do the Ethernet.begin with only MAC argument, with MAC and IP, with DNS, gateway and subnet, I have also tried to begin with DHCP instead, nothing.

When I read the shield's IP trough the serial port, the IP is the one I gave, but the shield is invisible in the network. Help please.

when I try to ping from my pc to the shield, it fails.

The code is simple: it receives a message via UDP

Do you really mean this? A true ping program uses ICMP, not UDP (and of course, not TCP).

The funny thing is that it works when I send a broadcast packet

How are you sending the broadcast packet? A program that sends using UDP?

I made a simple pc program in VB to send broadcast.

What ethernet shield are you using? Does it have a w5100, w5200, or w5500 controller?

So your "simple pc program in VB to send broadcast" uses UDP and works, but ping uses ICMP and doesn't work?

No, what I want to say is that I want to send a UDP packet from WAN, so I open the ports of my router to lead packets from port 12345 to 192.168.1.9 (ethernet shield IP) but it does not work.

The Wiznet is the 5100 one.

Can you access it from the LAN? Are you certain the localnet gateway is 192.168.1.1?

I can access from LAN, sending the UDP packet broadcast. Gateway is 192.168.1.1

Compile and run this code. Does it display "failed" or 2 IP addresses?

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

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

  // disable SD SPI
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
  }
}

void loop() {
}

SurferTim:
Compile and run this code. Does it display "failed" or 2 IP addresses?

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

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

// disable SD SPI
  digitalWrite(4,HIGH);

Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
  }
}

void loop() {
}

It gets stuck at "Starting ethernet" xD

Edit: nope, it failed.

Your router doesn't have a DHCP server?

Yes and it is configured.

Edit: I forgot to say that there is a switcher between arduino and the router, but I don't think it makes a difference. And why are you asking about the DHCP? I tried to set a static IP and it didn't work.

Then you have a problem somewhere.

Try this code as a test. Does it display 192.168.0.2 on the serial monitor?

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

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

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

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

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

  Serial.println(Ethernet.localIP());
}

void loop() {
}

SurferTim:
Then you have a problem somewhere.

Try this code as a test. Does it display 192.168.0.2 on the serial monitor?

Yes, but as I said, when I set a static IP and ask arduino to write it trough serial port, the IP it writes is the one I gave.

Then you have a failure of the w5100 IC on the RJ45 side. Have you checked everything? CAT5 cable ok? LEDs lit?

SurferTim:
Then you have a failure of the w5100 IC on the RJ45 side. Have you checked everything? CAT5 cable ok? LEDs lit?

Cable is OK, I changed three times, LEDs seem normal to me, and about the switcher, I bought it today. This is so strange, I set any static IP, but it's like there isn't any device with that IP in the network.

neukyhm:
No, what I want to say is that I want to send a UDP packet from WAN, so I open the ports of my router to lead packets from port 12345 to 192.168.1.9 (ethernet shield IP) but it does not work.

On my home router, that kind of bridging applies to packets coming into the router from the outside world. From the POV of the outside world, my ip adress is . When you connect from the outside world to :12345, I can send that though to a port on my local network.

But this has got nothing to do with interconnections inside my local network.

To check that port forwarding is ok, I turn off the wi-fi on my iPhone and connect to :12345 with the web browser. This goes out to the Telstra network and into my home setup from outside, and so the port forwarding happens.

Switcher? You mean a switch? Do you have the ethernet shield connected directly to the router, or is there a switch in between the two?

SurferTim:
Switcher? You mean a switch? Do you have the ethernet shield connected directly to the router, or is there a switch in between the two?

Sorry, in my country we know those devices as switchers haha. Yes, it's a switch.

I found the problem, the problem is that arduino doesn't like that switch. I tried to connect the shield directly to the router that is also the DHCP server, everything worked, but it is interesting that a simple switch was the problem.