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

Blynk 2.0, start timer

Hello there,
I would like to start a timer via a switch in the blynk app.
when i press the switch, a defined time should elapse and then a relay should switch off.
When the “solltemp” is reached, the timer starts and switches off after 30 minutes.
Can you help me

#define BLYNK_DEVICE_NAME "MAX6675"
#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;                                                    // hyst (Hystherese) deklariert

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)
}


void setup()
{
  Serial.begin(9600);                                               // Serielle Schnittstelle, serieller Monitor, Baudrate 9600
  Serial.println("Max6675 test");
  pinMode(LED, OUTPUT);                                             // deklariert LED als Output
  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  ");                           // 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 loop()
{
  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");


  delay(1000);




  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(8, 3, " ");      // 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);

  BlynkEdgent.run();

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

}

Hey there.
The easiest way to achieve this is using the automation.

You need to restructure your code so that you don’t have blocking delay(xxxx) commands in your void loop, and so that your Blynk.virtualWrite commands aren’t in the void loop either.

Start by reading this:
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

It appears that your code is written to act as a thermostat with a hysteresis value that is adjustable via a slider. If this is one of your goals then using timers within your sketch, rather than automations, is probably a better solution.

This description…

Doesn’t really explain in sufficient detail the functionality you are looking for, and how it fits in with your existing (badly written) functionality.
Could you elaborate more?
Is this switch a physical switch, or a switch widget?
If the latter, then does this widget already exist, and if so which virtual pin is it attached to?

If you want to understand timers better then you should probably read this…

Pete.

Thank you for your answer.

Yes, I would like to activate a timer via a switch as a widget in blynk.
I press the switch in the blynk app and a timer runs for 60 minutes and then PIN 23 (ESP32) goes off (LOW).
I will read your instructions, thanks for that.
ericsson

You need to use scene automation - Automations - Blynk Documentation

To activate the scene automation you have to go to automation tab, if you have many automation it will be annoying to find, instead you can use a button widget.

Hi Ericsson

I have a pond application, where we need to push at blynk button, to stop the filter pump in an period.
This is used when we feed the fish, and dont forget to start the filter again. :slight_smile:
I use blynk timer loop to count down.

I have only pasted part of the code to give you an ida, you can of course omit some of the code.

void TimerEvent() {
	if (TRunFoodPause > 0) {
		TRunFoodPause = TRunFoodPause - 1;
		Blynk.setProperty(V15, "onLabel", "Pause " + String(TRunFoodPause)+ " Sek");
		Blynk.setProperty(V16, "offLabel", "Stop");
	}

}

void loop() {

//--------- Control Food Break Timeout --------------
if (TRunFoodPause == 0 && FoodPauseTrig == 1) {
	Blynk.setProperty(V16, "offLabel", "Manuel");
	FoodPauseTrig = 0;
	Flow = FlowLast;
	FlowSoftStart = 55;
	Blynk.virtualWrite(V15, 0);
	WaterLevelDam = WaterLevel;
}
}
BLYNK_WRITE(V15) {    //Pause
	FoodPause = param.asInt();
	if (FoodPause == 1) {
	}
	else {
		TRunFoodPause = 0;
	}
	FilterClean = 0;
}

/Torben

@TorNor it’s always better to keep your void loop clean, check this out
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

A BlynkTimer in Timeout mode is what you need, but you do need to clean-up your void loop first.

Pete.

1 Like

Hi John

Yes if you dont know how to handle it, in this case “&& FoodPauseTrig == 1” prevent flooding the blynk server.

/Torben

Keeping the void loop clean isn’t just about avoiding flooding the server.

Pete.

Ok thanks. So I need the PRO Version to create automations. But I still have the PLUS Plan.

@Ericsson

That’s not right, you can use Automation even in the free plan

OK, I’m sorry. I’ll have another look.^^

If you need any help, I’m here. Feel free to ask.

Thanks a lot for that. I can´t find the option in my BlynkApp to configurate a automation like a timer. On the BlynkCloud I have activate the automation for a ON/OFF switch but thats all. How can I set a Switch as a Widget and when I push it a timer like 60min starts and after this time the declared PIN set low.

That’s because you haven’t ticked the relevant check boxes in your datastreams. When you do, the Automation icon will appear in the app.

https://docs.blynk.io/en/blynk.console/templates/datastreams/datastreams-common-settings/automation

Pete.

It’s gonna be something like this

Hi Pete, John
Its might looks better and are easier to comment some calls out, however you need some place for your main code. So can you give a good technical reason for, for keep the void loop free / clean.

A BlynkTimer in Timeout mode is what you need
Agree
This are also a solution, but you need to be care full, and track the timer ID, if you want to manipulate it after it is started.

The best way to handle this is to use a timer to call a function on a regular basis, and simply have Blynk.run and timer.run in the void loop.
The frequency of the timer that handles the code that is currently in the void loop will vary depending on the bigger picture, but it might run every 10 seconds, or every 1 second.

Pete.