While using magnetic switch on interrupt, getting Stack Dump and NodeMCU disconnects

#define BLYNK_PRINT Serial


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

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CPH1613";
char pass[] = "alok123456";

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = digitalRead(D1);
  if (isButtonPressed) {
    Serial.println("Someone Opened the door");

    // Note:
    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Someone Opened the door");
    
  }
 
}

void setup()
{
  // Debug console
  Serial.begin(9600);

Blynk.begin(auth, ssid, pass);
  // Setup notification button on pin 2
  pinMode(D1,INPUT_PULLUP);
  
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(D1), notifyOnButtonPress, CHANGE);
  delay(5000);
}
void loop()
{
  Blynk.run();
  
}

Here i am using a magnetic switch to get the status of my door, it works for sometimes, then get stuck and board reconnects and most of the time it through this stack values.
Help please.

You might be getting a massive button bounce effect? Basically crashing the system from hitting the interrupt too much too fast.

Instead of the interrupt pin, use a timer (BlynkTimer) and simply check the pin status every 1/4 second or so… include some form of time limit (another timer and flag) to keep from sending notifications more than once every 15 seconds.

http://docs.blynk.cc/#blynk-firmware-blynktimer

Hey, Gunner !
Will you please point out the line in the code where i hv to change.
I am not getting it !

Or Edit the code if you can.

There is no one line… basicly a different approach, thus almost totally different code… and no, I won’t write it for you :wink:

Search this forum for BlynkTimer examples as well as work through the Help Center documents and Example sketches to understand timer use.

https://examples.blynk.cc/?board=Arduino%20Nano&shield=Serial1&example=GettingStarted%2FPushData

Ok. Gunner !
NIce Shot…
Will do it by myself…:sunglasses:

Yes, just like all of us… but you will learn quicker with our assistance (well, if you listen to advice and check out offered links and so on). :wink:

Here is another example sketch that polls for a button press… no timers, but has some debouncing included…

…mix in a proper timer routine or two and you can have a good system for monitoring and notifying you of your door status.

Its Worked…A BIG Thanks…

Here is what is did !

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;


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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CPH1613";
char pass[] = "alok123456";

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = digitalRead(D1);
  if (isButtonPressed) {
    Serial.println("Someone Opened the door");

    // Note:
    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Someone Opened the door");
    
  }
 
}

void setup()
{
  // Debug console
  Serial.begin(9600);

Blynk.begin(auth, ssid, pass);
  // Setup notification button on pin 2
  pinMode(D1,INPUT_PULLUP);
  
  // Attach pin 2 interrupt to our handler
 // attachInterrupt(digitalPinToInterrupt(D1), notifyOnButtonPress, CHANGE);
 timer.setInterval(16000L,notifyOnButtonPress);
 
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

  
}

Any hints : What to do ,if once it notified me that the door is open then hold the notification until there is a CHANGE in the status of the door instead of sending me the notification for every 15 seconds.

Made that too.

Final Code : For DOOR Notification on Blynk APP using NodeMCU and Magnet Sensor.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;


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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CPH1613";
char pass[] = "alok123456";
int flag=0;

void notifyOnButtonPress()
{
  int isButtonPressed = digitalRead(D1);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Someone Opened the door");

    
    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Someone Opened the door");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
  
 
}

void setup()
{
  // Debug console
  Serial.begin(9600);

Blynk.begin(auth, ssid, pass);
  // Setup notification button on pin D1
  pinMode(D1,INPUT_PULLUP);
  
  
 timer.setInterval(16000L,notifyOnButtonPress);
 
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

  
}
2 Likes

I haven’t completely tested this, but change your existing timer to something quicker, every second perhaps, then swap in this void notifyOnButtonPress() function; It should reset your notification flag in 16 seconds after the door is triggered thus limiting the notification to the once per 16 seconds… while still allowing the regular sensor scanning and serial printouts.

void notifyOnButtonPress()
{
  int isButtonPressed = digitalRead(D1);
  if (isButtonPressed == 1 {
  Serial.println("Someone Opened the door");

    if flag == 0) {
    Blynk.notify("Someone Opened the door");  // We allow 1 notification per 15 seconds for now.
      flag = 1; // Sets flag preventing further notifications until reset

// This next three lines are something called a Lambda Function - it compacts a normally separate function around the timeout
      timer.setTimeout(16000L, []() {  // resets notification flag in 16 seconds
        flag = 0;
      });
      
    }
  }
}
1 Like

Wondefull code…
I want a virtual pin with D1 in your code… can u modify for me plllllzzzzz

Aside from reopening old topics… how would you know if it is wonderful code if you can’t understand the code enough to add in your own virtual pin function? :stuck_out_tongue: I think you have the wrong impression of what this forum is here for. HINT, not as code monkeys on demand :wink: