Get email alerts on sensor trigger with Wifi shield

Hello,

I use an Arduino Uno a wireless shield and the HC-SR04 sensor.

I have the following code and I am trying to do the following.

If the distance sensor gets a signal below 110cm to send me an email(not only once, every time that distance occurred) and have that distance in the body of the email also.

This is the code I use.

Any thoughts and help are welcome.

Thanks, Emmanuel

#include <SPI.h>
#include <WiFi.h>

//This is for the sensor
#define echoPin 4 // Echo Pin
#define trigPin 3 // Trigger Pin

int maximumRange = 250; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance
//End of code

char ssid[] = "MYSSID";      //  your network SSID (name) 
char pass[] = "MYPASSWORD";   // your network password
int keyIndex = 0;    // your network key Index number (needed only for WEP)

byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };  
IPAddress ip( 192,168,1,101 );    // ipaddress obtained from access point
IPAddress gateway( 192,168,1,1 );
IPAddress subnet( 255,255,255,0 );

char server[] = "mail.server.com";
int port = 25;

int status = WL_IDLE_STATUS;
WiFiClient client;

void setup() {
  Serial.begin(9600);

  //This is for the sensor
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
  //End of code

  while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
  Serial.println("WiFi shield not present");
  // don't continue:
  
  while (true);
  }
  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
  Serial.println("Please upgrade the firmware");
  }
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  
  // Connect to WPA/WPA2 network:
  status = WiFi.begin(ssid, pass);
  // wait 10 seconds for connection:
  delay(10000);
  }
  
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();
  delay(2000);
  //Serial.println("Ready. Press 'e' to send.");
  //delay(2000);
}

void loop() {
 
readDistance();

if(distance>minimumRange && distance < maximumRange)
{
 Serial.println(distance);
}
else if(Serial.println("Out of range..."));

else if(distance < 110)
{
  Serial.println(distance);
    if(sendEmail()) Serial.println(F("Email sent"));
    else Serial.println(F("Email failed"));
}
 delay(50);
}

int readDistance()
{
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 distance = duration/58.2;
}
       
  byte sendEmail()
  {
  byte thisByte = 0;
  byte respCode;
  
  Serial.println("here");     
  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.1.101");
  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
  // https://www.base64encode.org/
  client.println("EMAIL");     
  if(!eRcv()) return 0;     
  Serial.println(F("Sending Password"));
  // change to your base64 encoded password
  // https://www.base64encode.org/
  client.println("PASSWORD");     
  if(!eRcv()) return 0;     
  // change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL FROM: <mail@server.com>");
  if(!eRcv()) return 0;
  // change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT TO: <mail@server.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: You <mail@server.com>");     
  // change to your address
  client.println("From: You <mail@server.com>");     
  client.println("Subject: Arduino email test\r\n");     
  client.println("This is from my Arduino!");
  client.println("This is the code alert!");
  //distance trigger alarm
  // I would like to have here the distance tiriggered
  client.println(distance);     
  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"));
}
void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);    
  Serial.print(F("BSSID: "));
  Serial.print(bssid[5],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[4],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[3],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[2],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[1],HEX);
  Serial.print(F(":"));
  Serial.println(bssid[0],HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print(F("signal strength (RSSI):"));
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print(F("Encryption Type:"));
  Serial.println(encryption,HEX);
}

void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
    Serial.print(F("IP Address: "));
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];  
  WiFi.macAddress(mac);
  Serial.print(F("MAC address: "));
  Serial.print(mac[5],HEX);
  Serial.print(F(":"));
  Serial.print(mac[4],HEX);
  Serial.print(F(":"));
  Serial.print(mac[3],HEX);
  Serial.print(F(":"));
  Serial.print(mac[2],HEX);
  Serial.print(F(":"));
  Serial.print(mac[1],HEX);
  Serial.print(F(":"));
  Serial.println(mac[0],HEX);

  // print your subnet mask:
  IPAddress subnet = WiFi.subnetMask();
  Serial.print(F("NetMask: "));
  Serial.println(subnet);

  // print your gateway address:
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print(F("Gateway: "));
  Serial.println(gateway);
}
else if(Serial.println("Out of range..."));

Serial.println() will return 17, because that is how many characters it wrote to the serial port. So, this statement is, in effect:

else if(17)
{
   ;
}

Seems a strange bit of code to me.

else if(distance < 110)

The only way this statement will ever be reached is if distance is less than one and 17 is not true (which it is). So, you will never send an email.

Paul thanks for your answer.

Despite this code the emails arrives and everything is almost OK. My problem was if the code was correct essentially to the part that sends the email when an action occurred.
The other problem is that this sensor is not adequate for triggering events because its state change too often so my mail server crash from receiving too many mails, so I would try to put another sensor like photoresistor to try it again.
So the question is how to make the email action to work when a event is triggered?

Thanks again

Emmanuel