Hi.
Can you fix this code to send only 1 email when it trigger the sensor,right now when sensor trigger it send alot of mail. thank you.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
int LED = 13; // choose the pin for the LED
int SENSOR = 2; // choose the input pin (for SENSOR sensor)
int VAL =digitalRead(SENSOR); // variable for reading the pin status
int SENSORS = LOW;
// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };
// change network settings to yours
IPAddress ip( 192, 168, 0, 32 );
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
char server[] = "mail.smtp2go.com";
int port = 2525; // You can also try using Port Number 25, 8025 or 587.
File myFile;
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT); // declare LED as output
pinMode(SENSOR, INPUT); // declare sensor as input
digitalWrite(LED, LOW);
Ethernet.begin(mac);
delay(2000);
Serial.println(Ethernet.localIP());
Serial.println(F("Ready. Press 'e' to send."));
}
void loop(){
if (digitalRead(SENSOR) == HIGH) {
digitalWrite(LED, HIGH); // turn LED ON
////delay (300);
if (SENSORS == LOW) {
Serial.println("Motion detected!");
SENSORS = HIGH;
}
}
else {
digitalWrite(LED, LOW); // turn LED OFF
if (SENSORS == HIGH){
Serial.println("Motion ended!");
SENSORS = LOW;
}
}
byte inChar;
inChar = digitalRead(SENSOR); /// Serial.read();
if(inChar == HIGH )
{
if(sendEmail()) Serial.println(F("Email sent"));
else Serial.println(F("Email failed"));
}
}
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
if(client.connect(server,port) == 1) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your Arduino's ip
client.println("EHLO 192.168.0.31 ");
if(!eRcv()) return 0;
Serial.println(F("Sending auth login"));
client.println("auth login");
if(!eRcv()) return 0;
Serial.println(F("Sending User"));
// Change to your base64 encoded user
client.println(F("yyyyyyyyyyyyyyyyy"));
if(!eRcv()) return 0;
Serial.println(F("Sending Password"));
// change to your base64 encoded password
client.println(F("xxxxxxxxxxx"));
if(!eRcv()) return 0;
// change to your email address (sender)
Serial.println(F("Sending From"));
client.println("MAIL From: <hanluu@hotmail.com>");
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
client.println("RCPT To: <hanluu@gmail.com>");
if(!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println("DATA");
if(!eRcv()) return 0;
Serial.println(F("Sending email"));
// change to recipient address
client.println("To: <hanluu@gmail.com>");
// change to your address
client.println("From: <hanluu@hotmail.com>");
client.println("Subject: Your Subject");
client.println("Hi! Simple test message");
client.println(".");
if(!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
client.println("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"));
}