0

#include <ESP_Mail_Client.h>
#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define WIFI_SSID "Scissor Hand" // "Redmi_NOte_10_Pro_2"
#define WIFI_PASSWORD "9v6Z1yIIjG@"//"shristika"

#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465

/* The sign in credentials */
#define AUTHOR_EMAIL "" //"esp32serverdata@gmail.com"//
#define AUTHOR_PASSWORD ""//"esp32datasender"//

/* Recipient's email*/
#define RECIPIENT_EMAIL ""

/* Threshold vue */
const int thresholdValue = 100;

/* The SMTP Session object used for Email sending */
SMTPSession smtp;

/* Declare the session config data */ 
ESP_Mail_Session session;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  //Serial.println();
  //Serial.print("Connecting to AP");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED){
    //Serial.print(".");
    delay(200);
  }
  //Serial.println("");
  //Serial.println("WiFi connected.");
  //Serial.println("IP address: ");
  //Serial.println(WiFi.localIP());
  Serial.println();
  pinMode(2, INPUT); // input for mq-6 gas
  pinMode (26, OUTPUT); // output pin for lcd
  pinMode (25, OUTPUT); // output pin for buzzer.
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  
}

// the loop routine runs over and over again forever:
void loop() {
  
  //****************** read the input on analog pin 2;*********************
  int sensorValue = analogRead(2);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(2000);        // delay in between reads for stability

//*********************end for the read input **************************
  
//***************************convert the analog value into PPM*******************



// **************************end for conversion ******************************

//********************** output control with Threshold************************
  if (sensorValue > 100){ // for buzzer and led give the threshold value 
    sendEmail(sensorValue);
    digitalWrite(25, HIGH);
    digitalWrite(26, HIGH);
    }
  else {
        digitalWrite(25, LOW);
        digitalWrite(26,LOW );

       
    };
//******************************end for output control with threshold***********************************************
display.clearDisplay();
  display.setTextSize(1); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
display.setCursor(10,10);
//display.println(F("ss"));
//display.setCursor(12,3);
display.println(sensorValue);
display.display();


}
void sendEmail( int sensorValue){
  /* Declare the message class */
  smtp.debug(1);
  /* Declare the session config data */
  ESP_Mail_Session session;
  /* Set the session config */
  session.server.host_name = SMTP_HOST;
  session.server.port = SMTP_PORT;
  session.login.email = AUTHOR_EMAIL;
  session.login.password = AUTHOR_PASSWORD;
  session.login.user_domain = "";

  /* Declare the message class */
  SMTP_Message message;

  /* Set the message headers */
  message.sender.name = "ESP";
  message.sender.email = AUTHOR_EMAIL;
  message.subject = "ESP Test Email";
  message.addRecipient("lpg", RECIPIENT_EMAIL);

  /* Prepare the email content */
  String emailContent = "The sensor threshold value has been exceeded. Current value: ";
  emailContent += String(sensorValue);
  message.text.content = emailContent.c_str();
  /* Connect to server with the session config */
  if (!smtp.connect(&session)){
      Serial.println("couldnot connect to the server");
    return;
  }
  /* Send the email */
  if (!MailClient.sendMail(&smtp, &message)) {
    Serial.println("Error sending email: " + smtp.errorReason());
  } else {
        Serial.println("Email sent successfully");
  }

}

The following code works just fine without the email part. When the functions and commands related to mail are added, the mail is not send, neither does the buzzer,oled or the led work. There are no errors shown by Arduino IDE. The OLED screen also only shows the maximum value of the sensor (4095). I dont understand where there mightve been an error.help wpuld be appreciated.

  • try using test SMTP server such as https://mailsnag.com and see if that works. I always have trouble integrating with gmail - would be good to know if the issues is Gmail integration (port, auth, etc) or something else. – Iuri G. Jun 22 '23 at 00:40

0 Answers0