Need help to get events

Hello,

I am very new to Blynk. I had a previous project in the legacy app for opening my garage door with ESP 8266. IT seemed to work great and the occasional freeze requiring reboot of the router and the esp device. Since Blynk IOT, cannot get it to work. I tried diffrent things.

I need to have a counter that tell me when the status of digital pin 2 has changed to low I think in order to activate a counter and if greater than 5 minutes if sends a notification repeatidly every minute ontil the garage door closes. I also would want to be able to deactivate this notification.

Here is the old sketch

#include <Blynk.h>

/*************************************************************
This is the latest and likely final rev of my Smartphone Garage Door Opener sketch
using the Adafruit Feather Huzzah board and the Blynk framework. It's pretty robust 
and yells at you when you leave the garage door open too long. I hope it serves you well.

Tyler Winegarner, 2017

/* Comment this out to disable prints and save space */
#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[] = ########

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

// Select your pin with physical button
const int doorPin = 2;
int warnPin;
int warnThreshold = 2000;
int tick = 0;
WidgetLCD lcd(V3);



BlynkTimer timer;

BLYNK_WRITE(V1) {
  warnPin = param.asInt();
}


// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  
  // If state has changed...
  if (digitalRead(doorPin) == HIGH) {
   lcd.print(0,0, "Porte Garage Ouverte  "); means garage door open in french
   tick++;
  }  else {
   lcd.print(0,0, "Porte Garage Fermée"); means garage door cosed in french
    
   tick = 0;
   Blynk.virtualWrite(warnPin, LOW);
  }
  if (tick > warnThreshold) {
    if (warnPin == LOW) {
      Blynk.notify("LE GARAGE EST OUVERT!"); warning me the garage door is opened pass 5 min.
      tick = (tick - 500);
    }
  }
  Blynk.virtualWrite(V2, tick);
}

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

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup physical button pin (active low)
  pinMode(doorPin, INPUT);

  timer.setInterval(10, buttonLedWidget);
}

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

My new sketch.

Work in progress. :frowning:

/*************************************************************

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - ESP8266 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

"" /* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID          ########
#define BLYNK_TEMPLATE_NAME         "Quickstart Device"
#define BLYNK_AUTH_TOKEN            #######

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

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





// Select your pin with physical button
const int doorPin = 2;
int warnPin;
int warnThreshold = 100;
int tick = 0;


WidgetLCD lcd(V3);

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(doorpin)/*devrait etre V0*/
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V4, value);
}

BLYNK_WRITE(V1) {
  warnPin = param.asInt();
}


// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  
  // If state has changed...
  if (digitalRead(doorPin) == HIGH) {
   lcd.print(0,0, "Porte Gar Fermee"); means garage door closed
   tick++;
  }  else {
   lcd.print(0,0, "Porte Gar Ouvert"); means garage door opened
   
      tick = 0;
   
 Blynk.virtualWrite(warnPin, LOW);
  }
  if (tick > warnThreshold) {
  if (warnPin == HIGH) {Blynk.logEvent("test3");
         /*Blynk.notify("LE GARAGE EST OUVERT!")*/ 
      tick = (tick - 500);
      
    }
  }
  Blynk.virtualWrite(V2, tick);
}

void setup()

{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);


  // Setup physical button pin (active low)
  pinMode(doorPin, INPUT);

  timer.setInterval(10, buttonLedWidget);
}
void loop()
{
  Blynk.run();
  timer.run();
}  

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

Iphone 11 as device.

Help would be appreciated. Please remember, very new to this.

Check the documentation
https://docs.blynk.io/en/blynk.console/templates/events

thank you John,
I did read the articles but for the life of me cannot get it to work. I does work to a certain extent but for a reason unknown got notification limits after only several events. Like some posts I saw people saying after only 6 events.

So, I do not know how to proceed or what to look for and need some more guidance

Tks,

Your sketch makes no sense to me.

It might make slightly more sense if you explained what type of widget was attached to each of the virtual pins, and what the operational function of that widget was meant to be.

I certainly can’t understand what you’re doing with the warnPin variable as it’s value is set by whatever is attached to V1 and it’s then used to define which virtual datastream has a 0 written to it in this piece of code…

It then seems to be used in this function (which I’ve changed the formatting on so that I can make some sense of it) as the switch which turns notifications on and off…

void buttonLedWidget()
{
   // Read button
  
  // If state has changed... NOT CORRECT!!
  if (digitalRead(doorPin) == HIGH)
   {
     lcd.print(0,0, "Porte Gar Fermee"); garage door closed
     tick++;
   }
  else 
  {
     lcd.print(0,0, "Porte Gar Ouvert"); garage door opened
     tick = 0;
     Blynk.virtualWrite(warnPin, LOW);
  }
  
  if (tick > warnThreshold)
  {
    if (warnPin == HIGH)  // if Send Notifications widget is On?
    {
      Blynk.logEvent("test3");
       //Blynk.notify("LE GARAGE EST OUVERT!")
      tick = (tick - 500);
    }
  }
  Blynk.virtualWrite(V2, tick);
}

