Notification and switches

is there any way that i can get the notification when i trigger the switch only. because the below code always pop out notification whether i trigger switches or not. sorry for my bad english

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "5W_asadjakPPPSjdsakdapP"; //Enter the authentication code sent by Blynk to your Email
char ssid[] = "my wifi"; //Enter your WIFI SSID
char pass[] = "1231231231"; //Enter your WIFI Password

void HammerSwitch()
{
  int Switch1 = digitalRead(5);
  if (Switch1==1) 
  {
    Serial.println("Hammer is not in place");
    Blynk.notify("Alert : Hammer is not in place");   
  }
  else if (Switch1==0)
  {
    Serial.println("Hammer is in place");
    Blynk.notify("Alert : Hammer is in place");
  }
}
void HacksawSwitch()
{
  int Switch2 = digitalRead(4);
  if (Switch2==1)
  {
    Serial.println("Hacksaw is not in place");
    Blynk.notify("Alert : Hacksaw is not in place");
  }
  else if (Switch2==0) 
   {
    Serial.println("Hacksaw is in place");
    Blynk.notify("Alert : Hacksaw is in place");
  }
}


void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(5,INPUT);
pinMode(4,INPUT);
timer.setInterval(10000L,HammerSwitch); 
timer.setInterval(10000L,HacksawSwitch); 
}

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

Not totally clear what you are after.

1- you are calling the functions on a 10s timer so remove them from your void loop

By switch do you mean a physical switch or just the button on your app?

Not the switch on the blynk app…there are 2 physical switch on my breadboard…the result that i want is when i trigger either these two switches, the blynk app will sent the notification…i tried to remove the timer…but did not receive any notification on the phone when i trigger the switches

Your void loop repeats constantly, and should be executing hundreds of times per second (or more).

Your HammerSwitch and HacksawSwitch functions are therefore being called constantly. They each contain ‘if’ statements which will ALWAYS result in a notification message being sent - either one to say that the hammer/hacksaw is in place or isn’t in place.

Because of the frequency that you’re trying to send these notifications you’re are flooding the Blynk server with messages, and the server is limiting the rate at which the requests are sent.

Read this for more info…

There are several strategies for overcoming this, the moat sensible one is to use interrupts on the switches to trigger your routines only when the switches are pressed.
An alternative is to use flags to track when a message has been sent and only send another if the content of the message would be different from last time.

Pete.

1 Like

im sorry if i asked to many questions…how do i add interrupts in the code?

As this isn’t a Blynk specific thing, I’d suggest you try googling, although there are plenty of examples on this forum too.

Pete.