Blynk timer enable / disable

Hello !

I am trying to enable and disable a BlynkTimer when needed. I tried couple ways but did not work.

   timer.disable(timerID);
   timer.enable(timerID);

Is this the way to do it ? I have a function that will call the disable and enable the timer when needed.

Blynktimer timer;
int timerID;

void setup 
{
   // do myFunc every 5 seconds 
   timerID = timer.setInterval(5000L, myFunc);
   
} 

void myFunc() 

Can someone guide me ?

Pete.

I went through the link you have posted before i created the topic. That snipet i have posted above is following the same logic. But that ain’t working. The timer still runs. The disable function should be called once or at regular interval ?

I tried different methods but couldn’t get that to work. Can you show me a snipet that is in service :wink:?

How are you capturing and storing your timer id’s?
Have you created a sacrificial timer to avoid the timer 0 bug?

Post some of the actual code you are using, where you define your variables to hold the timer IDs and declare your timers.

Pete.

I have done the exact same thing that you have written in the blog/topic.
I am on my phone right now, i will post the code when i get to my laptop. And yes i read about the timer 0 bug in your post and created the sacrificial timer but that did not help.

May be i will try ticker lib where its easy to start n stop the tickers easily. If BlynkTimer does not support my needs. I will update once i make changes.

Hello

Things did not turn up well !! Now the whole code crashes !!

int timer_checkdays1;
int timer_checkdays2;
int timer_checkdays3;
int timer_checkdays4;
 
void setup() {
  timer.setTimeout(3600000L, [] () {} );
  timer_checkdays1 = timer.setInterval(10000L, checkdays1);
  timer_checkdays2 = timer.setInterval(10000L, checkdays2);
  timer_checkdays3 = timer.setInterval(10000L, checkdays3);
  timer_checkdays4 = timer.setInterval(10000L, checkdays4);
}

void disable_timer() { // will be called when needed
timer.disable(timer_checkdays1);
}

void enable_timer() { // will be called when needed
timer.enable(timer_checkdays1);
}

AND NOW A NEW PROBLEM
placing the checkPhysicalButton(); inside the loop will work and update the switch status on the app. But calling the same with the timer
timer.setInterval(500, checkPhysicalButton); will not update the status.

void checkPhysicalButton() {
  State1 = digitalRead(PUSH_BUTTON_1);
  if (State1 != oldState1) {
    oldState1 = State1;
    relay1State = !relay1State;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    Blynk.syncVirtual(V6);
  }

  State2 = digitalRead(PUSH_BUTTON_2);
  if (State2 != oldState2) {
    oldState2 = State2;
    relay2State = !relay2State;
    digitalWrite(RELAY_PIN_2, relay2State);
    Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
    Blynk.syncVirtual(V7);
  }

  State3 = digitalRead(PUSH_BUTTON_3);
  if (State3 != oldState3) {
    oldState3 = State3;
    relay3State = !relay3State;
    digitalWrite(RELAY_PIN_3, relay3State);
    Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
    Blynk.syncVirtual(V8);
  }

  State4 = digitalRead(PUSH_BUTTON_4);
  if (State4 != oldState4) {
    oldState4 = State4;
    relay4State = !relay4State;
    digitalWrite(RELAY_PIN_4, relay4State);
    Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
    Blynk.syncVirtual(V9);
  }
}

I am not able to figure out what is causing the issue.!

Post the complete sketch.

Pete.

That is the edgent example. With few functions called by timer. And the code is too long.
And I will have to post all the header files.

If you’re using an early Edgent example then there is already a timer object declared, so you should give your timer object a different name, like mytimer

Pete.

Edgent BlynkEdgent;
BlynkTimer edgentTimer;

void app_loop() {
    edgentTimer.run();
    edgentConsole.run();
}

It is named as edgentTimer on Edgent.h file.

EDIT :

just changed the timer name to mytimer. Did not help.

UPDATE:

I replaced the BlynkTimer.h with TimerEvent.h(adapted the code to work with new library) and now the device is stable. There are no crashes. But checkPhysicalButton() this function needs to be called inside the loop only, or else the the status on the app will not be updated. This is very strange.

That sounds like a bad idea!

What does this mean? Inside your void loop?