As far as the code regarding sending notifications is concerned, you appear to be doing exactly the opposite of what you’ve described here…

There is no change detection for digital pin 2 (doorPin) and your tick counter is being incremented by 1 only when the garage door is closed (doorPin == HIGH).
As the (oddly named) buttonLedWidget() function is called once every second, and warnThreshold is initially set to 100, the first notification will be sent when the garage door has been closed for 100 seconds and subsequent notifications will be sent every 500 seconds.
This means that your 500 events per 24 hour period will be used-up when your garage door is closed for around 83 minutes.

I suspect that when you say “events” you actually mean Notifications.

Blynk has two limits, 100 EVENTS per 24 hour period and 100 NOTIFICATIONS per 24 hour period.

As notifications are triggered by events, then this seems to imply that there is a one to one relationship between the two, but there is a setting in the Event Notifications tab which allows you to say how many events are ignored before a notification is sent.

I’d suggest that you read this tutorial…

Then wait 24 hours before powering-up your device again and turn-on the “Send event to timeline” option and check the Notificatiins Limit period is set to 1 minute and the Event Counter is set to 1

Pete.

Thank you Pete for the reply.

Like I mentionned at the beginning, I am very new and not a programmer and am trying as best as I can. I need some help please.

That being said, I read your comments and went back to look at the sketch again. The D2 can be read and it is indeed working as I expect it. When HIGH, the LCD says door closed. When LOW, door opened.

I have a digital pin 13 connected to a garage door switch which is also working as expected when pushed. It opens the door.

I was able to get the counter to work also has expected. The first event comes in around 1 minute and is repeated after every 30 seconds.

I am now trying to add a button widget that would disable the events when pushed. I mean, it would keep the sketch from registering events “porteouverte” which is the warning that the door is opened. And once the door is closed

I tried all sorts of things with no succes.

Could someone provide some guidance?

This is the new code:


/*************************************************************

You’ll need:

  • Blynk IoT app (download from App Store or Google Play)
  • ESP8266 board
  • Decide how to connect to Blynk
    (USB, Ethernet, Wi-Fi, Bluetooth, …)

There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID “#########”
#define BLYNK_TEMPLATE_NAME “Quickstart Device”
#define BLYNK_AUTH_TOKEN “#######”

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “#######”;
char pass[] = “######”;

// Select your pin with physical button
const int doorPin = 2;
int warnPin;
int tick = 1;
/* 60 equivaut a 1 min*/
int warnThreshold = 60;

WidgetLCD lcd(V3);

BlynkTimer timer;

// V3 LED Widget represents the physical button state
boolean btnState = false;

void LCDWidgetandWarning()
{
// Read doorpin status (D2) magnetic door sensor

// If state has changed…
if (digitalRead(doorPin) == HIGH) {
lcd.print(0,0, “Porte Gar Fermee”);
tick = 0;
}
else
{
lcd.print(0,0, “Porte Gar Ouvert”);
Blynk.virtualWrite(warnPin, LOW);
tick++;
}
if (tick > warnThreshold)
{
if (warnPin == LOW) {Blynk.logEvent(“porteouverte”);
tick = (tick - 30);
}
}

}

void setup()

{
// Debug console
Serial.begin(115200);

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, “blynk.cloud”, 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

// Setup physical button pin (active low)
pinMode(doorPin, INPUT);

// Setting interval to send data to Blynk Cloud to 1000ms.
// It means that data will be sent every second
timer.setInterval(1000, LCDWidgetandWarning);
}
void loop()
{
Blynk.run();
timer.run();
}

// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!


But this is the exact opposite of what the comments in your original (and supposedly working) Legacy sketch said…

Previously D2 HIGH = Door OPEN
Now, D2 HIGH = Door CLOSED

Unless you’ve changed the physical wiring of the switch connected to pin D2 then something odd is happening here. Whichever is the case then you need to be 100% clear with us what the D2 HIGH/LOW statuses represent if you want help with this.

Also, you’ve said multiple times that your code detects whether the status of pin D2 has CHANGED…

In order to know if the pin status has changed your code needs to store the previous value of D2 and compare that to the current value of D2. If the two values are different then the status of pin D2 has changed since the last time that D2 was read.

Your sketch has no code to detect change. It simply reads the current value of D2 and executes the appropriate code every single time that the D2 pin is read.

