BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to include variable value in an email

#1

I’m using text input widget, it takes a string input one time while you send it, then it’ll be gone although I used global variable, I need to send a number to the email that is stored in this global variable, how can I store it :wink::

String Email ;


BLYNK_WRITE(V1)
{
  String Email = param.asStr();
}

BLYNK_WRITE(V2){
 int pinValue = param.asInt(); 
              
 if ( pinValue == 1 ){ 

   randNumber = random(1000,3000);
   Serial.println(" Verification Code is : ");
   Serial.print (randNumber);
   Serial.println();
   resButton = 1;    // to make a condition works in void loop
   
    timer.setTimer(1000L, SendToEmail , 30 );
 }
}

void setup()
{
    Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Blynk.email(Email.c_str() ,  "YOUR  CODE  is" , randNumber ); 
  timer.setTimer(1000L, TakeEmail , 30 );
}

void loop()
{


if ( resButton == 1 )
   {
    Blynk.virtualWrite(V2 , 255 );
   } else if ( resButton == 0 ){
    Blynk.virtualWrite(V2 , 0 );
   }


   Blynk.run();
   timer.run();
} 

void SendToEmail () 
{
 if ( Email == "" )
  {
  }
  else 
  {
    
    Blynk.email(Email.c_str() ,  "YOUR  CODE  is" , randNumber ); 
    Email = "";
}
}
0 Likes

Please overload Blynk.email() for String recipient
#2
// FROM THIS
BLYNK_WRITE(V1)
{
  String Email = param.asStr();
}

// ERASE the word String to have it as global...

BLYNK_WRITE(V1)
{
  Email = param.asStr();
}
1 Like

#3

That was cool , thanks :grinning:
in the void setup always timers are running at the beginning of operation, for example when I have a servo, LED or any device, the timer strats running for a specified period . How can I avoid this case .

0 Likes

#4

This discussion has nothing to do with overloading the function, it should be in its own thread.

0 Likes

#6

Done!

0 Likes

#7

@Aziz, Can you include your complete sketch.

0 Likes

#9

1- when program is downloaded , servo is opening and closing , the same with LEDs.
2- when resButton = 1 ; it does not call sendtoemail function. could you notice something wrong ?!
3 - I changed timers to clocks counter until I know how to avoid running timers after downlaoding the code.
4- Blynk.email(Email.c_str() , "YOUR CODE" , randNumber ); it didn’t send any email , even when I do examples .

#include <Servo.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SoftwareSerial.h>
 

char auth[] = "553f8416608c43a78daecd8efc6b8765";
char ssid[] = "...";
char pass[] = "...";
String Email ;
String vcode ;
String randNumber;
  
int detect1 = D0;        // sensor 1 in nodemcu 
int detect2 = D1;        // sensor 2
int detect3 = D2;        // sensor 3
int LED1ava = D3;        // LED PARK 1 Availble
int LED2ava = D4;        // LED PARK 2 Availble
int LED3res = D5;        // LED PARK 3 reserving
int servo = D6;          // Gate 
int result1 = 0;     
int result2 = 0;
int result3 = 0;
int buttonState1 = 0;        
int buttonState2 = 0;
int buttonState3 = 0;
int Clock1 = 1;
int Clock2 = 1;
int Clock3 = 1;
int resButton = 0;
int callcode1 = 0;
int callcode2 = 0;


Servo myservo;
//SimpleTimer timer;
  

void setup()
{
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V2, 1 );
  Blynk.virtualWrite(V4, 1 );
  Blynk.virtualWrite(V7, 1 ); 
  Blynk.virtualWrite(V17, 0 );   
  pinMode (detect1 , INPUT );
  pinMode (detect2 , INPUT );
  pinMode (detect3 , INPUT );
  pinMode (LED1ava , OUTPUT );
  pinMode (LED2ava , OUTPUT );
  pinMode (LED3res , OUTPUT );
 

  myservo.attach(D6);
  Blynk.email(Email.c_str() ,  "YOUR  CODE" , randNumber ); 

  /*timer.setTimer(1000L, sendtoemail , 30 );
  timer.setTimer(1000L, vercode, 60  );
  timer.setTimer(1000L, gatelock , 60 ); 
  timer.setTimeout(80000L, refresh  );*/

}


