Hi all I am new to the Ardunio world only been playing with it now for a few days and I'm stuck.
I am building a (pump running) event alarm systems that will email me when power is applied to my pump. (the pump only runs when i have a leak, and thats bad)
Anyway, I have successfully built the system using the Arduino Dou and the ethernet shield and a relay switch that when power is applied the switchPin is set to HIGH.
Then on swithPin = HIGH it send me an email via the ethernet shield thru my smtp server.
The problem I am havign is that as long as the power is applied to the pump the switch is HIGH and I get emails every second until I remove power and the switch goes LOW.
How can I have it send one email only then wait say fifteen minutes before sending another email?
Here is my code so far: Thanks in advance for the help!
#include <SPI.h>
#include <Ethernet.h>
#define switchPin 7 // connected to relay switch.
byte mac[] = { 0x40, 0xG2, 0xDY, 0x00, 0xT5, 0x0D }; // mac address
char serverName[] = "10.0.0.5"; // My SMTP server
EthernetClient client;
void setup(){
Serial.begin(9600);
Serial.println("Message to serial monitor to send email on alarm"); // Message so I can watch in the serial monitor
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// if this failed check connections and dhcp server
while(true);
}
// show your IP address: assigned to ethershield
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();
delay(3000);
pinMode(switchPin, INPUT);
}
void loop()
{
byte inChar;
inChar = (digitalRead(switchPin)); // if switch contact is closed
if(inChar == HIGH)
{
if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
else Serial.println("Email failed");
}
}
//////Begining of sendEmail 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, sending email...");
}
else {
Serial.println("connection failed");
return 0; //send 0 (failed) back to sendEmail() function
}
//wait for server "queing" response
while(!client.available()) delay(1000);
client.println("EHLO canyousendmymail"); /*hello (statement after helo is needed but irrelevant)*/
//wait for server "hello" response
while(!client.available()) delay(1000);
client.println("AUTH LOGIN"); delay(1000); // authorise
client.println("email@domainname.com"); delay(1000); // enter your username below
// and password here
client.println("Password"); delay(1000);
client.println("MAIL From: Arduino@mailserver.com"); // identify sender, this should be the same as the smtp server you are using*/
//wait for server "sender ok" response
while(!client.available()) delay(1000);
client.println("RCPT To: email@domainname.com"); /* identify recipients email address */
//wait for server "receipent ok" response
while(!client.available()) delay(1000);
client.println("DATA");
//wait for server to say "enter your message" response
while(!client.available()) delay(1000);
//send email message to server
client.println("To: email@domainname.com"); /* identify recipient */
client.println("Subject: ALERT!ALERT! Arduino Mail!!"); /* insert subject */
client.println("The Pump system is RUNNING!!!"); /* insert body */
client.println("."); /* end email */
//wait for server "message accepted" response
while(!client.available()) delay(1000);
client.println("QUIT"); /* terminate connection */
//wait for server "goodby" response
while(!client.available()) delay(1000);
//stop client connection
client.stop();
Serial.println("disconnected");
return 1; //send 1 (success) back to sendEmail() function
}