Sending Email with Arduino

Asked

Viewed 647 times

1

I am not able to connect properly with gmail smtp server, says "Username and Password not accepted", but my email and password is correct, I am not knowing exactly where the error is.

#include <SPI.h>
#include <Ethernet.h>
#include <Thermistor.h>
#include <Ultrasonic.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 100, 95 };
//EthernetClient client;

Thermistor temp(0);

EthernetServer server(5560);
char msg;

//Inicializa o sensor nos pinos definidos acima
#define pino_trigger 4
#define pino_echo 5
Ultrasonic ultrasonic(pino_trigger, pino_echo);

//Servidor Email
EthernetClient clientEmail;
char serverEmail[] = "smtp.gmail.com"; // Colocar o ip do servidor SMTP 179.107.143.71
int portaEmail = 587;
bool enviaEmail = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop() {
    Ultrasonico();
    delay(500);
}

void Ultrasonico(){
  //Le as informacoes do sensor, em cm e pol
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();
  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  //Exibe informacoes no serial monitor
  //Serial.print("Distancia em cm: ");
  //Serial.print(cmMsec);
  //Serial.print("\n");
  if(cmMsec>9){
    if(!enviaEmail){
      if(EnviaEmail()){
        Serial.println(F("Email sent"));
      }
      else {
        Serial.println(F("Email failed"));
        delay(2000);
        enviaEmail = true;
      }
    }
  }
  else{
    //Serial.print("Fechado!\n");
  }
}

byte EnviaEmail() {
  byte thisByte = 0;
  byte respCode;
  Serial.println(F("conectando..."));
  if(clientEmail.connect(serverEmail,portaEmail) == 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

  clientEmail.println("HELO 177.137.241.191\r\n");
  if(!eRcv()) return 0;

  clientEmail.println("STARTTLS oAuth2\r\n");


  Serial.println(F("Sending auth login"));
  clientEmail.println("auth login \r\n");
  if(!eRcv()) return 0;

  Serial.println("Sending User");
  // Change to your base64 encoded user
  clientEmail.println("Y2pyLnByb2pldG9maW5hbHRjY0BnbWFpbC5jb20=\r\n"); 


  if(!eRcv()) return 0;

  Serial.println("Sending Password");
  // change to your base64 encoded password
  clientEmail.println("xxxxxxxxx\r\n"); 


  if(!eRcv()) return 0;

  // change to your email address (sender)
  Serial.println(F("Sending From"));
  clientEmail.println("MAIL From: <[email protected]>\r\n");
  if(!eRcv()) return 0;

  // change to recipient address
  Serial.println(F("Sending To"));
  clientEmail.println("RCPT To: <[email protected]>\r\n");
  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  clientEmail.println("DATA\r\n");
  if(!eRcv()) return 0;

  Serial.println(F("Sending email"));

  // change to recipient address
  clientEmail.println("To: Rodolfo Souza <[email protected]>\r\n");

  // change to your address
  clientEmail.println("From: Projeto TCC <[email protected]>\r\n");

  clientEmail.println("Subject: RoomX\r\n"); 

  clientEmail.println("Ola, a central foi aberta!\r\n");

  clientEmail.println(".");

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  clientEmail.println("QUIT\r\n");
  if(!eRcv()) return 0;

  clientEmail.stop();

  Serial.println(F("disconnected"));
  enviaEmail = true;
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!clientEmail.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
        clientEmail.stop();
        Serial.println(F("\r\nTimeout"));
        return 0;
      }
    }

    respCode = clientEmail.peek();

    while(clientEmail.available())
    { 
      thisByte = clientEmail.read(); 
      Serial.write(thisByte);
    }

    if(respCode >= '4')
    {
      efail();
      return 0; 
   }

  return 1;
}

void efail()
{
  byte thisByte = 0;
  int loopCount = 0;

  clientEmail.println(F("QUIT"));

  while(!clientEmail.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
        clientEmail.stop();
        Serial.println(F("\r\nTimeout"));
        return;
      }
  }

  while(clientEmail.available())
  { 
    thisByte = clientEmail.read(); 
    Serial.write(thisByte);
  }

  clientEmail.stop();

  Serial.println(F("disconnected"));
}

After compiling, my serial says: Mensagem

