BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help with Blynk simpletimer

I have used Blynk timer in a simple way and i know how to use timer.setTimout and set.timeInterval etc. But in this project i am a little stuck.

This is an Wemos D1 Mini that controlls an SSR relay that allows me to turn on/off my fan heater in the basement via button in the app etc.

All works fine, but i need to set a timer that turns off the relay (fan heater) automaticly after lets say 4 hours. I have tried timer.disable and timer.reset in several ways and i know i,m doing something wrong here, but kan someone give me an example out of the code posted below on how i can do this in simpliest way please?

Also, when i push the app button to turn on heater, i want the heater to turn off after 4 hours. If i turn off the heater via app and turn it on again i want the timer to start over.

Here is my code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int relayPin = 15;
int onLED = 12;
int buttonState;

char auth[] = "*****";

char ssid[] = "*****";
char pass[] = "*****";

BlynkTimer timer;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  pinMode(onLED, OUTPUT);
  timer.setInterval(1000L, temp);
}

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void emergencyOFF() {
  Blynk.virtualWrite(V1, LOW);
  digitalWrite(relayPin, LOW);
  digitalWrite(onLED, LOW);
  Blynk.virtualWrite(V3, "SJEKK TEMPERATUR!!");
  Blynk.notify("SJEKK TEMPERATUR!");
}

void temp() {
  sensors.requestTemperatures();
  Serial.print("Temperatur: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V2, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));

  if (sensors.getTempCByIndex(0) > 60) {
    emergencyOFF();
  }
  else {
    Blynk.virtualWrite(V3, "  ");
  }
}

BLYNK_WRITE(V1) {
  buttonState = param.asInt();

  if (buttonState == 1) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(onLED, HIGH);
  }
  else {
    digitalWrite(relayPin, LOW);
    digitalWrite(onLED, LOW);
  }
}

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

I’d start by reading the SimpleTimer documentation.

https://playground.arduino.cc/Code/SimpleTimer/

To be able to delete or disable a timer you need to know it’s ID, so when you initiate a timer , instead of this:

timer.setTimeout(4 * 60 * 60, turn_off_SSR)

you’d do this:

SSR_timeout_timer_ID = timer.setTimeout(4 * 60 * 60, turn_off_SSR)

note that SSR_timeout_timer_ID needs to be declared as a global integer variable so that you can use it to disable the timer from anywhere in your sketch.

You can then do things like:

timer.disable(SSR_timeout_timer_ID)

or

timer.delete(SSR_timeout_timer_ID)

Does this help?

EDITED TO ADD…

I forgot to mention this bug…

Pete.

Thank you wery mutch!

I will try that soon. I will post my results here :slight_smile:

I have possible did something wrong, but when i inplementet the code in my scetch, i get an error that says “setTimout was not declared in this scope”

Here is the code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int relayPin = 15;
int onLED = 12;
int buttonState;

int SSR_timeout_timer_ID; 

char auth[] = "***'";

char ssid[] = "****";
char pass[] = "****";

BlynkTimer timer;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  pinMode(onLED, OUTPUT);
  timer.setInterval(1000L, temp);
  SSR_timeout_timer_ID = setTimeout(10000L, turn_off_SSR);
  
}

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void emergencyOFF() {
  Blynk.virtualWrite(V1, LOW);
  digitalWrite(relayPin, LOW);
  digitalWrite(onLED, LOW);
  Blynk.virtualWrite(V3, "SJEKK TEMPERATUR!!");
  Blynk.notify("SJEKK TEMPERATUR!");
}

void temp() {
  sensors.requestTemperatures();
  Serial.print("Temperatur: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V2, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));

  if (sensors.getTempCByIndex(0) > 60) {
    emergencyOFF();
  }
  else {
    Blynk.virtualWrite(V3, "  ");
  }
}

void turn_off_SSR() {
    digitalWrite(relayPin, LOW);
    digitalWrite(onLED, LOW);
}

BLYNK_WRITE(V1) {
  buttonState = param.asInt();

  if (buttonState == 1) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(onLED, HIGH);
    turn_off_SSR();
  }
  else {
    digitalWrite(relayPin, LOW);
    digitalWrite(onLED, LOW);
  }
}

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

