[SOLVED] Problem with Blynk/Simpletimer

I got a problem with Blynk/Simpletimer…

Latest IDE / Blynk Lib and Simpletimer Lib.

My timers worked fine as they were in subroutines that are called sometime.

But then I started using them in the main routine and noticed they won’t work in the beginning just after Blynk does connect (see Picture, I measured the millis - Blynk App was closed while testing - mobile phone on standby/screen off).

So I wrote a simple Sketch to test it. Thats also where the picture came from.
(I removed Blynk.run() from the sketch to make it as simple as possible. Got the same result with blynk.run)

As soon as I comment out the connect part the timer is working correctly for me:

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

#define WLANname "XXX"
#define WLANpw "XXX"
#define authToken "XXX"

// Timer definieren
SimpleTimer Timer_A;
SimpleTimer Timer_B;
SimpleTimer Timer_C;

// Timer ID's initialisieren
int Timer_A_ID = 0;
int Timer_B_ID = 0;
int Timer_C_ID = 0;

unsigned long TESTmillis1 = 0;
unsigned long TESTmillis2 = 0;
unsigned long TESTmillis3 = 0;

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

  Timer_A_ID = Timer_A.setInterval(1000L, Test1);
  Timer_B_ID = Timer_B.setInterval(1000L, Test2);
  Timer_C_ID = Timer_C.setInterval(1000L, Test3);

  Blynk.begin(authToken, WLANname, WLANpw);
  Serial.print("\nConnecting to WiFi");
  while (Blynk.connect() == false);

  Serial.println(" ");
  
  TESTmillis1 = millis();
  TESTmillis2 = millis();
  TESTmillis3 = millis();
}

void loop()
{
  Timer_A.run();
  Timer_B.run();
  Timer_C.run();
}

void Test1()
{
  Serial.println("Timer - 1");
  Serial.println((millis() - TESTmillis1));
  TESTmillis1 = millis();
}

void Test2()
{
  Serial.println("Timer - 2");
  Serial.println((millis() - TESTmillis2));
  TESTmillis2 = millis();
}

void Test3()
{
  Serial.println("Timer - 3");
  Serial.println((millis() - TESTmillis3));
  TESTmillis3 = millis();
}

You only have to use one timer instance. Why would you want to run more?

byte Timer_A_ID = Timer.setInterval(1000L, Test1);
byte Timer_B_ID = Timer.setInterval(1000L, Test2);
byte Timer_C_ID = Timer.setInterval(1000L, Test3);

This will do fine. That may be the problem.

I need multiple since I need to activate and deactivate them accordingly in my sketch.
That Library allows 10.

So it should work with Blynk.

@Gorkde each instance allows 10 timers, so you just need 1 instance.

  1. Maybe but it should work anyway with Blynk so this shoud be looked into

  2. Maybe I’m thinking the wrong way but I need Timers that will be used in main as well as many subroutines. Therefore It won’t work for me with ONE Timer instance.

I need:
a timer to blink my Error LED that needs to be independend and able to be enabled and disabled
a timer to blink my Warn LED that needs to be independend and able to be enabled and disabled
a timer for checking sensors that can run all time
a timer for checking sensors quickly that can run all time
a timer for pushing data to Blynk at normal rate that can run all time
a timer for pushing data to Blynk quickly in a special mode

As well as some other that are critical and where I didn’nt decide by now if I will use Timer Lib or the internal Microprocessor Timer.

I would at least need 2 timer instances. One for normal and one for the special mode.

Also the Warn/Error timers won’t be used only in for example the error routine so It would be neccessary to enable and disable them. Sure I could write “around that issue” and for example embedd them in IF statements and check another variable set for this but that would mean many unneccessary program code that would mess up the routines and complicate the program unneccissarily.

Why should I not set up timers to enable and disable them (which is the more clean and easy way) when the Lib is made for that.

Could please any Blynk official look into that issue and check why this is behaving that way?

The timer can work that way. You can disable/enable the timers. You only need one instance. I use that myself too.

Following from my example:

Timer.enable(Timer_A_ID);
Timer.disable(Timer_B_ID);

This way you only need one Timer instance running. It saves on memory of which you have precious little on your board :slight_smile:

All the instances will work independently! You can set different intervals, run them multiple times etc. with only one instance of Timer.run();.

You misunderstood me.
I need one to run while the other is disabled!

The way you suppose to do it all timers attached to that instance would be stopped or am I wrong?

Will check that but anyway I think it should be checked why This issue is happening.

You are wrong. You can individually enable/disable timer instances within one running Timer.run().

Ok, So then I really might do that this way, testing it instantly :wink:
Did’nt know that. I did misunderstood that then. Thanks!

But anyway once Blynk isn’t involved it works so should be checked.

True that, but this way is easier on the memory too. I’ll try and reproduce it later :slight_smile:

Not only that it does make my code even more clean :wink: Thanks!

Yr welcome. It’s always good to keep a couple eyes out and open and let someone else look at it. I use the timers and subroutines to do exactly what you want, stop and start timers based on different events.

For example, in the daytime I check for the light level every 10s and when I detect it becomes to dark, I reset the interval to 5 minutes because I don’t want my automatic lights to go on and off every 10s seconds :slight_smile:

Ok I changed my code and got exactly the same result…
Also in the beginning the timers are way too fast.

After some half second they seem to work normal…

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

#define WLANname "XXX"
#define WLANpw "XXX"
#define authToken "XXX"

// Timer definieren
SimpleTimer Timer;

