Blynk 2.0, start timer

Check this out my friend
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

okay this is not yet clean as I wrote it in void sensorDataSend()?

#define BLYNK_TEMPLATE_ID "TMPLyYek5f7e"
#define BLYNK_DEVICE_NAME "Autoklav1"
#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;
int ktcCLK = 18;

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;

BlynkTimer timer;
//WidgetLED led1(V3);



MAX6675 ktc(ktcCLK, ktcCS, ktcSO);                                  // 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(V4){                                                    // Switch Widget for Timer
 int pinValue = param.asInt();
 }

void setup()
{
  Serial.begin(9600);                                               // Serielle Schnittstelle, serieller Monitor, Baudrate 9600
  Serial.println("Max6675 test");
  pinMode(LED, OUTPUT);                                             // deklariert LED als Output
  timer.setInterval(1000L, sensorDataSend);                         //timer will run every sec
  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 1  ");                           // 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() {
  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);

  if (ktc.readCelsius() > solltemp) {                                // wenn die gemessene Temperatur < der angegebene Sollwert
    digitalWrite(LED, LOW);                                // dann LED an bzw. PIN an
  }
  else if (ktc.readCelsius() <= solltemp - hyst) {                   // Hystherese, damit das Relais nicht ununterbrochen schaltet
    digitalWrite(LED, HIGH);                                // sonst LED aus bzw. Pin aus
  }
    
    Serial.print("Temperatur = ");                                   // 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
                                                                     // schreibt eine Leerzeile an angegebene Stelle, wenn der Wert unter 100 ist umd die erste Ziffer zu löschen
    if (ktc.readCelsius() < 100) u8x8.drawString(12, 3, " ");
    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();
  timer.run();
}

So?

Your description of the functionality you claim to have implemented…

doesn’t match with the code you’ve posted, and your description of the functionality you are hoping to implement…

would need to be implemented using a timeout timer, but advising you how to implement that is difficult when you have reverted to posting lots of code plus a few random comments.

Pete.

Sorry about that then. So is it too hard to start a timer as a switch (widget) via the blynk app that switches pin 13 off after the time has elapsed?

No, it’s very simple - using a timeout timer as I said before.

See the Timeout Timers section of this guide…

Pete.

thank you very much but i can’t get it to work -.-

Try this

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  int pinValue = param.asInt();
 if (pinValue == 1) {
  digitalWrite(ledPin, HIGH);
  timer.setTimeout(5000L, []() 
  {
  digitalWrite(ledPin, LOW);
 }
}
1 Like

Impossible to provide further assistance on why you can’t get it to work, unless you’re prepared to provide information about what you’ve tried, and what results you were seeing.

Pete.

Thanks for this tip John, you are great! The problem is that LED is always set HIGH again, because the target temperature has not yet been reached.
Yes, Pete, but I didn’t want to upload all the codes again, so I shouldn’t do that.

#define BLYNK_TEMPLATE_ID "TMPLyYek5f7e"
#define BLYNK_DEVICE_NAME "Autoklav1"
#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;
int ktcCLK = 18;

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;

BlynkTimer timer;
//WidgetLED led1(V3);



MAX6675 ktc(ktcCLK, ktcCS, ktcSO);                                  // 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(V3) {
  int relais = param.asInt();
  if (relais == 1) {
    digitalWrite(LED, HIGH);
    timer.setTimeout(30000L, []()
    {
      digitalWrite(LED, LOW);
    });
  }
}

void setup()
{
  Serial.begin(9600);                                               // Serielle Schnittstelle, serieller Monitor, Baudrate 9600
  Serial.println("Max6675 test");
  pinMode(LED, OUTPUT);                                             // deklariert LED als Output
  timer.setInterval(1000L, sensorDataSend);


  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 1  ");                          // 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() {
  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);


  if (ktc.readCelsius() > solltemp) {                                // wenn die gemessene Temperatur < der angegebene Sollwert
    digitalWrite(LED, LOW);                                          // dann LED an bzw. PIN an
  }
  else if (ktc.readCelsius() <= solltemp - hyst) {                   // Hystherese, damit das Relais nicht ununterbrochen schaltet
    digitalWrite(LED, HIGH);                                         // sonst LED aus bzw. Pin aus
  }

  Serial.print("Temperatur = ");                                   // 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
  // schreibt eine Leerzeile an angegebene Stelle, wenn der Wert unter 100 ist umd die erste Ziffer zu löschen
  if (ktc.readCelsius() < 100) u8x8.drawString(12, 3, " ");
  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();
  timer.run();

}

…and is the sketch so clean?

ericsson

1 Like

Is your relay active high or active low?
What is your button settings? Push or button?

the relay is high when pin13(LED) is high. so when the target temperature is reached the relay (pin13) is LOW. I use a button in Blynk.

Dave was asking whether your relay is energised by a HIGH signal (Active HIGH) or by a LOW signal (Active LOW).
Most relays are Active LOW.

My suggestion would be to add some serial print statements to your BLYNK_WRITE(V3) function (assuming that’s the one you’re having problems with).
Print messages that show you when the sketch has reached certain parts of the function, and turn on the time stamp facility in the IDE’s serial monitor to see what is happening.

Also, I notice that you are using one of the Edgent examples, but you’ve removed the board definition lines from the sketch. This means that the default board definition will be used instead. Take a look at Settings.h to ensure that there are no GPIO conflicts with the default board definition and the pins you are using in your sketch.

Pete.

This is a Active HIGH relay.
Okay, thank you pete. So I make some serial prints and try to find out where the problem is.

The problem is that pin13 is always set HIGH in the loop. How can the loop also be interrupted with the timeout command?

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

//loop:

void sensorDataSend() {                                             

  temp = ktc.readCelsius();
  Blynk.virtualWrite(V0, temp);                                      
  Blynk.virtualWrite(V1, solltemp);
  Blynk.virtualWrite(V2, hyst);


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

  Serial.print("Temperatur = ");                                    
  Serial.print(ktc.readCelsius());
  Serial.println(" °C");

  u8x8.setCursor(8, 3);                                           
  if (ktc.readCelsius() < 100) u8x8.drawString(12, 3, " ");
  u8x8.print(ktc.readCelsius(), 1);                              
  u8x8.setCursor(8, 5);
  if (solltemp < 100) u8x8.drawString(12, 5, " ");
  u8x8.print(solltemp, 1);


}

thanks in advance

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.