Aoiligies, I messed-up whne I copied the example from the documentaion and tried to adapt it into a real-world example.

It should say…

SSR_timeout_timer_ID = timer.setTimeout(4 * 60 * 60, turn_off_SSR)

(I missed-out the timer. part of the expression).

I’ll edit my earlier post so that anyone who stumbles across it in future doesn’t get confused.

Pete.

No worries :slight_smile:

Can you please give me an example for where the code snippets will place in code please? I,m not quite shure…

When i start the relay and turn on the heater, i get the timer to stop after 10 seconds. But when i turn off the heater via app after 5 seconds and turn it on again the timer is not reset. It stays on for 5 seconds and turns off.

I want the timer to reset when i turn of the heater via app and start over again when i turn it on.

Okay, if you explain the logic of how it should work and what widgets are used to turn the heater on an off manually etc. then I’ll take a look later.

Pete.

Okay.

I have an heater connected to the solid state relay that i can turn on/off via button in app. When i turn on the heater with the app button, i want the timer to start. (lets say 10 seconds for now when we test).

BUT… when i turn of the heater in blynk app button BEFORE the time (10 seconds) has gone, i want the timer to reset, and start over when i turn on the heater again via blynk app button.

Which button, what pin is it connected to?

Pete.

Virtual button V1 that controlls relayPin.

Okay, I thi9nk this is working, but you’ll need to test it thoroughly…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int relayPin = 15;
int onLED = 12;
int buttonState;
int SSR_on_duration = 10 * 1000; // 10 seconds - for testing 

int SSR_timeout_timer_ID; 

char auth[] = "REDACTED";

char ssid[] = "REDACTED";
char pass[] = "REDACTED";

BlynkTimer timer;

void setup()
{
  Serial.begin(74880);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  pinMode(onLED, OUTPUT);
  timer.setTimeout(10L, [](){});   // Sacrificial timer to overcome SimpleTimer bug
  timer.setInterval(1000L, temp);
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);  // Changed from syncAll, as only the V1 widget seems to need to be synched
}

void emergencyOFF()
{
  turn_off_SSR();
  Blynk.virtualWrite(V3, "SJEKK TEMPERATUR!!");
  Blynk.notify("SJEKK TEMPERATUR!");
}

void temp() 
{
  sensors.requestTemperatures();
  Serial.print("Temperatur: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V2, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));

  if (sensors.getTempCByIndex(0) > 60) 
  {
    emergencyOFF();
  }
  else 
  {
    Blynk.virtualWrite(V3, "  ");
  }
}

void turn_off_SSR()
{
    timer.disable(SSR_timeout_timer_ID); // stop the timeout timer if it's not yet completed
    digitalWrite(relayPin, LOW);
    Blynk.virtualWrite(V1, LOW);
    digitalWrite(onLED, LOW);
    Serial.println("SSR Off");
}

BLYNK_WRITE(V1) 
{
  buttonState = param.asInt();

  if (buttonState == 1) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(onLED, HIGH);
    Serial.println("SSR On");    
    SSR_timeout_timer_ID = timer.setTimeout(SSR_on_duration, turn_off_SSR);  // Start the timeout timer
  }
  else
  {
    turn_off_SSR();
  }
}

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

Changes I’ve made/things to be aware of…

  • Added a sacrificial timer in slot 0 to overcome the SimpleTimer bug

  • You don’t declare the timeout timers in void setup, only when/where you need them.

  • Added a parameter of SSR_on_duration to define how long the relay stays on for (set to 10 seconds)

  • Added more functionality to the turn_off_SSR function and removed it from elsewhere, to reduce code repetition

  • Changed the baud rate to 74880 as I was testing on a NodeMCU and this is the default baud rate, so it allows me to see boot messages from the board as well as serial output without having to switch baud rates in the serial monitor.

Pete.