[SOLVED]Simple push &email notification not working on Nodemcu

hii guys,
I am not getting push notification. I am using Nodemcu ESP8266 12e.
this is my code

                #include <ESP8266WiFi.h>
                #include <BlynkSimpleEsp8266.h>
                #include <SimpleTimer.h>
                // You should get Auth Token in the Blynk App.
                // Go to the Project Settings (nut icon).
                char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                const int btnPin = 5;


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

                    Blynk.notify("Yaaay... button is pressed!");
                  }
                }



                void setup()
                {
                  Serial.begin(115200);
                  Blynk.begin(auth, "killer k3", "sa1234567");
                  
                  // Attach pin 2 interrupt to our handler
                  pinMode(btnPin, INPUT_PULLUP);

                  while (Blynk.connect() == false) {
                    // Wait until connected
                  }
                pinMode(5, INPUT_PULLUP);
                  // Attach pin 2 interrupt to our handler
                  attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);

                }

                // V3 LED Widget represents the physical button state



                BLYNK_WRITE(V5)
                {
                  // You'll get HIGH/1 at startTime and LOW/0 at stopTime.
                  // this method will be triggered every day
                  // until you remove widget or stop project or
                  // clean stop/start fields of widget
                  Serial.print("Got a value: ");
                  Serial.println(param.asStr());
                }
                void loop()
                {
                 Blynk.run();


                }  

I also getting button is pressed in serial monitor
this output.& also not getting notiffcation when hardware goes offline.


please help guys.

can you post your code like this:

I am on phone.
The symbols are really small i can’t see this.
Please try to understand my code.

ha ha - OK mate :slight_smile: ill give it a go!

where did you get this code from?

which example is it?

It is combination of esp standalone & push notification

did you try this one:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   Blynk.notify("Device started");
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

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

I will try this. Now I’m in college.
Thanks

I uploaded your sketch to Nodemcu but not succeed.:sweat:
Getting this output

[1061] Connected to WiFi
[1061] IP: 192.168.43.68
[1061] Blynk v0.3.8 on NodeMCU
[5001] Connecting to blynk-cloud.com:8442
[5231] Ready (ping: 2ms).
Button is pressed.

then
for testing email notification
i have added this to your code
email notification also not working.:sob:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void emailOnButtonPress()
{
  // *** WARNING: You are limited to send ONLY ONE E-MAIL PER MINUTE! ***

  // Let's send an e-mail when you press the button
  // connected to digital pin 2 on your Arduino

  int isButtonPressed = !digitalRead(5); // Invert state, since button is "Active LOW"

  if (isButtonPressed) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("saurabhgangamwar78@mail.com", "Subject: Button Logger", "You just pushed the button...");

  }
}
void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "killer k3", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   Blynk.notify("Device started");
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
   attachInterrupt(digitalPinToInterrupt(5), emailOnButtonPress, CHANGE);
}

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

please help me guys I’m really frustrated.

Don’t get why if you put this line in code, it will read it. Blynk.notify(“Device started”);

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   Blynk.notify("Device started");
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

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

But if this is put in the if statement, it not reading it at all. Blynk.notify(“Button is pressed.”);

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   ///////////////////////////Blynk.notify("Device started");
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

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

When

Blynk.notify("Device started");

is triggered in setup()… you then have to wait up to 60seconds before another notification can be sent.
Are you waiting up to 2 minutes after you receive the first notification BEFORE pressing the button to trigger another?

If you want to bypass the 60second limit you must create your own local server and pay for your own SMS notifiations.

Also read up on Priority notifications here: Introduction - Blynk Documentation

“Priority” - high priority gives more chances that your message will be delivered without any delays.
Limitations: Maximum allowed body length is 120 symbols.
Only 1 notification per minute is allowed

i have removed this from code.
but not succeed.
I think it is happening due to network.
Nodemcu is connected to WPA2 wiffi.

The statement after the if code, Blynk.notify(“Button is pressed.”);
I wait for 1/2 hour to 5 min. to push the button, and not A thing will happen.
I do know that I have to wait for 60 second. The Blynk code it not reading it.
Upload it to A esp8266-12e and you will see what I am talking about.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

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

If I upload this code, the push notification is sent to the ipod and it goes off, at start up and if you
wait 2 min. and pin 5 goes low, push notification go to ipod. Its sending the notification ok, but not right. I still don’t get why it is reading one and not the other one?

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

void setup() {
   Serial.begin(9600); 
   Blynk.begin(auth, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxx");
   while (Blynk.connect() == false) {
      // Wait until connected 
   }
   Blynk.notify("Device started");
   pinMode(5, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

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

Straight away I see a massive problem.

When you press the button ONCE, it shows TWO Serial outputs as you can see in the image below.

What you’re doing when you press the button is flooding the service.

Also using interupts seem to mess with things. I recoded it to make it work at my end using the cloud servers.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxx";

int buttonStatePrev;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxxxxxx", "xxxxxxxxxx");
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  pinMode(5, INPUT);
  buttonStatePrev = HIGH;
}

void loop() {
  Blynk.run();
  
  int buttonStateCurr = digitalRead(5);
  if (buttonStateCurr == LOW && buttonStatePrev == HIGH) {
    Serial.println("Button is held down but activated the function only ONCE.");
    delay(50);
  }
  buttonStatePrev = buttonStateCurr;
}
2 Likes

Jamin

You are right about sending too many notifications
at one time. You’re new code is what I was looking for thank you for your help and thank you for your time.

I have tried your code.
i have added Blynk.email
under if statement
but not succeed:disappointed :cry:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxx";

int buttonStatePrev;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxxxxxxxxx", 'xxxxxxxxxxxx");
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  pinMode(5, INPUT);
  buttonStatePrev = HIGH;
}

void loop() {
  Blynk.run();
  
  int buttonStateCurr = digitalRead(5);
  if (buttonStateCurr == LOW && buttonStatePrev == HIGH) {
    Serial.println("Button is held down but activated the function only ONCE.");
     Blynk.email("saurabhgangamwar78@gmail.com", "Subject: Button Logger", "You just pushed the button...");

    delay(50);
  }
  buttonStatePrev = buttonStateCurr;
}

what is your serial output saying?

â=Qýþ[244] Connecting to new
[4246] Connected to WiFi
[4246] IP: 192.168.43.17
[4246] Blynk v0.3.8 on NodeMCU
[5001] Connecting to blynk-cloud.com:8442
[5242] Ready (ping: 0ms).
Button is held down but activated the function only ONCE.

i got succees. :slight_smile:
the main problem was internet.
I have connected my Nodemcu different wiffi router & got succees.
push notification also working.

2 Likes

Problem solved. :slight_smile:

2 Likes

FYI: You could just change the interupt from CHANGE to RISING or FALLING so the interupt routine is only called once.