Email alert of temperature [Solved]

Hello, first of all many thanks in advance for any help you can bring me. This is my first post and english is not my first language, so I'll do my best.

It is probably a simple issue, since I'm quite beginner with programming. Anyway a brief explanation of what I'm trying.

With an Arduino Ethernet + Nano Router as Client + 2 Temperature sensors + hmailserver I'm being able to send an email when temperature reaches some point. The problem is that once temperature falls down, and once again it reaches that point I don't get any email anymore...so it only works the first time, and in order to make it work again I've to reset.

Here's the code I'm using:

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

float temperatura1=0;
float temperatura2=0;
int led=9;
EthernetClient client;
void alarm(); //Function that sends the email
void setup()
{
  pinMode(led, OUTPUT);   
}

void loop()
{
  
temperatura1 = (5.0 * analogRead(0)*100.0)/1023.0; // Sensor 1
temperatura2 = (5.0 * analogRead(1)*100.0)/1023.0; // Sensor 2

delay (500); 
 if((temperatura1>=28)||(temperatura2>=28)) // If any sensor is beyond 28, call alarm()
 {
   alarm();
 }
}

void alarm()
{
  byte server[] = { 192, 168, 1, 28 }; // Arduino IP
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  byte ip[] = { 192, 168, 1, 35 }; // hmailserver IP
  digitalWrite(led, HIGH);  // LED ON
  Ethernet.begin(mac, ip);
  delay(1000);
  if (client.connect(server, 25)) { // Send the email with telnet commands
    delay(100);
    client.println("helo localhost");
    delay(100);
    client.println("auth login");
    delay(100);
    client.println("BASE64 email acount1"); 
    delay(100);
    client.println("BASE64 pwd email acount1"); 
    delay(100);
    client.println("MAIL FROM: email acount 1");
    delay(100);
    client.println("rcpt to: email account 2");
    delay(100);
    client.println("DATA");
    delay(100);
    client.println("Subject: Temperature level alarm");
    delay(100);
    client.println("La temperatura mesurada a la sala CPD excedeix els 28ºC!");
    delay(100);
    client.println(".");
    delay(100);
    client.println("quit");
while((temperatura1>=28)||(temperatura2>=28)) // Until temperature is under 28, check sensors
{
temperatura1 = (5.0 * analogRead(0)*100.0)/1023.0;
temperatura2 = (5.0 * analogRead(1)*100.0)/1023.0;
  }
 digitalWrite(led, LOW);   // Once temperature is ok, turn OFF the led.
}
}

Any idea why is only working one first time?

Many thanks!

I think it's a bad design to have the alarm function block until the temperature drops. Better to have that simply send the alarm, and have the control logic of when to send the alarm separate. One way to do that is to take the while loop and digitalWrite() out of alarm() and put them in loop() immediately after the call to alarm(). It won't affect the behaviour, but will give you a cleaner design.

You're initialising the ethernet client in alarm which means it'll be reinitialised each time you try to send an alarm. I don't know whether that would account for your problem, but it is more normal to initialise the ethernet client once in setup() and I suggest you do that.

email code that monitors for an event (an e sent from the serial monitor in this instance), then goes to the email code to send the email when the event is detected.

//zoomkat 4/08/12
//email test code using DNS
//developed from code posted by various persons

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

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "smtp.yourISP.net"; // your ISP's SMTP server
EthernetClient client;

//////////////////////