If there is another way to send email to Rduino, my only intention is to send me an email.

  • see if your gmail is enabled for less secure applications, you can see if it is enabled here http://www.google.com/settings/security/lesssecureapps. by logging in with your gmail account

  • It is already activated because used this email for other purposes tbm.

2 answers

0

To email using Gmail, Live or Yahoo SMTP you need to use SSL, However ... in the case of Gmail you will need to access your google account settings and "Allow less secure apps", direct link -> https://myaccount.google.com/lesssecureapps

0


I decided using the email BOL

#include <SPI.h>
#include <Ethernet.h>
#include <Thermistor.h>
#include <Ultrasonic.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 100, 95 };
//EthernetClient client;

Thermistor temp(0);

EthernetServer server(5560);
char msg;

//Inicializa o sensor nos pinos definidos acima
#define pino_trigger 4
#define pino_echo 5
Ultrasonic ultrasonic(pino_trigger, pino_echo);

//Servidor Email
EthernetClient clientEmail;
byte serverEmail[] = {200,147,99,132}; // Colocar o ip bol
int portaEmail = 587;
bool enviaEmail = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println(F("Setup"));
}

void loop() {
    Ultrasonico();
    delay(500);
}

void Ultrasonico(){
  //Le as informacoes do sensor, em cm e pol
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();
  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  //Exibe informacoes no serial monitor
  //Serial.print("Distancia em cm: ");
  //Serial.print(cmMsec);
  //Serial.print("\n");
  if(cmMsec>9){
    if(!enviaEmail){
      if(EnviaEmail()){
        Serial.println(F("Email sent"));
      }
      else {
        Serial.println(F("Email failed"));
        delay(2000);
        enviaEmail = true;
      }
    }
  }
  else{
    enviaEmail = false;
  }
}

byte EnviaEmail() {
  byte thisByte = 0;
  byte respCode;
  Serial.println(F("conectando..."));
  if(clientEmail.connect(serverEmail,portaEmail)) {
  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

  clientEmail.println("HELO 192.168.100.95");
  if(!eRcv()) return 0;

  //clientEmail.println("STARTTLS oAuth2\r\n");


  Serial.println(F("Sending auth login"));
  clientEmail.println("auth login");
  if(!eRcv()) return 0;

  Serial.println("Sending User");
  // Change to your base64 encoded user
  clientEmail.println("cm9vbXhAYm9sLmNvbS5icg=="); 


  if(!eRcv()) return 0;

  Serial.println("Sending Password");
  // change to your base64 encoded password
  clientEmail.println("xxxxxxxxxxxx"); 


  if(!eRcv()) return 0;

  // change to your email address (sender)
  Serial.println(F("Sending From"));
  clientEmail.println("MAIL From: <[email protected]>");
  if(!eRcv()) return 0;

  // change to recipient address
  Serial.println(F("Sending To"));
  clientEmail.println("RCPT To: <[email protected]>");
  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  clientEmail.println("DATA");
  if(!eRcv()) return 0;

  Serial.println(F("Sending email"));

  // change to recipient address
  clientEmail.println("To: Rodolfo Souza <[email protected]>");

  // change to your address
  clientEmail.println("From: Roomx AI <[email protected]>");

  clientEmail.println("Subject: RoomX"); 

  clientEmail.println("Ola, a central foi aberta!");

  clientEmail.println(".");

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  clientEmail.println("QUIT");
  if(!eRcv()) return 0;

  clientEmail.stop();

  Serial.println(F("disconnected"));
  enviaEmail = true;
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!clientEmail.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
        clientEmail.stop();
        Serial.println(F("\r\nTimeout"));
        return 0;
      }
    }

    respCode = clientEmail.peek();

    while(clientEmail.available())
    { 
      thisByte = clientEmail.read(); 
      Serial.write(thisByte);
    }

    if(respCode >= '4')
    {
      efail();
      return 0; 
   }

  return 1;
}

void efail()
{
  byte thisByte = 0;
  int loopCount = 0;

  clientEmail.println(F("QUIT"));

  while(!clientEmail.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
        clientEmail.stop();
        Serial.println(F("\r\nTimeout"));
        return;
      }
  }

  while(clientEmail.available())
  { 
    thisByte = clientEmail.read(); 
    Serial.write(thisByte);
  }

  clientEmail.stop();

  Serial.println(F("disconnected"));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.