Ethernet shield AND program flow.

Hello,

I’m trying to use the ethernet shield here, with my Arduino MEGA 2560 R3. However, the program’s “setup” flow seems to be interrupted (hangs and stops there) if there is no network connection to the ethernet shield.

Here’s the includes :

#include "Wire.h" //Wire Library
#include <LiquidCrystal.h> // The LCD library
#include <Ethernet.h>
#include <SPI.h>
#include <EthernetUdp.h>

/* - Internet Connection Systems - */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  

unsigned int localPort = 2390;      // UDP listening port

EthernetUDP Udp;

IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
IPAddress Gateway(192, 168, 1, 1); // time.nist.gov NTP server

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

And here’s the setup code:

  // Fire up the LCD
lcd.begin(16, 2);              // Define a  16x2 LCD module
 lcd.setCursor(0,0); // Set LCD Cursor
lcd.print("    Welcome     ");
delay(1000);
lcd.print("                ");
// Start RTC & Serial Comm. 
  Wire.begin(); //Begin Handshaking with the RTC
  Serial.begin(9600); //Start Serial Communication
  
   pinMode(led, OUTPUT);    //Set Ambient LED socket as output
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
   
     pinMode(lcdpower, OUTPUT);    //Set Ambient LED socket as output
   digitalWrite(lcdpower, HIGH);   // turn the LED on (HIGH is the voltage level)

Ethernet.begin(mac); //Start Ethernet
Udp.begin(localPort);
}

How can this be solved?

Thanks!

Insure this is where it is failing. It should show failed or the ip within a minute or so.

Serial.print(F("Starting w5100..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(Ethernet.localIP());

If you have a SD card in the shield slot, remove it and power cycle the Arduino for this test. Any other SPI devices lurking around in your hardware?

SurferTim: Insure this is where it is failing. It should show failed or the ip within a minute or so.

Serial.print(F("Starting w5100..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(Ethernet.localIP());

If you have a SD card in the shield slot, remove it and power cycle the Arduino for this test. Any other SPI devices lurking around in your hardware?

I've use that code, and, here's the outputs :

With Network : Starting w5100...192.168.2.3

Without Network : Starting w5100...failed

Now, thanks for help pointing out the problem. :)

The program "continues" after about ~50-70 secs of delay. So, with that problem solved:

1) Are there any way to speed up the process? (reducing the startup delay?) 2) In the program, how do I check if an internet connection is available?

Thanks again!

That is about a minute or so. ;)

If you get an ip, that is an indication that your gateway router is probably working, but does not indicate an internet connection. If I want to check the internet connection, I use UDP protocol and an NTP time check call to a remote NTP server. If I get the time, there is an internet connection.

edit: You could do the same thing if you use dns and resolve a domain name using a remote dns server.