If you won’t post your full original code (before you started messing around with TimerEvent.h then it’s not really possible to provide any insight about where you are going wrong.

Pete.

Okay. I will post the non working part of the code that dont require the header files of the edgent.

I will post a code that compiles for you. Please see if that works out for you. I will post the code shortly.

Hello !

The idea behind using that lib is simple way to start or stop the timer timer1.enable or timer1.disable no need to capture the ID of the timer.

void app_loop() {
 ​edgentTimer.run();
 ​edgentConsole.run();
 ​check_activetoday.update();
 ​check_days1.update();
 ​check_PhysicalButton.update();
}

This is now solved by updating the timers.update in the app_loop if placed inside the void loop() the switches or other functions that are called by the timers are unresponsive during the reconnection period. (wifi reconnection and internet)

But this brings me a new problem that is the soft watchdog reset everynow an then. So i have placed ESP.wdtFeed(); inside void loop to prevent watchdog timer resets.

I am not sure this is right way to solve the issue, but for now the program is kind of stable. But not perfect. ets Jan 8 2013,rst cause:4, boot mode:(3,0)
This may be due to the fact that i am powering the module with the FTDI’s power supply. Correct me if i am wrong.

Once you start hacking around with the Blynk libraries then you’re on your own, and I don’t think the forum is the place to start looking for assistance.

The BlynkTimer library works fine without the need for modifications, so you’re doing something wrong in your sketch, but as you haven’t shared your original code (before you started making all these unnecessary changes) then we can’t really help.

Pete.

There is nothing to hide or no rocket science running in my code ! As i need time to reduce the code(only needed part) i was not able to sit for long time as i had my knee surgery.

Anyways i just made the code short. Code does not compile as there are no header files.

#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxx"

#include "ESP8266TimerInterrupt.h"

ESP8266Timer ITimer;
BlynkTimer mytimer;
#define TIMER_INTERVAL_MS           100
#define DEBOUNCE_TIME               25

#define VPIN_BUTTON_1    V6
#define VPIN_BUTTON_2    V7
#define VPIN_BUTTON_3    V8
#define VPIN_BUTTON_4    V9

#define RELAY_PIN_1      02
#define RELAY_PIN_2      15
#define RELAY_PIN_3      13
#define RELAY_PIN_4      16

#define PUSH_BUTTON_1    05
#define PUSH_BUTTON_2    04
#define PUSH_BUTTON_3    14
#define PUSH_BUTTON_4    12

volatile bool oldState1;
volatile bool oldState2;
volatile bool oldState3;
volatile bool oldState4;

volatile bool State1 = 0;
volatile bool State2 = 0;
volatile bool State3 = 0;
volatile bool State4 = 0;

volatile bool relay1State = LOW;
volatile bool relay2State = LOW;
volatile bool relay3State = LOW;
volatile bool relay4State = LOW;


void ICACHE_RAM_ATTR checkPhysicalButtonI() { // THIS IS WORKING PERFECT EVEN WHEN THE DEVICE IS TRYING TO 
                                              //CONNECT TO INTERNET OR EVEN WHEN NO WIFI IS AVAILABLE "NO LAG"
  State1 = digitalRead(PUSH_BUTTON_1);
  if (State1 != oldState1) {
    oldState1 = State1;
    relay1State = !relay1State;
    digitalWrite(RELAY_PIN_1, relay1State);
  }

  State2 = digitalRead(PUSH_BUTTON_2);
  if (State2 != oldState2) {
    oldState2 = State2;
    relay2State = !relay2State;
    digitalWrite(RELAY_PIN_2, relay2State);
  }

  State3 = digitalRead(PUSH_BUTTON_3);
  if (State3 != oldState3) {
    oldState3 = State3;
    relay3State = !relay3State;
    digitalWrite(RELAY_PIN_3, relay3State);
  }

  State4 = digitalRead(PUSH_BUTTON_4);
  if (State4 != oldState4) {
    oldState4 = State4;
    relay4State = !relay4State;
    digitalWrite(RELAY_PIN_4, relay4State);
  }
}

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

  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, 0);
  relay1State = 0;


  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_2, 0);
  relay2State = 0;


  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_3, 0);
  relay3State = 0;


  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_4, 0);
  relay4State = 0;

  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, checkPhysicalButtonI))
    Serial.println("Starting  ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer. Select another freq. or interval");
  delay(1000);

  mytimer.setInterval(500,checkPhysicalButton);

  BlynkEdgent.begin();
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
  relay3State = param.asInt();
  digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
  relay4State = param.asInt();
  digitalWrite(RELAY_PIN_4, relay4State);
}

void loop() {
  BlynkEdgent.run();
  mytimer.run(); // THIS WILL NEVER UPDATE THE SWITCH STATUS ON THE APP SIDE
  //checkPhysicalButton(); THIS UPDATE THE SWITCH STATUS ON THE APP SIDE
}

void checkPhysicalButton() {
  State1 = digitalRead(PUSH_BUTTON_1);
  if (State1 != oldState1) {
    oldState1 = State1;
    relay1State = !relay1State;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    Blynk.syncVirtual(V6);
  }

  State2 = digitalRead(PUSH_BUTTON_2);
  if (State2 != oldState2) {
    oldState2 = State2;
    relay2State = !relay2State;
    digitalWrite(RELAY_PIN_2, relay2State);
    Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
    Blynk.syncVirtual(V7);
  }

  State3 = digitalRead(PUSH_BUTTON_3);
  if (State3 != oldState3) {
    oldState3 = State3;
    relay3State = !relay3State;
    digitalWrite(RELAY_PIN_3, relay3State);
    Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
    Blynk.syncVirtual(V8);
  }

  State4 = digitalRead(PUSH_BUTTON_4);
  if (State4 != oldState4) {
    oldState4 = State4;
    relay4State = !relay4State;
    digitalWrite(RELAY_PIN_4, relay4State);
    Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
    Blynk.syncVirtual(V9);
  }
}

There are no header files needed to compile !! But you get the idea.

Placing the checkPhysicalButton() inside the loop will work just fine but the ESP crashes once in a while. And keeping the void loop clean cannot be achieved !! But calling the same function with a timer will never the update the switch status on the app side.

What are these two lines about?

You’d be better uploading the entire sketch as a .zip file to GitHub, along with some proper documentation about the exact issue you are experiencing with this exact sketch. You also ought to add some serial print statements so that you can see in your serial monitor exactly when your timed function are being called.

I’m away from home at the moment, with a limited amount of time and hardware available, so it will now probably be another 5-6 weeks before I get time to look at this in any detail.

Pete.

1 Like

These are ISR. The switch inputs are registered even during the wifi is reconnecting or trying for internet connectivity.

Basically to eliminate the non responsiveness of the switches. These work great no problem.

The only thing is when the checkphysicalbutton is called via timer. If the same thing is placed inside void loop !! It works fine.

Oops we miss you. Be safe. Take care.