Problems sending email with ESP8266

Hi everyone,
I’m working on a project which should understand when an old person falls on the ground and sends an email to some relative to alert him. I did everything but the email part: my problem is that when I run the code with Blynk stuff, Blynk stops the execution of everything and doesn’t send any email. Could you help me to find where I am wrong? Tank you all.

Right now the device always connects to wifi and then stops.

Here is the code

#define BLYNK_PRINT Serial

//#include <SPI.h>
#include <Wire.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>

#define ESP8266_BAUD 115200
#define MPU 0x68  // I2C address of the MPU-6050
#define GREEN 6
#define RED 5
#define BLUE 7
#define SENSIBILITY 20
#define BUTTON 3
#define DELAY 5


double AcX,AcY,AcZ, Anet;
int Pitch, Roll, oldPitch, oldRoll, check;
char auth[] = "*******";
char ssid[] = "*******";
char pass[] = "*******";
int con = 0;
bool connected = false;

void setup(){

  Serial.begin(9600);
  init_MPU(); // Inizializzazione MPU6050
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);

  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);

  attachInterrupt(1, changeCheck, CHANGE);

  oldPitch = 0;
  oldRoll = 0;
  check = 0;



 if(Roll < 50 && Roll > -50 && Pitch <50 && Pitch > -50){

    analogWrite(RED, 0);
    analogWrite(GREEN, 255);
    analogWrite(BLUE, 0);


  }
  else{
    analogWrite(RED, 0);
    analogWrite(GREEN, 0);
    analogWrite(BLUE, 255);
  }

}

void loop()
{


  FunctionsMPU(); // Acquisisco assi AcX, AcY, AcZ.

  if(abs(oldPitch - Pitch) > SENSIBILITY || abs(oldRoll -Roll) > SENSIBILITY){
  
    //IN PIEDI
    if(Roll < 50 && Roll > -50 && Pitch <50 && Pitch > -50){
  
      analogWrite(RED, 0);
      analogWrite(GREEN, 255);
      analogWrite(BLUE, 0);

  
    } //CADUTA

    else {
  
      analogWrite(RED, 0);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 255);
      int initial = millis();
      int stopdelay = 0;
      check = 2;
  
      while(check == 2){
    
        if(stopdelay > DELAY*1000)
         {

          while(true){
      
          analogWrite(RED, 255);
          analogWrite(GREEN, 0);
          analogWrite(BLUE, 0);

          //Invio email
          SoftwareSerial EspSerial(10,11);
          ESP8266 wifi(&EspSerial);
          EspSerial.begin(ESP8266_BAUD);
          Blynk.begin(auth, wifi, ssid, pass);
          Blynk.run();       
          Blynk.email("*******", "HELP", "SONO CADUTO!");
          }
        }
    
        stopdelay = millis() - initial;
      }

      check = 0;
      while(Pitch < -30 || Pitch > 30 || Roll < -30 || Roll > 30){
        
      analogWrite(RED, 0);
     analogWrite(GREEN, 0);
      analogWrite(BLUE, 0);
  
      FunctionsMPU();
      }
    }
    oldPitch = Pitch;
    oldRoll = Roll;
  }
}


void init_MPU(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 registe
     Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  delay(1000);
}

//Funzione per il calcolo degli angoli Pitch e Roll
double FunctionsPitch(double A, double B, double C){
  double DatoA, DatoB, DatoC, Value;
  DatoA = A;
  DatoB = B*B;
  DatoC = C*C;

  Value = atan(DatoA/sqrt(DatoB + DatoC));
  Value = Value * 180/PI;

  return (int)Value;
}

double FunctionsRoll(double B, double A, double C){
  double DatoA, DatoB, DatoC, Value;
  DatoA = A;
  DatoB = (B*B);
  DatoC = C*C;

  Value = atan(DatoA /sqrt(DatoB + DatoC));
  Value = Value * 180/PI;

 return (int)Value;
}

double FunctionsAnet(double A, double B, double C){

  double DatoA, DatoB, DatoC, Value;
  DatoA = A*A;
  DatoB = B*B;
  DatoC = C*C;

  Value =  sqrt(DatoA + DatoB + DatoC);

  return Value;
}

//Funzione per l'acquisizione degli assi X,Y,Z del MPU6050
void FunctionsMPU(){

  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,6,true);  // request a total of 14 registers

  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)

      
  Roll = FunctionsRoll(AcX, AcY, AcZ);   //Calcolo angolo Roll
  Pitch = FunctionsPitch(AcX, AcY, AcZ);  //Calcolo angolo Pitch
  Anet = FunctionsAnet(AcX, AcY, AcZ); //Calcolo Anet

  Serial.print("Roll "); Serial.print(Roll);
  Serial.print("\t");
  Serial.print("Pitch "); Serial.print(Pitch);
  Serial.print("\t");
  Serial.print("Anet "); Serial.print(Anet);
  Serial.print("\n"); 
}

void changeCheck(){ check == 2 ? check = 1 : check = check; Serial.println("INTERRUPT!");}

What hardware are you using?

Your void loop contains far too much code for Blynk to run successfully, you need to put all that stuff in a separate function and call it using a timer.

Pete.

I’m using Arduino UNO, wifi module ESP8266, MPU 6050 IMU and a RGB led

