Help with push button notification

Hello friends,
I used the project for a long time blink legacy and esp32 wifi module which controls the relays and has one push button which sends me notifications on the blynk app
doc-how-pullup-resistor-works-for-esp32
now i want to switch to blynk iot and I researched the code and the outputs for the relays work but the push button which sends me notifications on the blynk iot android don’t work, I assume that the problem is with the code, so please can someone help me and write where I went wrong.

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "........"
#define BLYNK_DEVICE_NAME "...."

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

int output1 = 12;
int output2 = 13;
int output3 = 14;
int input1 = 18;





BLYNK_WRITE(V12) {
int pinvalue = param.asInt();
digitalWrite (output1,pinvalue);
}

BLYNK_WRITE(V13) {
int pinvalue = param.asInt();
digitalWrite (output2,pinvalue);
}

BLYNK_WRITE(V14) {
int pinvalue = param.asInt();
digitalWrite (output3,pinvalue);

}


void setup()
{
  Serial.begin(115200);
  delay(100);

  pinMode(output1,OUTPUT);
  pinMode(output2,OUTPUT);
  pinMode(output3,OUTPUT);
  pinMode(input1,INPUT);

  BlynkEdgent.begin();




}

void loop() {
  BlynkEdgent.run();

   if(digitalRead(input1) == LOW) {
 Blynk.logEvent("event_code");
}

}



please add this and show us serial monitor

if(digitalRead(input1) == LOW) {
 Serial.print("button low");
 Blynk.logEvent("event_code");
}

You should read this

This should be inside a separate void function, not in the void loop, and you should use a timer to call it.

I think ESP is resetting when he pushes the button, it’s why I want to see his serial monitor.
Better use interrupts and debouncing …

Yes, the esp32 is resetting after the push button

serial monitor shows me this

R�"�z� ���������������������������w�������������{�����������z����������������|�������{��������������������g����������������������_�

you have to set 115200 on your serial monitor

[4155] Certificate OK

[4197] Ready (ping: 41ms).

[4266] CONNECTING_CLOUD => RUNNING

button lowbutton lowbutton lowbutton lowbutton lowbutton lowbutton lowbutton lowbutton lowbutton

Did you push the button one or more time ?

only once

Better use interrupts and debouncing …

You could eliminate the need for the resistor if you changed your pinMode statement to INPUT_PULLUP instead of simpler INPUT.

You certainly need to clean-up your void loop. You should either use a timer to call a function that polls the pin that has the button attached, or use an interrupt and some debounce code. If you use a timer then polling the button every 100ms is s good starting point.

You will also need to be careful about the limit of one notification per minute, and 100 notifications per 24 hour period. Using a flag variable, in a similar way to how you’d do switch debouncing, is probably a good idea too, so that if the button is kept pressed it won’t keep sending events to Blynk.

I’m guessing that the event you’ve created doesn’t actually have an event code of event_code, so that will need to be changed, and you’ll need to ensure that it’s actually the event code rather the name that’s used.

Pete.

I am currently using this code on blink legacy and everything works perfectly so I don’t want to change the connected resistor on a working project
I have no experience with writing code, I’m a beginner in this world, so I thought that someone could, based on the old code, tell me what to do with the new code so that the project would work on blynk iot


#include <WiFi.h>

#include <BlynkSimpleEsp32.h>

 

int ulaz1 = 5;

 

const char* ssid = "Salas_net"; // Enter your Wifi name

const char* password = "salas.net21"; // Enter wifi password

char auth[] = "...................................."; // Enter the Auth Token provied by Blynk app

 

void setup()

{

Serial.begin(9600);

pinMode(ulaz1,INPUT);

Blynk.begin(auth, ssid, password);

}

 

void loop() // put your main code here, to run repeatedly

{

Blynk.run();

if(digitalRead(ulaz1) == LOW)

{

Serial.println("Send Notification to Blynk");

Blynk.notify("Salas detektovano kretanje !!! ");

 

}

}

The problem is that your sketch is badly flawed, and breaks the “keep your void loop clean” rules that should have been applied - even with Legacy.

You’ve chosen to use the Edgent sketch rather than simply fixing your existing sketch and updating it to work with Blynk IoT.
Edgent is less forgiving when it comes to poor programming practice, and the notification process in IoT has restrictions built-in to prevent a silly number of events from being logged and notifications sent.

You’ll need to fix your poorly written code to use it with IoT.

If you want to stick with the external pull-up resistor rather than activating the internal pull-up on your switch pin that’s fine, I’m simply pointing out that this piece of external hardware is not required.

Pete.

1 Like

Thank you all, i researched and modified the code and came up with the result
but i have a new problem


// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "........"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "....."


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


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int barijera = 12;
int sat_220V = 13;
int rigovi = 14;
int ulaz_d18 = 18;
int ulaz_d19 = 19;
int ulaz_d21 = 21;


BLYNK_WRITE(V12) {
int pinvalue = param.asInt();
digitalWrite (barijera,pinvalue);
}

BLYNK_WRITE(V13) {
int pinvalue = param.asInt();
digitalWrite (sat_220V,pinvalue);
}

BLYNK_WRITE(V14) {
int pinvalue = param.asInt();
digitalWrite (rigovi,pinvalue);

}

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}
  
//okidanje dojave
void ulaztimer()

{

    if(digitalRead(ulaz_d18) == LOW){
Blynk.logEvent("ulaz_d18");
  }
    if(digitalRead(ulaz_d19) == HIGH){
Blynk.logEvent("ulaz_d19");
  }
      if(digitalRead(ulaz_d21) == LOW){
Blynk.logEvent("ulaz_d21");
  }
 

}

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

   delay(100);

  pinMode(barijera,OUTPUT);
  pinMode(sat_220V,OUTPUT);
  pinMode(rigovi,OUTPUT);
  pinMode(ulaz_d18,INPUT);
  pinMode(ulaz_d19,INPUT);
  pinMode(ulaz_d21,INPUT);

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

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(300L,ulaztimer);


}

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!
}

When the status of some of the pins d18, d19 or d21 changes, the notification works, but the blynk server soon blocks me due to an excessive number of spamming
The state change on those pins sometimes remains permanent for half a second, sometimes a couple of minutes and then returns to the previous state
During that period of changing the state and staying in that state for a few minutes, the blink server block notifications for 24 hours
How to make a pause in the code when a change of state occurs on a pin and the blynk notification passes so that the timer after the first notification takes a break of a few minutes and then starts again and waits for a change of state on one of the specified pins?
Thank you for your answers :slight_smile:

I’d start by reading this…

Pete.

It also makes sense to adopt a policy of using notifications for “exception reporting” where you send notifications to users if an exceptional situation occurs, not a run-of-the-mill situation which occurs very frequently through the day.

If you don’t take this approach then you risk flooding the user with lots of unnecessary notifications and creating a “notification fatigue” situation where notifications are frequently missed or ignored, because they are insignificant and have little meaning.

Pete.