void loop()
{
   
    Blynk.run();
    if ( resButton == 1 )              // calling email function
   {
    sendtoemail ;
    
   }else if ( resButton == 2 ){}      // do nothing after taking an email to strat calling next function
    else if ( resButton == 3){
    Blynk.virtualWrite(V17 , 1 );     // keep switch ON 
   }else if ( resButton == 0 ){
    refresh;
   } 
   
   
   
   
   result1 = digitalRead (detect1);    // 0 means busy .... 1 means  Available
   result2 = digitalRead (detect2);
   result3 = digitalRead (detect3);

   
   if ( result1 == LOW )
   {
    Serial.println("PARK 1 BUSY");
    Blynk.virtualWrite(V2 , 0 );
    digitalWrite ( LED1ava   , LOW );
   } else {
    Serial.println("PARK 1 Available");
    Blynk.virtualWrite(V2, 255 );
    digitalWrite ( LED1ava   , HIGH );
   }

   if ( result2 == LOW )
   {
    Serial.println("PARK 2 BUSY");
    Blynk.virtualWrite(V4 , 0 );
    digitalWrite ( LED2ava   , LOW );
   } else {
    Serial.println("PARK 2 Available");
    Blynk.virtualWrite(V4, 255);
    digitalWrite ( LED2ava   , HIGH );
   }
     
   
   if ( result3 == LOW )
   {
    Serial.println("PARK 3 BUSY");
    Blynk.virtualWrite(V7 , 0 );
    digitalWrite ( LED3res   , LOW );
   } else {
    Serial.println("PARK 3 Available");
    Blynk.virtualWrite(V7, 255);
   }

   
  

   if ( callcode1 == 1 )
   {
    vercode ;
   }else{}

   if ( callcode2 == 1 )
   {
    gatelock;
   }else{}

   Serial.println(".............................");
   delay(1000);   
   
   //timer.run();
} 



void sendtoemail ()
{
Blynk.virtualWrite(V17 , 1 );                     // keep switch ON

Serial.print(" Park 3 reserved at :  ");  
Serial.print(Clock1);                     // *  tracking the counter on Serial Monitor *
Serial.print(" sec");                     
Clock1 ++;
Serial.println(); 
Serial.println("Email in function is :");         // while the counter is running I'd like to see the Email 
Serial.println(Email); 
  
  if ( Email == "" )
  {}
  else 
  {
    Blynk.email(Email.c_str() ,  "YOUR  CODE" , randNumber ); 
    
    Clock1 = 0;
    resButton = 2;                // do nothing 
    callcode1 = 1;                // start calling next function
   }
   if ( Clock1 >= 30 )
   {
    Clock1 = 0;
    resButton = 0 ; 
       
   }
}

void vercode ()
{
  
  Clock2 ++;
  Serial.print ( " Waiting your code , " );
  Serial.print (Clock2);
  Serial.print ( "sec" );

  if ( Clock2 <= 60 )
  {    
   if ( vcode == randNumber )
  {
    digitalWrite ( LED3res   , HIGH );
    vcode = "";                           // delete vcode that user entered on app
    callcode1 = 0;                        // void loop will stop calling this function 
    callcode2 = 1 ;                       // start tracking second verification to open the servo
    resButton = 3;                        // keep switch ON 
  }
  } else 
  {
     Clock2 = 0;         
     callcode1 = 0;
     resButton = 0;                      // call refresh
     
  }
}

void gatelock ()
{
  Serial.print ( " Gate is waiting you :   " );
  Serial.print (Clock3);
  Serial.print ( "sec" );
  Clock3 ++ ;
  
  if ( Clock3 <= 60 )
  {
  if ( vcode == randNumber )
  {
    myservo.write(80);
    delay (2000);
    myservo.write(160);
    callcode2 = 0 ;               // stop calling this function 
    delay (3000);
    resButton = 0;
  }
  }else {
     Clock3    = 0;               
     callcode2 = 0;
     resButton = 0;               
          
  }
}

void refresh ()
{
  Serial.print ( " ( Time is up ) " );
  vcode = "";
  Email = "";
  digitalWrite ( LED3res  , LOW );
  Blynk.virtualWrite(V17 , 0 );
}


BLYNK_WRITE(V6)
{
    Email = param.asStr();
    
}

BLYNK_WRITE(V8){
   vcode = param.asStr();
   Serial.print(" verification code from Blynk is : " ); 
   Serial.print( vcode ); 

}

   
BLYNK_WRITE(V17){
 int pinValue = param.asInt();
 
              
 if ( pinValue == 1 ){ 
   
   int resButton = 1;
   String Email = "" ;
   String vcode = "" ;
   randNumber = random(1000,3000);
   Serial.println(" Verification Code is : ");
   Serial.print (randNumber);
   Serial.println();
   // timer.setTimer(1000L, sendtoemail , 30 );
 }
}



