Blynk 2.0, start timer

The term “the loop” is generally used to refer to the void loop, which executes repeatedly while the device is running. In your case you seem to be referring to a function called sensorDataSend() which is called via a Blynk Timer.

One way to achieve what you want is to pause the timer that calls this function, but a better way is probably to set a global variable as a flag when you start the timeout command, then clear that flag when the timeout completes. Then, modify your sensorDataSend() function to check the status of this flag variable, and don’t set pin 13 HIGH if your flag variable is set.

Pete.

Thank you for your answer.
Okay and how it looks like with a flag? Do you have an example?

Something like this…

bool timer_flag:

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt();
  if (pinValue == 1)
  {
    digitalWrite(LED, HIGH);
    timer_flag = true;
    timer.setTimeout(30000L, []()
    {
      digitalWrite(LED, LOW);
      timer_flag = false;
    });
  }
}

// in sensorDataSend() …

if(timer_flag == false)
{
  if (ktc.readCelsius() > solltemp)
  {                               
    digitalWrite(LED, LOW), led1.off();                                
  }
  else if (ktc.readCelsius() <= solltemp - hyst)
 {                   
    digitalWrite(LED, HIGH), led1.on();                                   
  }
}

Pete.

Thank you very much Pete, it seems to be the right way. but when the LED (13) is turned off by the timer, it immediately comes back on.

You need to add some serial print statements to work-out the program flow.

Pete.

Thank you for the help. Now everything works as it should. You’re great!

Can I set the timer.setTimeout via a Slider in Blynk. Set 5 minutes via a slider as a widget for example.

Yes, you can.
There is an example of how to do this in the “Using variables instead of hard-coded numbers” section of the BlynkTimer document that I linked to earlier.

Pete.

Okay and then I must it write in the sketch like:

BLYNK_WRITE(V5){
  timerZeit = param.asDouble();
}

for example.

int slider; // a global variable

BLYNK_WRITE(V5)
{

slider = param.asInt(); // assigning incoming value from pin V5 to the global variable

}

I don’t see the point in using a double precision variable.
An Integer on an ESP8266 is 32-bits, so more than large enough for anything you’re going to want to do.
Also, bear in mind that timer delays are in milliseconds, so if you want to…

the slider value would need to be multiplied by 60,000 to give the corresponding delay in milliseconds.

Pete.

Posting snippets of code make it very difficult to diagnose what’s happening, as it’s impossible to see which physical pins are being referenced and what is in your void loop.

These digitalWrites look very odd to me…

The correct syntax for a digitalWrite command is:
digitalWrite(pin, value);

I don’t really how your digitalWrites are being interpreted by the C++ compiler.

Pete.

Okay sorry.

int LED = 13;
WidgetLED led1(V3);
digitalWrite(LED, LOW), led1.off();

That is not the correct syntax for digitalWrite?
And what does the syntax look like if i want to light up a led as a widget in blynk for a certain action?

It should be

digitalWrite(LED, LOW);

Only

For WidgetLED in Blynk too?
Because now it works, when I push the Widget V4, the led Widget in Blynk goes on.
digitalWrite(LED, HIGH), led1.on();

Check this example

There are two ways to do this. My preferred method is:

Blynk.virtualWrite(vPin,value);

Note that LED widgets in Blynk have a brightness range of 0-255 so if you want to turn on a widget LED at full brightness then the command is:

Blynk.virtualWrite(vPin,255);

The alternative is to declare a WidgetLED object as you have done…

WidgetLED led1(V3);

and turn it on/off with:

led1.on() and led1.off()

If this is what you were trying to achieve with your code then it should have been:

digitalWrite(LED, LOW);
led1.off();

These are two separate commands, which need to be separated by a semicolon ; not a comma , as you did.

Pete.

1 Like

That is the error, when I push the widget button as a switch (V4):

