#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.
#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.
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
}
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;
});
}
}
}
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? I think you have the wrong impression of what this forum is here for. HINT, not as code monkeys on demand