BLYNK_WRITE(V15){
  int ser  = param.asInt();

  if ( ser == 1 )
  {
    myservo.write(80);
  }
  else {
    myservo.write(160);
  }
}
0 Likes

#10

You really should read this…

Pete.

0 Likes

#11

In another way:

1- After downloading the sketch , it immediately runs timers & servo. which is not necessary:

void setup()
{ 
timer.setTimer(1000L, sendtoemail , 30 );
myservo.attach(D6);
}

2 - void loop should not has delays , this is right, but it also works with delays " I have 1 second only " . and it works with me. I was asking here , which is not calling a function:

void loop ()
{
if ( resButton == 1 )              // calling email function
     {
   sendtoemail ;
      }
}

I have global variable int resButton == 0 ; , it changed after switch is ON

BLYNK_WRITE(V17){
 int pinValue = param.asInt();
 if ( pinValue == 1 ){ 
      int resButton = 1;
    }
}

Then it goes to void loop to start calling a function.
I think void loop is clean .

Thanks …

0 Likes

#12

Not at all… all those commands are unnecessarily processing thousands of times a second. Learn to properly use Blynk Timer.

1 Like

#13

I think you mean that I should use timer inside BLYNK_WRITE(V17) such as :

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setTimer(1000L, sendtoemail , 30 );
}

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

BLYNK_WRITE(V17){
 int pinValue = param.asInt();
 if ( pinValue == 1 ){ 
   
   int resButton = 1;
   String Email = "" ;
   String vcode = "" ;
   randNumber = random(1000,3000);
   Serial.println(" Verification Code is : ");
   Serial.print (randNumber);
   Serial.println();
   timer.setTimer(1000L, sendtoemail , 30 );    
 }
}

but the timer is running after downlaoding directly which is not needed :

timer.setTimer(1000L, sendtoemail , 30 );    

previously I used it when @wickedbeernut asked me, but I changed it to simple counter and global variable resButton to 1 when I saw these LEDs and servo are playing after downlaoding , simple counter makes it easy to run it under condition inside void loop , but as you said it’s really stupid choose :wink:

0 Likes

#14

Wow! I don’t even know where to begin.

When you invoke timer.setTimer(long d, timer_callback f, int n) in your setup() routine, it’s going to immediately start invoking the timer_callback() function n times, once every d milliseconds. If that’s not what you’re after, we need to find a more appropriate place to start the timer.

If your loop() function calls gatelock() and gatelock() calls delay(2000) and delay(3000), that’s the same as the loop() function calling delay(2000) and delay(3000).

We need to agree on a starting point. Your sketch seems to be evolving rather rapidly with each new post. Let’s start with something like this,

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

void setup(void) {
   Serial.begin(115200);
   Blynk.begin(auth, ssid, pass);
}

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

then grow your sketch one step at a time. Don’t add anything to the loop() routine. Don’t invoke the delay() function anywhere. Incorporate one aspect of your sketch. You seem to want to send an E-mail containing a random number. Let’s start with that.

2 Likes

#15

I did it with timers , I swear I sing up here to ask this question :laughing: while Pete & Gunner misunderstood me… actually I did it at that time with only monitoring, now I want to add some features “email” , well let’s see a new sketch :smiley::
hmm everything works except email :

#include <Servo.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SoftwareSerial.h>
 

char auth[] = " ";
char ssid[] = " ";
char pass[] = " ";
String Email ;
String vcode ;
String randNumber;
  

// Global variables are here

Servo myservo;
SimpleTimer timer;


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  Blynk.virtualWrite(V17, 0 ); 
  myservo.attach(D6);
  Blynk.email(Email.c_str() ,  "YOUR  CODE" , randNumber ); 

  timer.setInterval(1000L, park1 );
  timer.setInterval(1000L, park2 );
  timer.setInterval(1000L, park3 );
}


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


void sendtoemail ()
{
Blynk.virtualWrite(V17 , HIGH );                     // keep switch ON
Serial.print(" Park 3 reserved at :  ");          
Serial.print(Clock1);                             
Serial.print(" sec");                             
Clock1 ++;
Serial.println(); 
Serial.println("Email in function is :");       
Serial.println(Email); 
  
  if ( Email == "" )
  {}
  else 
  {
    Blynk.email(Email.c_str() ,  "YOUR  CODE" , randNumber ); 
    Clock1 = 0; 
    timer.disable( timerID1 );
    timerID2 = timer.setTimer(1000L, vercode , 60 );
   }
   if ( Clock1 >= 30 )
   {
    Clock1 = 0;
    timer.setTimeout(1000L, refresh  );
   }
}