// Timer ID's initialisieren
int Timer_A_ID = 0;
int Timer_B_ID = 0;
int Timer_C_ID = 0;

unsigned long TESTmillis1 = 0;
unsigned long TESTmillis2 = 0;
unsigned long TESTmillis3 = 0;

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

  Timer_A_ID = Timer.setInterval(1000L, Test1);
  Timer_B_ID = Timer.setInterval(1000L, Test2);
  Timer_C_ID = Timer.setInterval(1000L, Test3);

  Blynk.begin(authToken, WLANname, WLANpw);
  Serial.print("\nConnecting to WiFi");
  while (Blynk.connect() == false);

  Serial.println(" ");

  TESTmillis1 = millis();
  TESTmillis2 = millis();
  TESTmillis3 = millis();

  Serial.println("START");
}

void loop()
{

  Timer.run();
}

void Test1()
{
  Serial.println("Timer - 1");
  Serial.println((millis() - TESTmillis1));
  TESTmillis1 = millis();
}

void Test2()
{
  Serial.println("Timer - 2");
  Serial.println((millis() - TESTmillis2));
  TESTmillis2 = millis();
}

void Test3()
{
  Serial.println("Timer - 3");
  Serial.println((millis() - TESTmillis3));
  TESTmillis3 = millis();
}

Serial Monitor

Connecting to WiFi 

START
Timer - 1
1000
Timer - 2
1000
Timer - 3
1000
Timer - 1
1000
Timer - 2
1000
Timer - 3
1000
Timer - 1
1000
Timer - 2
1000
Timer - 3
1000

Revised sketch:

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

#define WLANname "xxxx"
#define WLANpw "xxxx"
#define authToken "xxxx"

// Timer definieren
SimpleTimer Timer;

// Timer ID's initialisieren
int Timer_A_ID = 0;
int Timer_B_ID = 0;
int Timer_C_ID = 0;

unsigned long TESTmillis1 = 0;
unsigned long TESTmillis2 = 0;
unsigned long TESTmillis3 = 0;

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



  Blynk.begin(authToken, WLANname, WLANpw);
  Serial.print("\nConnecting to WiFi");
  while (Blynk.connect() == false);

  Serial.println(" ");

  TESTmillis1 = millis();
  TESTmillis2 = millis();
  TESTmillis3 = millis();

  Timer_A_ID = Timer.setInterval(1000L, Test1);
  Timer_B_ID = Timer.setInterval(1000L, Test2);
  Timer_C_ID = Timer.setInterval(1000L, Test3);  

  Serial.println("\nSTART");
}

void loop()
{

  Timer.run();
}

void Test1()
{
  Serial.println("Timer - 1");
  Serial.println((millis() - TESTmillis1));
  TESTmillis1 = millis();
}

void Test2()
{
  Serial.println("Timer - 2");
  Serial.println((millis() - TESTmillis2));
  TESTmillis2 = millis();
}

void Test3()
{
  Serial.println("Timer - 3");
  Serial.println((millis() - TESTmillis3));
  TESTmillis3 = millis();
}

Ok so you put the timer behind Blynk connect. Thanks that will help me for my project if that works here as well!

But then Blynk must mess something up in the Timers while starting.

Think you will find it is WiFi that messes up timers and lots of other things, NOT Blynk.

This works.

But if I loose connection for some reason and it gets messed up again at the next reconnect (will test right now) I can’t ensure that Blynk doesn’t get flooded when using timers.

Think you will find it is WiFi that messes up timers and lots of other things, NOT Blynk.

I’m not that experienced to look into all those Libs to check which one causes the problem.

Will try with WiFi.begin to see if that happens there as well.

Yes it’s doing the same with just WiFi.

But how can I avoid to flooding when WiFi is reconnecting after some disconnect then?

#include <SimpleTimer.h>
#include <ESP8266WiFi.h>

#define WLANname "XXX"
#define WLANpw "XXX"
#define authToken "XXX"

// Timer definieren
SimpleTimer Timer;

// Timer ID's initialisieren
int Timer_A_ID = 0;
int Timer_B_ID = 0;
int Timer_C_ID = 0;

unsigned long TESTmillis1 = 0;
unsigned long TESTmillis2 = 0;
unsigned long TESTmillis3 = 0;

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

  Timer_A_ID = Timer.setInterval(1000L, Test1);
  Timer_B_ID = Timer.setInterval(1000L, Test2);
  Timer_C_ID = Timer.setInterval(1000L, Test3);
 
  WiFi.begin(WLANname, WLANpw);
  while (WiFi.status() != WL_CONNECTED) {yield();}

  TESTmillis1 = millis();
  TESTmillis2 = millis();
  TESTmillis3 = millis();

  Serial.println("\nSTART");
}

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

void Test1()
{
  Serial.println("Timer - 1");
  Serial.println((millis() - TESTmillis1));
  TESTmillis1 = millis();
}

void Test2()
{
  Serial.println("Timer - 2");
  Serial.println((millis() - TESTmillis2));
  TESTmillis2 = millis();
}

void Test3()
{
  Serial.println("Timer - 3");
  Serial.println((millis() - TESTmillis3));
  TESTmillis3 = millis();
}

I already knew that.

If you think about it logically it would be impossible for you to flood Blynk’s server when WiFi is reconnecting.

Why? I got a timer that pushes in 1 second intervall.

Once it connects the interval is set to 0-3ms so it sends some times very quickly as in the sample sketch. The flood error was the reason I found that problem in the first place.

How would you avoid it then?