1

I am currently trying to get the Ethernet Shield working on my Mega. I was trying to run the Webserver example but the program seems to stuck at one point, so I tried to start from scratch.

This is my test code:

#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
 0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x3D
};
byte subnet[] = { 255,0,0,0 };
byte gateway[] = { 2,0,0,1 };
IPAddress ip(2, 0, 0, 1);
EthernetServer server(80);
void setup() {
 Serial.begin(9600);
 Ethernet.begin(mac, ip, gateway, subnet);
 Serial.println("Ethernet started");
 server.begin();
 Serial.println("Server started");
}
void loop() {
 // put your main code here, to run repeatedly:
 Serial.println("Loop");
}

The output I get from the serial console is:

Etrted
Ethernet started

So I think the program gets stuck inside the EthernetServer::begin() function. I am aware that there are earlier versions of ethernet shields which are not compatible to the mega, but the vendor of my shield says it is.

Also I don't understand, why it outputs the first line.

Thanks for your hints!

asked Jun 5, 2015 at 12:53
2
  • Ok, I had to make some hardware changes to fit the pin layout. Now the code works but the interface does not get the desired IP adress. Instead of putting out the right IP, the serial console only prints 255.255.255.255 or 0.0.0.0 randomly. Any hints on that? Commented Jun 8, 2015 at 21:06
  • I now know that 0.0.0.0 occurs when pin 53 (SS, actual pin 10 on ethernet shield) is set to LOW and 255.255.255.255 occurs when pin 53 is set to HIGH (disable ethernet). In all cases, pin 4 (SD_CS) is set to HIGH to disable the SD card reader. Commented Jun 8, 2015 at 23:23

3 Answers 3

3

You must use these libraries:

#include <Ethernet.h> --> #include <Ethernet2.h>
#include <EthernetUdp.h> --> #include <EthernetUdp2.h>

More info at this link Arduino Ethernet Shield2

answered Aug 3, 2015 at 13:55
0
// fill in your Domain Name Server address here:
IPAddress myDns(192,168,1,1);
// initialize the library instance:
EthernetClient client;
char server[] = "www.arduino.cc";
unsigned long lastConnectionTime = 0;
// last time you connected to the server, in milliseconds
boolean lastConnected = false;
// state of the connection last time through the main loop
const unsigned long postingInterval = 14*1000;
// delay between updates, in milliseconds
void setup() {
 pinMode (pin, OUTPUT);
 digitalWrite (pin, LOW);
 // start serial port:
 Serial.begin(9600);
 // give the ethernet module time to boot up:
 delay(1000);
 // start the Ethernet connection using a fixed IP address and DNS server:
 Ethernet.begin(mac, ip, myDns);
 // print the Ethernet board/shield's IP address:
 Serial.print("My IP address: ");
 Serial.println(Ethernet.localIP());
}
void loop() {
 // if there's incoming data from the net connection.
 // send it out the serial port. This is for debugging
 // purposes only:
 if (client.available()) {
 Serial.println ("1"); delay(500);
 char c = client.read();
 Serial.print(c);
 } else {Serial.println ("11");delay (500);}
 // if there's no net connection, but there was one last time
 // through the loop, then stop the client:
 if (!client.connected() && lastConnected) {
 Serial.println(2); delay(500);
 Serial.println();
 Serial.println("disconnecting.");
 client.stop();
 } else {Serial.println ("22");delay (500);}
 // if you're not connected, and ten seconds have passed since
 // your last connection, then connect again and send data:
 if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
 Serial.println(3); delay(500);
 httpRequest();
 Serial.println ("4");delay (100);
 } else {Serial.println ("33");delay (500);}
 // store the state of the connection for next time through
 // the loop:
 lastConnected = client.connected();
 Serial.println ("end loop");
}
// this method makes a HTTP connection to the server:
void httpRequest() {
 // if there's a successful connection:
 Serial.println ("httpRequest"); delay(500);
 if (client.connect(server, 80)) {
 Serial.println ("H1"); delay(500);
 Serial.println("connecting...");
 // send the HTTP PUT request:
 client.println("GET /latest.txt HTTP/1.1");
 client.println("Host: www.arduino.cc");
 client.println("User-Agent: arduino-ethernet");
 client.println("Connection: close");
 client.println();
 // note the time that the connection was made:
 lastConnectionTime = millis();
 } else {
 Serial.println ("H11"); delay(500);
 // if you couldn't make a connection:
 Serial.println("connection failed");
 Serial.println("disconnecting.");
 client.stop();
 }
}

I inserted debug lines. all I get are the faileure "if" comments. after 15 seconds, I get debug #3 and then "HTTP Request" then nothing but crickets....

dda
1,5951 gold badge12 silver badges17 bronze badges
answered Jun 18, 2015 at 6:20
0

Just to get this answered (see comments): It was a weird "bug" in my case. After days of research I found people with the same issue and in their cases it helped to cut all of the pin headers but the ICSP header by 2mm so the ICSP header had better contact. I tried it on my shield and it worked.

answered May 10, 2020 at 3:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.