Guru Meditation Error: Core 1 panic’ed (IntegerDivideByZero). Exception was unhandled.
Core 1 register dump:
PC : 0x400d57ea PS : 0x00060530 A0 : 0x800d4c76 A1 : 0x3ffb1f70
A2 : 0x3ffc1a5c A3 : 0x00000000 A4 : 0x00017d52 A5 : 0x3ffb1e90
A6 : 0x00000000 A7 : 0x3ffb0060 A8 : 0x3ffc1a80 A9 : 0x00000000
A10 : 0x00017d52 A11 : 0x00000010 A12 : 0x00000001 A13 : 0x00000000
A14 : 0x00000002 A15 : 0x00000001 SAR : 0x0000000a EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x400d57da LEND : 0x400d5817 LCOUNT : 0x0000000e

ELF file SHA256: 0000000000000000

Backtrace: 0x400d57ea:0x3ffb1f70 0x400d4c73:0x3ffb1f90 0x400e051c:0x3ffb1fb0 0x4008a2f2:0x3ffb1fd0

Rebooting…

Do you have an idea what the problem is?

#define BLYNK_TEMPLATE_ID "TMPLvmzlelxk"
#define BLYNK_DEVICE_NAME "Autoklav3"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial

#include <max6675.h>
#include <Wire.h>
#include <U8x8lib.h>
#include <U8g2lib.h>
#include "BlynkEdgent.h"                                            //Bibliothek für die Verbindung mit der Blynk App über WLAN

U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);   // Display 128x64 ausgewählt in Bibliothek

int ktcSO = 19;                                                     // ktcSO deklariert und auf PIN 19 initialisiert
int ktcCS = 5;                                                      // ktcCS deklariert und auf PIN 5 initialisiert
int ktcCLK = 18;                                                    // ktcCLK deklariert und auf PIN 18 initialisiert

int LED = 13;                                                       // LED deklariert und PIN 13 initialisiert
double solltemp = 121;                                              // solltemp deklariert und mit 26 initialisiert
double hyst = 2;                                                    // Hystherese deklariert
int temp = 0;
int timerZeit;


bool timer_flag;                                                    // Flag Variable als boolschen Wert um Schleife zu kontrollieren

BlynkTimer timer;
BlynkTimer timer2;
WidgetLED led1(V3);
WidgetLED ledTimer(V6);




MAX6675 ktc(ktcCLK, ktcCS, ktcSO);                                  // Initialisierung Sensor MAX6675 für Temperatur Sensor Typ K


BLYNK_WRITE(V1)                                                     // Schreibt virtuellen PIN 3 in Blynk
{
  solltemp = param.asDouble();                                      // Sollwert = Parameter als Double (für Slider)
}

BLYNK_WRITE(V2)
{
  hyst = param.asDouble();                                          // Hystherese = Parameter als Double (für Slider)
}

BLYNK_WRITE(V5) {
  timerZeit = param.asInt() * 60000;                                // Timer, einstellbar über Slider in Blynk 60.000 Millisekunden = 1 Minute 
}


BLYNK_WRITE(V4)
{
  int pinValue = param.asInt();
  if (pinValue == 1) {
    Serial.print("\nTaste in Blynk wurde gedrückt");
    //ledTimer.on();
    timer_flag = true;
    timer.setTimeout(timerZeit, []()
    {
      Serial.print("\nTimer läuft");
      digitalWrite(LED, LOW);
      led1.off();
      //ledTimer.off();
      Serial.print("\nTimer ist abgelaufen - ausschalten");
      timer_flag = true;
    });
  }
  else{
    digitalWrite(LED, HIGH);
    led1.on();
    //ledTimer.off();
  }
}

