Email when physical button is pressed

Hey guys, I was trying some things out with the email code where if a button is pressed then the blynk server will send an email. I hooked up to pin 2 and ground with a button and when I press the button I get an email, but the arduino disconnects from blynk. How can I fix this? I am using an arduino uno with a lan shield.

this is the code that I used.

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth key"
#define W5100_CS  10
#define SDCARD_CS 4

void notifyOnButtonPress()
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(2);
  if (isButtonPressed) {
    Serial.println("Button is pressed.");

    // Note:
    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Yaaay... button is pressed!");

void setup()
  // Debug console

  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // You can also specify server:
  //Blynk.begin(auth, "", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8442);

  // Setup notification button on pin 2
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, CHANGE);

void loop()

Currently your code spams the notification command and thus Bynk will disconnect you as its breaks the Flooding rules.

You need to set a flag like notifySent = 1 directly after sending an notification and also check for the flag before sending so it doesnt spam the command… then using a timer to reset the flag a reasonable time later like 5 minutes.

1 Like

Sorry for the late response. But, I suppose its something like this?

void emailOnButtonPress()
  int notifySent = 0; 
  int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"
  if (isButtonPressed) // You can write any condition to trigger e-mail sending
   if(notifySent == 1) 
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
   Serial.println("Notification Sent");


Oh and I am no longer using the blynk notification code and instead am using the blynk email example code.

Remove the 100 second delay (never use delays with blynk as it causes heartbeat timeouts).

Try this:

BlynkTimer timer; // setup blynk timer instance
int notifySent; // global var
int notifyTimeout = 20; // timeout in seconds
void emailOnButtonPress() {
  if(!digitalRead(2) && !notifySent){  // if button pressed and notifySent = 0 so send email
    Serial.println("Button is pressed. Notification Sent"); // Serial output for debug only"", "Shaker", "Shaker has stopped."); // send email
    notifySent = 1; // notify flag set
    timer.setTimeout(notifyTimeout * 1000, [](){
      notifySent = 0; // notify flag reset after non-blocking timeout
  } else if(!digitalRead(2) && notifySent){
    Serial.println("Button is pressed but still in notification cool down of " + notifyTimeout + String(" sec")); // Serial debug 

Hi Jamin,
I tried the code and it sent an email the first the button is pressed, but the button presses after that no longer send emails but instead shows up on the serial monitor as "Button is pressed but still in notification cool down. And I changed the timeout so that it would be 20 seconds and I did wait it out to see if the timeout would stop but it stayed the same.

Put another serial output below the notifySent = 0 line and set the timeout to 5 seconds and press the button. Wait 5 seconds for the new serial output comes up as reset.

notifySent= 0;
Serial.println("notifySent has reset. Can now send email again");

Also make sure you have setup the timer in the loop.;

1 Like

The code works perfectly. Thanks for the help!

1 Like

Hi Pavee, could you share me your complete code regarding for this email?