Disconnected after quick (rapidly repetitive) press of button

Hello,

I have noticed that every time and every project I press rapidly a button 4 or 5 times in the blynk app, the nodemcu disconnect and connect it again to my local server.

this is what I do in the code

BLYNK_WRITE(V1) // Boton 1
{
  if (param.asInt()) 
  {
    L1.on();
    digitalWrite(LED1, LOW);
    Timer.setTimeout(tiempo1 * 1000L, apaga1);
  }
}

void apaga1()
{
  L1.off();
  digitalWrite(LED1, HIGH);
}

any suggestion or any known issue on that?

Edit: trying to find the correct character to format the code… :wink:
Edit 2: found!

Thanks!

This snippet is fine… the rest of your code might help?

Any particular reason for wanting to do this?
Space invaders, perhaps?

1 Like

Really? :slight_smile:
it’s no needed a reason for everything, and I know for sure that my wife, or daughter will press this button many times until they see that the button do something…
Could you please try to do it in one of your projects?

This is all my code:

#define BLYNK_PRINT Serial
#include "RiegoDani.h"

#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>

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

WidgetLED L1(V21);
WidgetLED L2(V22);
WidgetLED L3(V23);
WidgetLED L4(V24);

void CheckConnectionProc()
{
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      Serial.println("Reconexion OK");
    } else {
      Serial.println("Reconexion no OK");
    }
  }
}


void leeTemp()
{
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V4, temp);
}

BLYNK_CONNECTED()
{
  if (isFirstConnect)
  {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

void apaga1()
{
  L1.off();
  digitalWrite(LED1, HIGH);
}

void apaga2()
{
  L2.off();
  digitalWrite(LED2, HIGH);
}

void apaga3()
{
  L3.off();
}

void lifeLed()
{
  if (!lifeLED)
  {
    L4.on();
    lifeLED = true;
  }
  else
  {
    L4.off();
    lifeLED = false;
  }
}

BLYNK_WRITE(V1) // Boton 1
{
  if (param.asInt()) 
  {
    L1.on();
    digitalWrite(LED1, LOW);
    Timer.setTimeout(tiempo1 * 1000L, apaga1);
  }
}
BLYNK_WRITE(V2) // Boton 2
{
  if (param.asInt()) 
  {
    L2.on();
    digitalWrite(LED2, LOW);
    Timer.setTimeout(tiempo2 * 1000L, apaga2);
  }
}

BLYNK_WRITE(V3) // Boton 3
{
  if (param.asInt()) 
  {
    L3.on();
    Timer.setTimeout(tiempo3 * 1000L, apaga3);
  }
}

BLYNK_WRITE(V11) // Slider 1
{
  tiempo1 = param.asInt(); 
}

BLYNK_WRITE(V12) // Slider 2
{
  tiempo2 = param.asInt(); 
}

BLYNK_WRITE(V13) // Slider 3
{
  tiempo3 = param.asInt(); 
}

void setup()
{
  Serial.begin(9600);

  sensors.begin();
  leeTemp();

  WiFiManager wifi;
  wifi.autoConnect("Mizuyari 2.0"); 
  Blynk.config(auth, "noipaddress.ddns.net");

  pinMode(LED1, OUTPUT);
  digitalWrite(LED1, HIGH);

  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, HIGH);

  Timer.setInterval(5000L, leeTemp);
  Timer.setInterval(10000L, CheckConnectionProc);
  Timer.setInterval(3000L, lifeLed);
  
}

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

And RiegoDani.h


#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS D5
#define LED1 BUILTIN_LED
#define LED2 D4

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

char auth[] = "th15 15 th3 4uth c0d3";

float temp = 0;

bool isFirstConnect = true;
bool lifeLED = false;

int tiempo1 = 0;
int tiempo2 = 0;
int tiempo3 = 0;

SimpleTimer Timer;


Thanks!

Not quite true… there is a reason you are getting disconnected and it is probably due to too much too quickly (if you run debug to the IDE monitor, you will probably see flood errors).

If you are unable to control the button pushers, then you need to force specific action in the code. In your case perhaps making it so the button MUST be held down for a set time before action, else it cancels the requested action. And perhaps making a LED widget light up AFTER the button has been held for the correct time… to provide positive feedback to the game player… I mean button pusher :wink:

Check out this example… I believe your code is missing the else statement that makes it work. Tweak as required for your application. [SOLVED] Virtual button pressed for X seconds - #36 by Costas

@darkmoon when Blynk introduced the setProperty() function, as a proof of concept, I built my “Space Invaders” game to run on Blynk.

It was simply to familiarise myself with setProperty() and to have some idea of the limitations regarding the frequency at which buttons could be pressed without flooding the server.

I was quite surprised at just how good the server handled rapid button presses and the details of the SmartBlynkie™© are available at SmartBlynkie™© ioT Game

I knocked up your project and guessed at some of your settings:

Few observations / comments:

9600 baud is for “old school” hardware, use 115200 for ESP’s in standalone mode.

Assume display widget for temperature is set at PUSH frequency.

Assume buttons are set as PUSH mode.

Set slider values as 1 to 5. What values are you using?

You declare tiempoX as 0 which would be normal procedure, but as your sketch is written, if the sliders are not moved before you start pressing the buttons then the setTimeouts will also equate to zero.

You could add #define BLYNK_DEBUG as the first line of your sketch but to be honest the debug info doesn’t really help very much.

I added the following to RiegoDani.h:
unsigned int pressed;

and the following to V1:

pressed++;
Serial.println(pressed);

and in CheckConnectionProc() added:
pressed = 0;

I get varying results from your sketch depending on if I move the slider before I start pressing the buttons. I was able to drop the server connection with 3 or 4 rapid button presses on occasions, other times took around 10 button presses and others were as high as 30 button presses.

10 was a fairly common result which makes me think it is due to the maximum of 10 timers running for each instance of SimpleTimer.

You should look at the way you have coded up the setTimeout and as pointed out by @Gunner code up a fix if you think users are going to treat your project as a game of Space Invaders.

1 Like

Hey Costas, you were very close in your app design… :slight_smile:

If I’m not wrong with syncall tiempo variables should be populated with last data in app…

I’m going to try to delete the timers after use as gunner tolds to see if something works better but I think in other projects without timers it also happens… we will se

BTW, thanks for your support

Blynk adds a great mobile GUI to our many projects, however all that cross Phone ↔ Server ↔ Device communication does come with a few catches…

Not to mention the ESP8266’s own WiFi timing issue (i.e. don’t block anything in code too long, else it may drop the WiFi connection).

Makes that “timing is everything” saying very relevant in Blynk projects :wink:

@darkmoon sorry I missed the syncAll() call in the BLYNK_CONNECTED() function.

this will be true for all “connected” projects :wink:

1 Like