void vercode ()
{
  
  Serial.print ( " Waiting your code , " );
  Serial.print (Clock2);
  Serial.print ( "sec" );
  Clock2 ++;

  
   if ( vcode == randNumber )
  {
    digitalWrite ( LED3res   , HIGH );
    vcode = "";                          
    timer.disable( timerID2 );
    timerID3 = timer.setTimer(1000L, gatelock , 60 ); 
  }
   if ( Clock2 >= 59 ) 
  {
     Clock2 = 0;         
     timer.setTimeout(1000L, refresh  );     
  }
}

void gatelock ()
{
  Serial.print ( " Gate is waiting you :   " );
  Serial.print (Clock3);
  Serial.print ( "sec" );
  Clock3 ++ ;
  Blynk.virtualWrite(V17 , HIGH ); 
  
 
  if ( vcode == randNumber )
  {
    timer.disable(timerID3); 
    myservo.write(80);
    delay (3000);
    myservo.write(160);
    timer.setTimeout(5000L, refresh  );
  }
  if ( Clock3 >= 59 ){
     Clock3    = 0;               
     timer.setTimeout(1000L, refresh  );            
  }
}

void refresh ()
{
  Serial.print ( " ( Time is up ) " );
  vcode = "";
  Email = "";
  digitalWrite ( LED3res  , LOW );
  Blynk.virtualWrite(V17 , LOW );
}



BLYNK_WRITE(V6)
{
    Email = param.asStr();
}

BLYNK_WRITE(V8){
   vcode = param.asStr();
   Serial.print(" verification code from Blynk is : " ); 
   Serial.print( vcode ); 
}

   
BLYNK_WRITE(V17){
  int resButton = param.asInt();  
 if ( resButton == 1 )
 { 
   String Email = "" ;
   String vcode = "" ;
   randNumber = random(1000,3000);
   Serial.println(" Verification Code is : ");
   Serial.print (randNumber);
   Serial.println();
   timerID1 = timer.setTimer(1000L, sendtoemail , 30 );
 }
}


void park1 ()
{}
void park2 ()
{}
void park3 ()
{}
0 Likes

#16

No misunderstanding… yes, it can be done in the loop(), it is just the wrong way to do it with a background running process like Blynk.

You might want to space out your timers a bit… The way it is now, each one is trying to run it’s respective function at the exact same time every second.

I have no idea what you are doing with email so can’t help there, sorry.

1 Like

#17

Where are the following variables declared?

Clock1;
Clock2;
Clock3;

timerID1;
timerID2;
timerID3;

So you can’t send a single E-mail? I assume you’ve included the E-mail widget in your project. Can you try specifying the E-mail address as part of the E-mail widget and just test,

Blynk.email("subject", "body");

as part of your setup() routine.

1 Like

#18

And your code compiles, correct? Including this statement,

randNumber = random(1000,3000);

in which you’re trying to assign a long integer to a string?

1 Like

#19

Thanks sir! it was not included, I added it and it works with my blynk app email. I had two Text Input Widgets , one is for giving hardware any email :

BLYNK_WRITE(V6)
{
    Email = param.asStr();
} 

so I’ll use :

  Blynk.email(Email.c_str() ,  "YOUR  CODE  is" , randNumber );  

as part of void setup() to make it general emails, correct ?
and also in void sendtoemail() function :

void sendtoemail ()
{
Blynk.virtualWrite(V17 , HIGH );                     // keep switch ON
Serial.print(" Park 3 reserved at :  ");          
Serial.print(Clock1);                             
Serial.print(" sec");                           
Clock1 ++;
Serial.println(); 
Serial.println("Email in function is :");       
Serial.println(Email);
Serial.println();
Serial.println(randNumber);
 
 if ( Email == "" )
 {}
 else 
 {
   Blynk.email(Email.c_str() ,  "YOUR  CODE  is" , randNumber ); 
   Clock1 = 0; 
   timer.disable( timerID1 );
   timerID2 = timer.setTimer(1000L, vercode , 60);
  }
  if ( Clock1 >= 30 )
  {
   Clock1 = 0;
   timer.setTimeout(1000L, refresh  );
  }
}

I tried this way, email widget still has my Blynk login email.!
Email.c_str() and it prints emails correctly in serial monitor.
( compiling is done correctly including randNumber = random(1000,3000);) .

0 Likes

#20

@wickedbeernut

it works now :grinning: I should delete my email then run it to accept any email, after I stopped the project blynk’s one came back ! I got it ; just to avoid sending random emails, so it keeps yours as save way :white_check_mark:

0 Likes