Simple Ethernet Shield Program - Acts different

Hi all,

So I’m trying to use my Ethernet Shield to send a simple email via SMTP using Telnet.

The problem is the shield is very unpredictable. The code below worked once and now will not connect to the server. It used to say return 0 at client.connect(“smtpcorp.com”, 2525); but now it just stays stuck at the function. Also 0 is not a valid return value according to http://arduino.cc/en/Reference/ClientConnect.

Here’s my code:

// EMAIL VIA TELNET

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

byte mac[] = {  // MY MAC ADRESS
  0xDE, 0xAD, 0xBE, 0xEE, 0xEE, 0xEE };
IPAddress ip(192,168,1,66); // my IP\

//IP address of the server 207.58.147.66
IPAddress server(207,58,147,66); 

//initialize client libaray
EthernetClient client;

void setup(){
  // start ethernet connection with my MAC and IP
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  int connectStat = client.connect("smtpcorp.com", 2525);
  Serial.println(connectStat);
  if (connectStat) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop(){

  if (client.available()) { // read to see if there is anything to read
    char c = client.read();
    Serial.print(c);
  }
   if (client.connected()) {
      client.print("ehlo"); 
    }
      if (client.available()) { // read to see if there is anything to read
    char c = client.read();
    Serial.print(c);
  }
    while(true);// stay here and do nothing

}


void sendData(){
}

Can anyone explain what is going on and how I can get it to connect reliably?

The only valid return value for a successful connection using a domain name is 1. A zero (unable to connect to the server) or negative (unable to resolve the domain name) return value is a failed connection.

  if (connectStat == 1) {
    Serial.println("connected");
  } 
  else if (connectStat == 0) {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  else {
    // if you didn't get a resolution on the domain:
    Serial.println("dns failed");
  }

edit: I had them change the client.connect reference page to show the dns fails (-1 to -5), but they left out the 0 return, which is unable to connect to the server.

oh ok Gotcha. Thanks.

I still don't know why it works sometimes and other times it does not connect when everything on my side is EXACTLY the same.

I suspect my shield may have a hardware problem. Can somebody run my code on their shield to test it?

I am pretty sure your code will connect to my email server if I use port 25, but your code doesn't send anything. I have email client code in the playground that works.
http://playground.arduino.cc/Code/Email

You might try increasing the delay between starting the w5100 and attempting the connection to 2 seconds. My routers sometimes take a couple seconds to register the mac/IP in the ARP table.

Is it the connection that is failing or the dns resolution failing?

You're connecting to a site on the Internet, so there may be any problem between your Arduino's Ethernet port and the remote server that keeps your sketch from connecting. Even if everything on your side stays the same between the two connection attempts.

BTW: If you're connecting to a remote site, you should either use the DHCP begin() call:

Ethernet.begin(mac);

or the fully fledged call with all the necessary information provided:

Ethernet.begin(mac, ip_address, dns_server_ip, gateway_ip, subnet_mask);

All other versions let the library estimate your network configuration, which may be not accurate for your setup.