void setup(){
  Serial.begin(9600); 
  Serial.println("DNS and DHCP-based email test 4/08/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  Serial.println();
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
    else Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(serverName, 25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
  
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: you@athome.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@athome.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

Does your LED ever go off?

Hello, many thanks for your answers, it is working perfectly now.

I've missed the client.stop() at the end of the alarm() function (I think it was the main problem), and also I've put the while and the digitalwrite just after the alarm() function is called. Although I'll keep the Ethernet.begin in the alarm(), since the temperature is not expected to grow more than once per month, and maybe I'm wrong, but I'm doing it in orden to save power due to the router won't be listening the whole time. Is that a correct assumption?

Many thanks again!

And of course if anybody is trying to do something similar, just let me know and I'll try to help.

This will cause you problems with some servers like gmail and mine. I put comments at those places in the code. Mine returns a "I don't accept email from rude servers" error message, and "hangs up" on you.

  //wait for server "queing" response

// once this evaluation falls through (characters available)
// and you do not read the packet,
// this will evaluate to "characters available" for all others like this
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response

// like this one...
  while(!client.available()) delay(1);

  client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response

// and this one...
  while(!client.available()) delay(1);

  client.println("RCPT To: you@athome.net"); /* identify recipient */

  //wait for server "receipent ok" response

// and this one...
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
// and this one...
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@athome.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
// and this one...
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
// and this one.
  while(!client.available()) delay(1);

edit: Without any error checking or timeout, you need at a minimum:

  // after each of these..
  while(!client.available()) delay(1);
  // add this 
  while(client.available()) Serial.write(client.read());

The code won't detect the error, but if it fails, you will see why on the serial monitor.

Hi,
I'm new in this forum, but I read a long time and I appreciate your advice. I have an arduino1rev3 and one ethernet shield.

I have 2 working sketch:
1° send data to Emoncms
2° sends mail if one sensor (like light) reads value lower than 25

I can not put together the sketcht. If i put together the sketch, arduino no works. Why? how should I put them together? I have done many tests, but nothing.... =(. Help me. Thx

my 2 sketch:

1st
#include <SPI.h>
#include <Ethernet.h>
int analogInPin = A0;
int sensorValue = 0;

// Local network configuration:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x23, 0x5D };
byte ip[] = { 192, 168, 0, 120 };

// Login data:
String UserName64 = "myid";
String Password64 = "mypw";

// SMTP server data:
// smtp.libero.it : 212.52.84.54
// smtp.iol.it : 212.52.84.203
// out.virgilio.it : 62.211.72.20
byte server[] = { 212, 52, 84, 54 }; // SMTP server
String ServerName = "libero.it";

// Mail data:
String Sender = "mymail";
String Recipient = "mymail2";
String Subject = "Messaggio inviato da Arduino!";
String Body = "txt";

int time = 1000;
int wait = 1000;
String ServerResponse="";
EthernetClient client;
int precedente=0;
void setup() {
Serial.begin(9600);
pinMode( analogInPin, INPUT);
}

void loop() {
sensorValue = analogRead(analogInPin);

Serial.print("sensor = " );
Serial.println(sensorValue);
if (sensorValue>50)
{precedente=0;
}

delay(1000);

if ((sensorValue<50) and (precedente==0)){
precedente=1;
Serial.begin(9600);
Serial.println("Program started, waiting for router...");
delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */

Serial.println("Starting network module...");
Ethernet.begin(mac, ip);
delay(2000);

Serial.println("connecting...");
if (client.connect(server, 25)) {
Serial.println("connected");

SendMsg("EHLO " + ServerName); /* say hello*/
SendMsg("AUTH LOGIN ");
SendMsg(UserName64); /* Username*/
SendMsg(Password64); /* Password /
SendMsg("MAIL From:<" + Sender +">"); /
identify sender /
SendMsg("RCPT To:<" + Recipient + ">"); /
identify recipient /
SendMsg("DATA");
SendMsg("To: " + Recipient); /
recipient in message header /
SendMsg("From: " + Sender); /
seder name in message header /
SendMsg("Subject: "+ Subject); /
insert subject /
SendMsg(""); /
empty line /
SendMsg(Body); /
insert body /
SendMsg(""); /
empty line /
SendMsg("."); /
end mail /
SendMsg("QUIT"); /
terminate connection */
client.println();

} else {
Serial.println("connection failed");
}
}
}
void GetResponse() {
if (client.available()) {
char c = client.read();
while (client.available()) { // Store command char by char.
ServerResponse +=c;
c = client.read();
}
Serial.println("<<<" + ServerResponse);
ServerResponse="";
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

}
}

void SendMsg(String m) {
client.println(m);
Serial.println(">>>" + m);
delay(wait); /* wait for a response */
GetResponse();
}

2nd in this link
http://playground.arduino.cc/italiano/emoncms

I can not put together the sketcht. If i put together the sketch, arduino no works. Why? how should I put them together? I have done many tests, but nothing.... smiley-cry. Help me. Thx

Is this correct? Or is the [Solved] in the new title correct?

Hello PaulS

Yes, it is correct. I started the post and solved my problem some months ago so I changed the title. Seems that giovannilg is having an issue now...should I change it again?

should I change it again?

No. The title on any given post depends on what you reply to. If you change the title, to add [Solved] (as you did) on the initial post, and I reply to something later in the thread, the title on my reply will not say [Solved].

I can't figure out how giovanillg got [Solved] in the title. And, I can't figure out why he/she didn't start a new thread. Referencing this one would have been fine. Hijacking it is not.

Sorry if I caused confusion. It was my first time in this forum. I opened a new tread. If you have any solution for my problem, thank yuo.