void setup()
{
  Serial.begin(9600);                                               // Serielle Schnittstelle, serieller Monitor, Baudrate 9600
  Serial.println("\nMax6675 test");
  pinMode(LED, OUTPUT);                                             // deklariert LED als Output
  timer.setInterval(1000L, sensorDataSend);                         // Timer jede Sekunde werden die Daten gelesen
  timer2.setInterval(1000L, sensorDataWrite);
  

  u8x8.begin();
  //Der Bildschirm hat ein Raster aus Bildpunkten
  //Pixel oder Bildpunkt: Ein Kästchen des Rasters

  u8x8.setFont(u8x8_font_7x14_1x2_f);
  // u8x8 Library mit der man sehr einfach Texte darstellen kann, Schriftgröße nur 8x8 Pixel, kaum Schriftarten
  // => wenig Ressourcen
  // bei u8x8 arbeitet man mit Zeilen und Spalten
  // bei u8g2 arbeitet man mit Pixeln

  u8x8.drawString(0, 1, "   Autoklav 3  ");                          // 0. Zeichen von links in der 1. Spalte von oben wird geschrieben
  u8x8.drawString(0, 3, "Temp:");
  u8x8.drawString(0, 5, "Soll:");
  u8x8.drawString(11, 3, " °C");                                     // 11. Zeichen von links in der 3. Spalte von oben wird geschrieben
  u8x8.drawString(12, 5, "°C");
  u8x8.drawString(12, 5, " ");
  u8x8.setCursor(7, 1);

  BlynkEdgent.begin();                                               // Kommunikation mit der Blynk 2.0 App
}




void sensorDataSend() {                                              // Timer: jede Sekunde wird der Sensor ausglesen und ausgegeben
                                                                     // hält den void-loop sauber damit die Blynk Cloud nicht mit Daten überlastet wird

  temp = ktc.readCelsius();
  Blynk.virtualWrite(V0, temp);                                      //Schreibt die gemessene Werte auf die virtuellen PINs in der Blynk APP
  Blynk.virtualWrite(V1, solltemp);
  Blynk.virtualWrite(V2, hyst);
  //Blynk.virtualWrite(V5, timerZeit);


  if (timer_flag == false) {                                        // flag als variable
    Serial.print("\nflag ist aus");
    if (ktc.readCelsius() > solltemp)
    {
      digitalWrite(LED, LOW);
      led1.off();
      Serial.print("\nTemperatur ist höher als Soll - ausschalten");
    }
    else if (ktc.readCelsius() <= solltemp - hyst)                  // wenn gemessene Temperatur <= Solltemperatur - Hystherese über Slider in Blynk
    {
      digitalWrite(LED, HIGH);                           // dann LED bzw. Relais an
      led1.on();
      Serial.print("\nTemperatur ist niedriger als Soll - anschalten");
    }
  }
}
void sensorDataWrite() {                                            // Timer: Gibt ausgelesene Werte auf das Display zurück

  Serial.print("\nTemperatur = ");                                  // Ausgabe im seriellen Monitor
  Serial.print(ktc.readCelsius());
  Serial.println(" °C");

  u8x8.setCursor(8, 3);                                             // Stellt den Cursor zum schreiben an das 9. Zeichen von links und die 3. Spalte von oben
  if (ktc.readCelsius() < 100) u8x8.drawString(12, 3, " ");         // schreibt eine Leerzeile an angegebene Stelle, wenn der Wert unter 100 ist und die erste Ziffer zu löschen
  u8x8.print(ktc.readCelsius(), 1);                                 // schreibt an der oben genannten Stelle den Messwert (eine Nachkommastelle: ,1)
  u8x8.setCursor(8, 5);
  if (solltemp < 100) u8x8.drawString(12, 5, " ");
  u8x8.print(solltemp, 1);


}

void loop() {

  BlynkEdgent.run();                                                // Handshake mit dem Blynk Server
  timer.run();                                                      // startet den Timer (sensorDataSend)
  timer2.run();                                                     // startet den Timer (sensorDataWrite)

}

Post you full sketch!

Pete.

a single BlynkTimer can schedule many timers( up to 16 timers ), so most probably you need only one instance of BlynkTimer in your sketch.

Ok thank you but it´s not the problem, right?