In that case, your baud rate is far too high for software serial communication between your Uno and ESP:

Your ESP needs to be configured (via an AT command) to use 9600 baud and your sketch needs to match this.

Personally, I’d scrap the Uno/ESP combination and go for a Wemos D1 Mini. Much smaller and more portable, less power hungry and much easier to get working with Blynk.

Pete.

1 Like

unfortunately I cannot change my devices so I have to use UNO/ESP. I’m sorry but, since I have never used before this kind of ESPs, I have no idea of what are you talking about :slight_smile:

I have followed this guide http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app trying to move out of the loop function the code and this is the result:

#include <Blynk.h>

#define BLYNK_PRINT Serial

//#include <SPI.h>
#include <Wire.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>

#define ESP8266_BAUD 115200
#define MPU 0x68  // I2C address of the MPU-6050
#define GREEN 6
#define RED 5
#define BLUE 7
#define SENSIBILITY 20
#define BUTTON 3
#define DELAY 5


double AcX,AcY,AcZ, Anet;
int Pitch, Roll, oldPitch, oldRoll, check;
char auth[] = "*******";
char ssid[] = "**********";
char pass[] = "********";
int con = 0;
bool connected = false;

BlynkTimer timer;

void setup(){
  
  Serial.begin(9600);
  SoftwareSerial EspSerial(10, 11);
  ESP8266 wifi(&EspSerial);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setInterval(1000L, timerEvent);

  
  init_MPU(); // Inizializzazione MPU6050
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);

  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);

  attachInterrupt(1, changeCheck, CHANGE);
  
  oldPitch = 0;
  oldRoll = 0;
  check = 0;



  if(Roll < 50 && Roll > -50 && Pitch <50 && Pitch > -50){
    
    analogWrite(RED, 0);
    analogWrite(GREEN, 255);
    analogWrite(BLUE, 0);

    
  }
  else{
    analogWrite(RED, 0);
    analogWrite(GREEN, 0);
    analogWrite(BLUE, 255);
  }

  

}
 
void loop()
{
  
  Blynk.run();
  timer.run();
  ////
}


void init_MPU(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  delay(1000);
}
 
//Funzione per il calcolo degli angoli Pitch e Roll
double FunctionsPitch(double A, double B, double C){
  double DatoA, DatoB, DatoC, Value;
  DatoA = A;
  DatoB = B*B;
  DatoC = C*C;
  
  Value = atan(DatoA/sqrt(DatoB + DatoC));
  Value = Value * 180/PI;
  
  return (int)Value;
}

double FunctionsRoll(double B, double A, double C){
  double DatoA, DatoB, DatoC, Value;
  DatoA = A;
  DatoB = (B*B);
  DatoC = C*C;
  
  Value = atan(DatoA /sqrt(DatoB + DatoC));
  Value = Value * 180/PI;
  
  return (int)Value;
}
 
//Funzione per l'acquisizione degli assi X,Y,Z del MPU6050
void FunctionsMPU(){
  
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,6,true);  // request a total of 14 registers
  
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)

      
  Roll = FunctionsRoll(AcX, AcY, AcZ);   //Calcolo angolo Roll
  Pitch = FunctionsPitch(AcX, AcY, AcZ);  //Calcolo angolo Pitch

  Serial.print("Roll "); Serial.print(Roll);
  Serial.print("\t");
  Serial.print("Pitch "); Serial.print(Pitch);
  Serial.print("\t");

  angleAnalysis();
}

void changeCheck(){ check == 2 ? check = 1 : check = check; Serial.println("INTERRUPT!");}

void angleAnalysis (){
  if(abs(oldPitch - Pitch) > SENSIBILITY || abs(oldRoll -Roll) > SENSIBILITY){
      
    //IN PIEDI
    if(Roll < 50 && Roll > -50 && Pitch <50 && Pitch > -50){
      
      analogWrite(RED, 0);
      analogWrite(GREEN, 255);
      analogWrite(BLUE, 0);
  
      
    } //CADUTA
  
    else {
      
      analogWrite(RED, 0);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 255);
      int initial = millis();
      int stopdelay = 0;
      check = 2;
      
      while(check == 2){
        
        if(stopdelay > DELAY*1000)
         {

          while(true){
          
          analogWrite(RED, 255);
          analogWrite(GREEN, 0);
          analogWrite(BLUE, 0);
       
          Blynk.email("********", "AIUTO", "SONO CADUTO!");
          }
        }
        
        stopdelay = millis() - initial;
      }
  
      check = 0;
      while(Pitch < -30 || Pitch > 30 || Roll < -30 || Roll > 30){
            
      analogWrite(RED, 0);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 0);
  
      FunctionsMPU();
      }
    }
    oldPitch = Pitch;
    oldRoll = Roll;
  }
}


void timerEvent()
{
  FunctionsMPU();
  angleAnalysis();
}

but it still doesn’t work

That really doesn’t give other forum members much to go on does it?

You’ll never get the ESP and Uno to communicate at a baud rate of 115200 using software serial, the software serial library just isn’t capable of handling that speed. If you don’t know how to change the baud rate of your ESP down to 9600 then search this forum for information on the correct AT commands to use, and for ways in which to send these commands to your ESP.

Pete.