Sketch runs fine for several hours then stops working?

I have a sketch to send emails over the internet (I will replace some code with xxxx because I don't want any one seeing my information, not that i don't trust you guys :P) Anyways, the sketch works fine for several hours before it stops working. Its strange because it still returns a ping though it doesn't work. heres my sketch--

#include <Ethernet.h>

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192,168,1,177 };

// Enter the IP address of the server you're connecting to:
byte server[] = {
xxx,xxx,xx,xxx};

boolean open=false;
boolean oldOpen=false;

// Initialize the Ethernet client library
// with the IP address and port of the server
Client client(server, 25);

void setup() {
pinMode(3, INPUT);
pinMode(7, OUTPUT);

digitalWrite(7,HIGH);

// start the Ethernet connection:
Ethernet.begin(mac, ip);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1500);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect()) {
Serial.println("connected");
}

oldOpen=digitalRead(3);
}

void loop(){
// if the server's connected
if (client.connected()) {
//Serial.println("connected");
open=digitalRead(3);

digitalWrite(7,LOW);

if(open==oldOpen){
}else{
oldOpen=open;
if(open==true){
//open
Serial.println("open");
client.println("EHLO MYSERVER");
client.println("MAIL FROM:xxxx@xxxxx.net");
client.println("RCPT TO:xxxxxxxxxx@messaging.sprintpcs.com");
client.println("DATA");
client.println("SUBJECT: The garage door has been opened");
client.println();
client.println(".");
client.println(".");
}else{
//close
Serial.println("close");
client.println("EHLO MYSERVER");
client.println("MAIL FROM:xxxx@xxxxx.net");
client.println("RCPT TO:xxxxxxxxxx@messaging.sprintpcs.com");
client.println("DATA");
client.println("SUBJECT: The garage door has been closed");
client.println();
client.println(".");
client.println(".");
}
}

delay(1000);
}else{
digitalWrite(7,HIGH);
client.connect();
}
}

I have it set up using a reed switch to monitor my garage door. The switch works great and the code is fine without the Ethernet part (I just got the Ethernet shield after my Wifi shield bit the dust). I have the shield connected to an old router which I loaded gargoyle custom firmware on to use as an access point. I know that this works great with my computer, I used if for weeks without a problem so I don't think this would affect the arduino but I'm not really sure. Any ideas?

I think your problem is that you are staying connected to the SMTP server indefinitely. I know you are testing for if it is still connected, but even so.

What I would do is rework the logic a bit ...

  • First see if the door status has changed (open to closed, closed to open).
  • If so, connect to the SMTP server. Wait a second or two. Then send the message. Then disconnect.

Also I note that you are not testing the response from the server. That means (especially as you stay connected) that a single "not OK" reply is going to throw the whole thing out. Something like "busy, try again" and you will be out of sync with the server. Reconnecting partly solves that.

Google for "smtp error messages" for more details about the responses. In brief, if the response starts with "4" or "5" you have a problem. Because basically if you have a problem you may need to try again. Like, wait 30 seconds and retry.

I've got a reed switch on my garage door too. :slight_smile:

However mine is connected to a web server on a Uno, so I just interrogate the server from elsewhere if I want to see the status of the door (it is like a "pull" request, whereas you are doing a "push"). Haven't ever had to reboot the Arduino yet. But that is partly, I think, because it waits for a connection, sends the door status, and drops the connection. So the default state is not connected.

Clients should connect, do their busness, then disconnect. You might want to look at the code in the below post to hoe the connection is closed.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235534880/15