I flagged-up both of these issues in my initial post, but you’ve dismissed that as being incorrect or irrelevant.
I’d suggest that you study what I’ve written above in detail, along with your original and your latest code, then go back and re-read my initial post.

Once you’ve done that, come back and address the issues I’ve raised.

Pete.

Hello Pete,

Tks for the reply. I did not get back earlier because I was reading up and trying to understand. I have limited knowledge and trying to understand it all.

So in the original sketch, it is accurate. Why? Because my magnetic sensor is normally closed. While I was testing, I did not have the sensor hooked up and therefore did the opposite. By putting a jumper wire between the terminals. So, that is why it gets confusing.

I have finally installed it and go it to work live. It is indeed at High for closed now. And low for open.

I am sorry if I may not say it correctly. To me, it was detecting a status change. That is the issue with newbies, we don’t understand all the language and details. Anyways, in my case that is it.

I did read your articles several times. My head started to hurt. All jokes aside, it not always easy since English is not my mother tongue.

I was able to get the sketch to work. It tells me the status of the door, I have a widget buttons that can disable the events from counting and sending notifications and I have a button that open the door via the Digital 13 pin of the feather huzzah esp8266 board.

I am quite pleased and happy since it now works. I do want to add a led indicator that would be green when the door is closed and red when opened. Why you may asked? Because I am more a visual person than a reader and seeing a red led will get more my attention than the lcd one. But, I do not know how to go about it. I need some help

Lastly, I read several posts to try and get my answers for my problems. I understand that you want people to read more and be more knowledgeable and I get it but, if someone is a beginner and is having issues and says so, in my case, it is because I really do need it. The documentation can be hard to understand at first. I had to read it several times and I am not looking for a quick and easy answer but more guidance.

I really do appreciate that you answer and provide feedback. I am sorry if I was not clear at first. I will get better with time.

Tks.

This is the latest sketch.

/*************************************************************

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - ESP8266 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "#####"
#define BLYNK_TEMPLATE_NAME         "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "######"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

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

// Select your pin with physical button
const int doorPin = 2;
int warnPin;
int tick = 1;
/* 60 equivaut a 1 min*/
int warnThreshold = 300;
int value;
int autowarning = 0; //To say if manual overide or not. Autowarning (0) or override (1)

bool alert_sent = false; 

BlynkTimer timer;

WidgetLCD lcd(V3);

// V3 LED Widget represents the physical button state
BLYNK_WRITE(V2)
{
  autowarning=param.asInt();  // assign the value from the switch widget to our new variable
}
// Added - Force the Blynk server to trigger the BLYNK_WRITE(V2)callback
// when the device boots-up or reconnects to Blynk
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V2);
}

void LCDWidgetandWarning()
{
   // Read doorpin status (D2) magnetic door sensor
  // If state has changed...
  if (digitalRead(doorPin) == High)/*Switcher Low temporairement le temps de tester*/ 
  {
   lcd.print(0,0, "Porte Gar Fermee");
   tick = 0;
   // Pour faire un reset du bouton
  Blynk.virtualWrite(V2, 0);
  // This is for trying to get the led working
  Blynk.virtualWrite(V1, 0);
   }  
  else 
  {
   lcd.print(0,0, "Porte Gar Ouvert");
   Blynk.virtualWrite(warnPin, LOW)/*Switcher Low temporairement le temps de tester*/ ;
   Blynk.virtualWrite(V1, 1);
         tick++;
  }
    if (tick > warnThreshold) 
  {
  if ((warnPin == LOW)&& (autowarning == 0))//autowarning 0 veut dire le bouton est off
   {
    Blynk.logEvent("porteouverte");
      tick = (tick - 90);
    }
    else
      {
      tick = 0;
      autowarning = 1;
      }
   }
}

void setup()

{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);


  // Setup physical button pin (active low)
  pinMode(doorPin, INPUT);

  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(1000, LCDWidgetandWarning);
}
void loop()
{
  Blynk.run();
  timer.run();
}

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

Please edit your post, and add triple backticks ``` before and after your whole sketch.

Sorry. Done!

It would help us (and you when you come to look at this code again) if your in-code comments were accurate. This rxample above should (I think) refer to pin V2 not V3.

There are numerous other cases in your latest sketch where the comments are unclear or inaccurate.

That’s quite difficult in Blynk IoT.
You would need to leave the LED on at all times and change its colour using the Blynk.setProperty command.

Pete.

Thank you once again for all your comments.

I will clean up my comments in my sketch. It is more obvious to me since I am the one doing the coding and testing. I should think if other see it.

That being said, I will see if I can get the led going as I mentionned and from your reply.

Would this be an issue however? I mean, the fact that the led is always on?

If it is an issue, I will think of something else instead.

Tks.
Christian.