I have an Arduino Uno Rev 3 with Ethernet Shield. My goal is when a smoke detector goes off (cannot remember if it is NO or NC) the board will send an email (which will be changed to email to text). I have found the code below online and it works well, except I cannot get it to run only on the switch status change. I have tried to insert parts of the state change example code into this one but I cannot get it to work. (depending on where the parts of code are inserted it either doesn't work at all (rapid email's being sent, connection failure, or timeout failure). I am very new to Arduinos and would appreciate any advice you can give.
Thanks,
Craig
/*
Tihomir Nedev
April 2014
Change the values for the network setting.
Change the emails. NB! Some sorvers won't accept email for unedentified devices as Arduino.
Hardware: Arduino board + Ethernet shield
*/
#include <SPI.h>
#include <Ethernet.h>
// Set the panic button pin
byte panicButton = 2;
boolean statusCheck=false;
// this must be unique
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x6B, 0x55 };
// change network settings to yours
IPAddress ip(192,168,1,19);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "mail.bresnan.net";
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println(F("Ready."));
pinMode(panicButton, INPUT);
}
void loop()
{
if(digitalRead(panicButton)==HIGH && statusCheck==false)
{
if(sendEmail()) Serial.println(F("Email sent"));
else Serial.println(F("Email failed"));
statusCheck = true;
}
else if (digitalRead(panicButton)==LOW)
{
statusCheck = false;
}
}
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
if(client.connect(server,25)) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
// change to your public ip
client.println(F("helo 1.2.3.4"));
if(!eRcv()) return 0;
Serial.println(F("Sending From"));
// change to your email address (sender)
client.println(F("MAIL From: xxx@xxxx.net"));
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
client.println(F("RCPT To: xxxx@gmail.com"));
if(!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println(F("DATA"));
if(!eRcv()) return 0;
Serial.println(F("Sending email"));
// change to recipient address
client.println(F("To: xxxx@gmail.com"));
// change to your address
client.println(F("From: xxxx@xxxx.net"));
client.println(F("Subject: Barn Smoke Alarm!\r\n"));
client.println(F("The Barn Smoke Alarm is Active!!!"));
client.println(F("."));
if(!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
client.println(F("QUIT"));
if(!eRcv()) return 0;
client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
respCode = client.peek();
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
if(respCode >= '4')
{
efail();
return 0;
}
return 1;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;
client.println(F("QUIT"));
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return;
}
}
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
client.stop();
Serial.println(F("disconnected"));
}