Network Questions

Hello everyone,

I am trying to code a sketch that will sense if a door is open and send an email if it is. all works well and sends the first time but then after a while it disconnects and wont connect again unless I power cycle the arduino. Im certain that it is a simple issue that I am overlooking, but I am stumped all the same.

Any help would be awesome!!

Thanks,
Toby

Here is the code:

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

byte mac[] = {dwino mac addy};
IPAddress ip(dwino addy);

// Enter the IP address of the server you're connecting to:
IPAddress server(server addy); 

EthernetClient client;


// Distance sensor
int Door1;

int melody[] = {
  NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  8, 8, 8, 8, 8, 8, 8, 8 };
  
  
  

void setup()
{
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

}





void loop()
{
Door1= analogRead(2);
delay(500);

if (Door1 > 100){
  Serial.print ("Door 1= ");
  Serial.print (Door1);
  Serial.print("what the heck!! Close the friggin door!!");
  Serial.println ();

     for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/noteDurations[thisNote];
      tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
    // stop the tone playing:
      noTone(8);
    }


  // give the Ethernet shield a second to initialize:
  delay(3000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
    client.println("EHLO server addy");
      delay(2800);
    client.println("MAIL FROM: email addy");
      delay(2800);
    client.println("RCPT TO: email addy");
      delay(2800);
    client.println("DATA");
      delay(2800);
   client.println("SUBJECT: Door open");
      delay(2800);
   client.println();
   client.println();
      delay(2800);
    client.println("Some dork left the door open again.");
      delay(2800);
    client.println("Go make em shut it!!.");
      delay(2800);
    client.println(".");
      delay(1000);
    client.println("quit");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  
} else
  {
   Serial.print ("Door 1= ");
  Serial.print (Door1);
  Serial.println ();
  }
}

Hi, you don't seem to ever disconnect/stop() the client from the server. Yes, you send "quit" and the server disconnects, but from the client's point of view there are still characters to read and the disconnection probably never happens. Try disconnecting the client right after "quit".

You should read the responses from the email server. This code does that.
http://playground.arduino.cc/Code/Email
You can replace the “Press ‘e’ to send” with your trigger.

spatula: Hi, you don't seem to ever disconnect/stop() the client from the server. Yes, you send "quit" and the server disconnects, but from the client's point of view there are still characters to read and the disconnection probably never happens. Try disconnecting the client right after "quit".

Thanks, after a little rearranging and disconnecting the client it works like a charm!! Thanks again, Toby

if do you want to do ethernet server and ethernet client you usually have a problem and not connecting.

I have a "dumb" question. How do you get your networked Arduino to send an email? It would seem the following code is key, but it isn't obvious to me. (ie "EHLO server addy" - Email HLO??) Can you use a generic me@gmail.com account? Thanks in advance for any help.

 if (client.connect(server, port)) {
    Serial.println("connected");
    client.println("EHLO server addy");
      delay(2800);
    client.println("MAIL FROM: email addy");
      delay(2800);
    client.println("RCPT TO: email addy");
      delay(2800);
    client.println("DATA");
      delay(2800);
   client.println("SUBJECT: Door open");
      delay(2800);

See reply #2 above. Click on the link. That reads and displays the email server responses to all those sends. That way if something goes wrong, you will know what it was.

BTW, my email server does not take email with your code, even if everything is correct. If you do not read the response before sending again, my email server refuses the email with the reply "500 I don't take email from rude servers" and "hangs up" on you.

purza: I have a "dumb" question. How do you get your networked Arduino to send an email? It would seem the following code is key, but it isn't obvious to me. (ie "EHLO server addy" - Email HLO??) Can you use a generic me@gmail.com account? Thanks in advance for any help.

 if (client.connect(server, port)) {
    Serial.println("connected");
    client.println("EHLO server addy");
      delay(2800);
    client.println("MAIL FROM: email addy");
      delay(2800);
    client.println("RCPT TO: email addy");
      delay(2800);
    client.println("DATA");
      delay(2800);
   client.println("SUBJECT: Door open");
      delay(2800);

Its sending it via a telnet session. Tim is correct, some servers will reject you however our servers are in house so I have greater control on how they are handled. You might be able to send like this through gmail, I don't know. Google could tell ya a lot more than I!

Btw, if is need send a garanteed mail more suitable will be use not a home smtp, but sendgrid. In this case, somewhere placed php, which uses get in, will be an quick solution.

Surfer Tim, thanks for pointing me to the link. After some investigation it appears I have lot more reading ahead of me. Plus I need a better understanding of C++ objects.

tobywimberly, cool application! I’m building a Fish Pond Monitor that monitors and maintains water level and turns on a heater if it goes below 0 C. It serves a status web page of temperature. I eventually want to incorporate a text message to my not so smart phone when a problem occurs via cellphonenumber@vxpix.com

purza: Surfer Tim, thanks for pointing me to the link. After some investigation it appears I have lot more reading ahead of me. Plus I need a better understanding of C++ objects.

tobywimberly, cool application! I'm building a Fish Pond Monitor that monitors and maintains water level and turns on a heater if it goes below 0 C. It serves a status web page of temperature. I eventually want to incorporate a text message to my not so smart phone when a problem occurs via cellphonenumber@vxpix.com

Thanks! By the way, you could accomplish that using the free version of smartermail if you have a spare when-doze box and dynamic dns. then you could telnet your email to your server and send it to your phone using similar code in my project!

Good luck! Keep me posted, Id like to know how it works out!