Ethernet Shield 5100

Hi All,
I’m using an Arduino MEGA 256 with Ethernet shield 5100 on it.
I’m a newbie to Arduino so I did everything step by step, tried to blink a led to set dhcp to transmit udp.
Basically everything worked as it should.
Now that Im trying to integrate things together I’m bumping into problems.

what I’m experiencing is when ever I set the command : Ethernet.begin(mac) ; , I’m loosing the ability to perform a digitalwrite to different IOs.
when I’m remarking this line, everything works.
I tried writing to different IOs ending with the same problem. tried to update the ethernet library and the problem remained.

the following is a code that suffers from the problem.

Please help,

Miki

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <IRremote.h>

IRsend irsend;
unsigned char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 50;
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 1234; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// Open serial communications and wait for port to open:
// Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac) ;
// if (Ethernet.begin(mac) == 0) {
// Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
// for(;:wink:
// ;
// }
// print your local IP address:
// Serial.print(“My IP address: “);
// for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
// Serial.print(Ethernet.localIP()[thisByte], DEC);
// Serial.print(”.”);
// }
// Serial.println();
// start the UDP:
// Udp.begin(localPort);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Can't use D50 on a Mega with the ethernet shield. That is one of the SPI data pins (MISO). Use another pin besides D50 to D53. D49 should be ok.

int led = 50;

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

tried writing to D49 with no luck!

the thing is that I tried many different pins and it looks like it doesn't write,
when removing the ethernet.begin(mac); it starts working again.

miki

Try this. Does it show the IP or failed? It may take a couple minutes to show the failed.

  // change this
   Ethernet.begin(mac) ; 

  // to this
   while(Ethernet.begin(mac) == 0) {
     Serial.println("Failed to configure Ethernet using DHCP");
     delay(5000);
   }

   // print your local IP address:
   Serial.print("My IP address: ");
   Serial.println(Ethernet.localIP());

edit: If you have a SD card in the shield's slot, remove it for this test.

Hi Tim,

First Many thanks for your support.
I tried running your code, after 10 minutes the terminal was still empty.
I put your code in remark and took mine of the remark and got the same result.

went back to my original code, and I see I dhcp is working and I got a valid ip address but still unable to write.

Shai

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

IRsend irsend;

// 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 };
unsigned int localPort = 1234; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

unsigned char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
unsigned char btohexa_high(unsigned char b)
{
b >>= 4;
// return (b>0x9u) ? b+‘A’-10:b+‘0’;
return b;
}
//Converts the lower nibble of a binary value to a hexadecimal ASCII byte.
// For example, btohexa_low(0xAE) will return ‘E’.
unsigned char btohexa_low(unsigned char b)
{
b &= 0x0F;
// return (b>9u) ? b+‘A’-10:b+‘0’;
return b;
}

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for(;:wink:
;
}
// print your local IP address:
Serial.print(“My IP address: “);
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(”.”);
}
Serial.println();
// start the UDP:
Udp.begin(localPort);
}

void loop() {

int packetSize = Udp.parsePacket();
char buffer;

if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote*, DEC);*

  • if (i < 3)*
  • {*
  • Serial.print(".");*
  • }*
  • }*
  • Serial.print(", port ");*
  • Serial.println(Udp.remotePort());*
  • // read the packet into packetBufffer*
  • Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);*
  • Serial.println(“Contents:”);*
  • for(int i=0;i< packetSize; i++){*
    _ //Serial.print(packetBuffer*,HEX);_
    Serial.print(btohexa_high(packetBuffer),HEX);
    Serial.print(btohexa_low(packetBuffer),HEX);
    _ }
    Serial.println(“SHAI”);
    Serial.println(sizeof(char),DEC);
    irsend.sendRaw((unsigned int)packetBuffer,packetSize,38);

    * delay(100);
    }
    }*_

Hi Tim,

My mistake forgot to open the serial port.

I tested your code and I immediately get an IP address.

Shai