How to create a time swich function?

Hey guys, this is my first post here :slight_smile:

First of all I hope, I am right here. I am using an ESP32 working via BLYNK Cloud.

I cant use “delay();” because it pauses the whole script and then i get disconnected from the blynk servers. The programm reads temperature, humidity, co2, etc. and swiches on devices for regualting the humidity, temperature, etc. now i want to attach some ventialors, that should go on for 10 seconds, then off for 30 min and on again for 10 seconds, off for 30min, etc…The code is running fine so far, but i want to swich a device on for a certain time, then off and then wait a certain time before start again.

I tried it already in a few scenarios… but it downs’t work… i pin 27 is attaced to a relays, works fine, but not with a delay or timer command. airflowduration is the time the ventilator should be on and airflowdelay should be for the time when the ventilator is off. Those two variables are set in the blynk web dashboard.

void Airflow()
{
  digitalWrite(27, HIGH);
         timer.setTimeout(1000*airflowduration, []() { 
            digitalWrite(27, LOW);
          timer.setTimeout(1000*airflowdelay, []() {  //
              });
          });
}

Idk if this is helpful, but I am using Blynk Liberary version: 1.1.0
This is the whole code:


#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
#include "BlynkEdgent.h"
#include <Wire.h>
#include "Adafruit_CCS811.h"
#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;
Adafruit_CCS811 ccs;
BlynkTimer timer;


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     INT     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;



/////////////////////////////////////////////////////////////////////remove LED
BLYNK_WRITE(V100)
{
  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
}
/////////////////////////////////////////////////     HUMIDITY CONTROL   [PIN14]  ///////////////////////////////////////////
BLYNK_WRITE(V4)
{
  int humiditycontrol = param.asInt();
  Serial.print("humiditycontrol: ");
  Serial.print(humiditycontrol);
  Serial.println("%");
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

/////////////////////////////////////////////////     TEMPERATURE CONTROL    [PIN15]  ////////////////////////////////////////
BLYNK_WRITE(V5)
 {
  int temperaturecontrol = param.asInt();
  Serial.print("temperaturecontrol: ");
  Serial.print(temperaturecontrol);
  Serial.println("C");
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

////////////////////////////////////////////////     FAN_IN CONTROL    [PIN27]  ////////////////////////////////////////
BLYNK_WRITE(V7)
{
  int airflowdelay = param.asInt();
  Serial.print("airflowdelay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
}
////////////////////////////////////////////////     FAN_IN CONTROL    [PIN27]  ////////////////////////////////////////
BLYNK_WRITE(V6)
{     
  int airflowduration = param.asInt();
  Serial.print("airflowduration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////     VOID SETUP    //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void setup()

{
  //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end

/////////////////////////////////////////////////////////////////////new




  pinMode(12, OUTPUT);
  Serial.begin(115200);
  delay(100);
  pinMode(14, OUTPUT);
  pinMode(27, OUTPUT);

/////////////////////////////////////////////////////////////////////new


  BlynkEdgent.begin();
  timer.setInterval(2000L, CCS811);
  timer.setInterval(2000L, sensorDataRecive);
  timer.setInterval(2000L, sensorDataSend);

  
}
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA RECIVE     //////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Airflow()
{
  digitalWrite(27, HIGH);
         timer.setTimeout(1000*airflowduration, []() { 
            digitalWrite(27, LOW);
          timer.setTimeout(1000*airflowdelay, []() {  //
              });
          });
}

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA SEND     ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void sensorDataSend()
{
//new start
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
//new end
}
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID CCS811     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CCS811()
{
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("CO2: ");
      Serial.println(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  Serial.print("T=");
	Serial.print(hdc1080.readTemperature());
	Serial.print("C, RH=");
	Serial.print(hdc1080.readHumidity());
	Serial.println("%");
} 

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID LOOP     ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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

I would be so happy if someone could help me out here! :slight_smile:

I think my approach would be to have a setInterval timer (declared in void setup) which runs every 30 minutes (1800000ms) and calls a turn_fan_on function.

That function would activate your fan relay, then start a timeout timer that runs for 10 seconds (10000ms) then de-activates the fan relay when the timeout timer expires.

Pete.

Thank you for your response @PeteKnight!
is it right if i do it like this?
i want to be able to adjust the duration and the delay.

I just loaded the new script on the esp32 and it keeps rebooting. The fan tunred on but now its stuck in a boot loop i guess.

void turn_fan_on()
{
  digitalWrite(27, HIGH);
         timer.setTimeout(1000*airflowduration, []() { 
            digitalWrite(27, LOW);
          });
}


void setup()
{
  turn_fan_on();
  timer.setTimeout(1000*airflowdelay, []() {  //
              });

Maybe, can’t really tell based just on the code snippets.

I prefer baby steps to prove the concept, then working up from there.

The serial monitor is usually a good pointer as to why this is happening.

Pete.

Thank you for your time responding :slight_smile:
the suggestion to prove the concept first, was great!
I now have it 2 seconds on and 4 seconds off.
As it worked fine for 3 rounds, i came a bit closer to the esp and hit my desk xD
than it stopped working. I first try to find the issue in the program but than noticed i should put that Relays pin back in haha xD. It went out while hitting the desk i guess :sweat_smile:

I try to get the custom timing function running now

Surgeon

@PeteKnight
Unfortuniatly it keeps rebooting again…
This is what my Serial Monitor says:

23:32:09.370 -> [9117] CONNECTING_NET => CONNECTING_CLOUD
23:32:09.370 -> Guru Meditation Error: Core  1 panic'ed (IntegerDivideByZero). Exception was unhandled.
23:32:09.370 -> Core 1 register dump:
23:32:09.370 -> PC      : 0x400d5cba  PS      : 0x00060330  A0      : 0x800d5a52  A1      : 0x3ffb1f60  
23:32:09.370 -> A2      : 0x3ffc1828  A3      : 0x3ffc18c4  A4      : 0x00000003  A5      : 0x00000001  
23:32:09.402 -> A6      : 0x0000239d  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00001181  
23:32:09.402 -> A10     : 0x0000121c  A11     : 0x3ffc18c4  A12     : 0x00000002  A13     : 0x3ffc7834  
23:32:09.402 -> A14     : 0x00000000  A15     : 0x00000001  SAR     : 0x0000000a  EXCCAUSE: 0x00000006  
23:32:09.402 -> EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
23:32:09.434 -> 
23:32:09.434 -> ELF file SHA256: 0000000000000000
23:32:09.434 -> 
23:32:09.434 -> Backtrace: 0x400d5cba:0x3ffb1f60 0x400d5a4f:0x3ffb1f90 0x400e22d0:0x3ffb1fb0 0x4008a2f2:0x3ffb1fd0
23:32:09.434 -> 
23:32:09.434 -> Rebooting...
23:32:09.434 -> ets Jun  8 2016 00:22:57
23:32:09.434 -> 
23:32:09.434 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
23:32:09.434 -> configsip: 0, SPIWP:0xee
23:32:09.434 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
23:32:09.434 -> mode:DIO, clock div:1
23:32:09.471 -> load:0x3fff0018,len:4
23:32:09.471 -> load:0x3fff001c,len:1044
23:32:09.471 -> load:0x40078000,len:10124
23:32:09.471 -> load:0x40080400,len:5856
23:32:09.471 -> entry 0x400806a8
23:32:09.796 -> �[4438] 

This is the main code:

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
#include "BlynkEdgent.h"
#include <Wire.h>
#include "Adafruit_CCS811.h"
#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;
Adafruit_CCS811 ccs;
BlynkTimer timer;


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     INT     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;



/////////////////////////////////////////////////////////////////////remove LED
BLYNK_WRITE(V100)
{
  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
}
/////////////////////////////////////////////////     HUMIDITY CONTROL   [PIN14]  ///////////////////////////////////////////
BLYNK_WRITE(V4)
{
  int humiditycontrol = param.asInt();
  Serial.print("humiditycontrol: ");
  Serial.print(humiditycontrol);
  Serial.println("%");
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

/////////////////////////////////////////////////     TEMPERATURE CONTROL    [PIN15]  ////////////////////////////////////////
BLYNK_WRITE(V5)
 {
  int temperaturecontrol = param.asInt();
  Serial.print("temperaturecontrol: ");
  Serial.print(temperaturecontrol);
  Serial.println("C");
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

////////////////////////////////////////////////     AIRFLOW_DELAY    [PIN27]  ////////////////////////////////////////
BLYNK_WRITE(V7)
{
  int airflowdelay = param.asInt();
  Serial.print("airflowdelay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  
}
////////////////////////////////////////////////     AIRFLOW_DURATION    [PIN27]  //////////////////////////////////////
BLYNK_WRITE(V6)
{     
  int airflowduration = param.asInt();
  Serial.print("airflowduration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}
void Airflow()
  {
    digitalWrite(27, HIGH);
    timer.setTimeout(airflowduration*1000, []() { //*airflowduration
  digitalWrite(27, LOW);
    });
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////     VOID SETUP    //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
   //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end

  pinMode(12, OUTPUT);
  Serial.begin(115200);
  delay(100);
  pinMode(14, OUTPUT);
  pinMode(27, OUTPUT);

  BlynkEdgent.begin();
  timer.setInterval(2000L, CCS811);
  timer.setInterval(2000L, sensorDataRecive);
  timer.setInterval(2000L, sensorDataSend);  
  timer.setInterval(airflowdelay*1000+airflowduration*1000L, Airflow);
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA RECIVE     //////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA SEND     ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void sensorDataSend()
{
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
}
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID CCS811     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CCS811()
{
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("CO2: ");
      Serial.println(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  Serial.print("T=");
	Serial.print(hdc1080.readTemperature());
	Serial.print("C, RH=");
	Serial.print(hdc1080.readHumidity());
	Serial.println("%");
} 

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID LOOP     ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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

Other question regarding that issue…
can timer.setTimeout(airflowduration*1000, {};
get the number behind the int airflowduration, lets say it s 100 for 100seconds and multiply it by 1000, as it is in my code?
i am new to coding, sry for asking so stupid questions…

  {
    int airflowduration= param.asInt();
    int airflowdelay= param.asInt();
    digitalWrite(27, HIGH);
    timer.setTimeout(airflowduration*1000, []() { //*airflowduration
  digitalWrite(27, LOW);
    });
  }

here my question is, if i need to mention int airflowdelay = param.asInt(); again?


  int airflowdelay= param.asInt();

  timer.setInterval(2000L, CCS811);

  timer.setInterval(2000L, sensorDataRecive);

  timer.setInterval(2000L, sensorDataSend);  

  timer.setInterval(airflowdelay*1000+airflowduration*1000L, Airflow);```

Yes, but you should take care with this…

because I think it needs some brackets to give the results you want…

timer.setInterval((airflowdelay*1000)+(airflowduration*1000L), Airflow);

param.asInt() can only be used in a BLYNK_WRITE(vPin) callback function to obtain the incoming datastream value.

Have you tried putting this into the ESP Exception Decoder?

Pete.

You can try automation. it’s so useful in such cases.
https://docs.blynk.io/en/concepts/automations

@John93 thank you for your reply!
I’ll have a look on the automations, thanks

Surgeon

Hey @PeteKnight!
Thank you for your time helping me!

I tried your code here, it keeps rebooting :confused:

I tried to install it from https://github.com/me-no-dev/EspExceptionDecoder/releases/tag/2.0.2
Unfortunatly the tool won’t show in the tools menu…
I tried it also with the 1.8 version of Arduino IDE instead of the newest one.

Thats the Backtrace from the Serial Output Monitor: (Full code is in the end)

Backtrace: 0x400d5cba:0x3ffb1f60 0x400d5a4f:0x3ffb1f90 0x400e22d0:0x3ffb1fb0 0x4008a2f2:0x3ffb1fd0

I defined the int here on the top:

int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;

Here, I get the correct values printed in the Serial Monitor from airflowdelay and from airflowduration

BLYNK_WRITE(V7)
{
  int airflowdelay = param.asInt();
  Serial.print("airflowdelay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  
}
////////////////////////////////////////////////     AIRFLOW_DURATION    [PIN27]  //////////////////////////////////////
BLYNK_WRITE(V6)
{     
  int airflowduration = param.asInt();
  Serial.print("airflowduration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}

I wonder why the programm crashes when i use airflowdelay and airflowduration in

void Airflow()
  {
    digitalWrite(27, HIGH);
    timer.setTimeout(airflowduration*1000, []() { //*airflowduration
  digitalWrite(27, LOW);
    });
  }
void setup()
{
  
  BlynkEdgent.begin();
  timer.setInterval(2000L, CCS811);
  timer.setInterval(2000L, sensorDataRecive);
  timer.setInterval(2000L, sensorDataSend);  
  timer.setInterval((airflowdelay*1000)+(airflowduration*1000L), Airflow);
}

This is my current full code:

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
#include "BlynkEdgent.h"
#include <Wire.h>
#include "Adafruit_CCS811.h"
#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;
Adafruit_CCS811 ccs;
BlynkTimer timer;


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     INT     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;



/////////////////////////////////////////////////////////////////////remove LED
BLYNK_WRITE(V100)
{
  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
}
/////////////////////////////////////////////////     HUMIDITY CONTROL   [PIN14]  ///////////////////////////////////////////
BLYNK_WRITE(V4)
{
  int humiditycontrol = param.asInt();
  Serial.print("humiditycontrol: ");
  Serial.print(humiditycontrol);
  Serial.println("%");
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

/////////////////////////////////////////////////     TEMPERATURE CONTROL    [PIN15]  ////////////////////////////////////////
BLYNK_WRITE(V5)
 {
  int temperaturecontrol = param.asInt();
  Serial.print("temperaturecontrol: ");
  Serial.print(temperaturecontrol);
  Serial.println("C");
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

////////////////////////////////////////////////     AIRFLOW_DELAY    [PIN27]  ////////////////////////////////////////
BLYNK_WRITE(V7)
{
  int airflowdelay = param.asInt();
  Serial.print("airflowdelay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  
}
////////////////////////////////////////////////     AIRFLOW_DURATION    [PIN27]  //////////////////////////////////////
BLYNK_WRITE(V6)
{     
  int airflowduration = param.asInt();
  Serial.print("airflowduration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}
void Airflow()
  {
    digitalWrite(27, HIGH);
    timer.setTimeout(airflowduration*1000, []() { //*airflowduration
  digitalWrite(27, LOW);
    });
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////     VOID SETUP    //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
   //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end

  pinMode(12, OUTPUT);
  Serial.begin(115200);
  delay(100);
  pinMode(14, OUTPUT);
  pinMode(27, OUTPUT);

  BlynkEdgent.begin();
  timer.setInterval(2000L, CCS811);
  timer.setInterval(2000L, sensorDataRecive);
  timer.setInterval(2000L, sensorDataSend);  
  timer.setInterval((airflowdelay*1000)+(airflowduration*1000L), Airflow);
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA RECIVE     //////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////     VOID SENSOR DATA SEND     ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void sensorDataSend()
{
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
}
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID CCS811     /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CCS811()
{
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("CO2: ");
      Serial.println(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  Serial.print("T=");
	Serial.print(hdc1080.readTemperature());
	Serial.print("C, RH=");
	Serial.print(hdc1080.readHumidity());
	Serial.println("%");
} 

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////     VOID LOOP     ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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

Full serial monitor output:

12:32:33.584 -> Rebooting...
12:32:33.584 -> ets Jun  8 2016 00:22:57
12:32:33.584 -> 
12:32:33.584 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
12:32:33.584 -> configsip: 0, SPIWP:0xee
12:32:33.584 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
12:32:33.584 -> mode:DIO, clock div:1
12:32:33.584 -> load:0x3fff0018,len:4
12:32:33.584 -> load:0x3fff001c,len:1044
12:32:33.584 -> load:0x40078000,len:10124
12:32:33.584 -> load:0x40080400,len:5856
12:32:33.624 -> entry 0x400806a8
12:32:33.967 -> �[4442] 
12:32:38.347 ->     ___  __          __
12:32:38.347 ->    / _ )/ /_ _____  / /__
12:32:38.347 ->   / _  / / // / _ \/  '_/
12:32:38.347 ->  /____/_/\_, /_//_/_/\_\
12:32:38.379 ->         /___/ v1.1.0 on ESP32
12:32:38.379 -> 
12:32:38.379 ->  #StandWithUkraine    https://bit.ly/swua
12:32:38.379 -> 
12:32:38.379 -> 
12:32:38.379 -> [4448] --------------------------
12:32:38.379 -> [4451] Product:  MushcubePrototype
12:32:38.379 -> [4454] Firmware: 0.1.0 (build Dec 17 2022 00:27:58)
12:32:38.379 -> [4459] Token:    fv_R - ••��• - •��•• - ��•••
12:32:38.423 -> [4473] ESP sdk:  v3.3.5-1-g85c43024c
12:32:38.423 -> [4476] Chip rev: 1
12:32:38.423 -> [4478] Free mem: 268596
12:32:38.423 -> [4480] --------------------------
12:32:38.423 -> 
12:32:38.423 -> >[4483] INIT => CONNECTING_NET
12:32:38.423 -> [4486] Connecting to WiFi: cablelink_0289877
12:32:43.110 -> [9201] Using Dynamic IP: 192.168.0.48
12:32:43.110 -> [9201] CONNECTING_NET => CONNECTING_CLOUD
12:32:43.110 -> Guru Meditation Error: Core  1 panic'ed (IntegerDivideByZero). Exception was unhandled.
12:32:43.143 -> Core 1 register dump:
12:32:43.143 -> PC      : 0x400d5cba  PS      : 0x00060530  A0      : 0x800d5a52  A1      : 0x3ffb1f60  
12:32:43.143 -> A2      : 0x3ffc1828  A3      : 0x3ffc18c4  A4      : 0x00000003  A5      : 0x00000001  
12:32:43.143 -> A6      : 0x000023f1  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00001186  
12:32:43.143 -> A10     : 0x0000126b  A11     : 0x3ffc18c4  A12     : 0x00000002  A13     : 0x3ffc7834  
12:32:43.175 -> A14     : 0x00000000  A15     : 0x00000001  SAR     : 0x0000000a  EXCCAUSE: 0x00000006  
12:32:43.175 -> EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
12:32:43.175 -> 
12:32:43.175 -> ELF file SHA256: 0000000000000000
12:32:43.175 -> 
12:32:43.175 -> Backtrace: 0x400d5cba:0x3ffb1f60 0x400d5a4f:0x3ffb1f90 0x400e22d0:0x3ffb1fb0 0x4008a2f2:0x3ffb1fd0
12:32:43.175 -> 
12:32:43.175 -> Rebooting...

Are you getting any compiler warnings (in red) about mixing variable types?

Best to declare both variables as long, and use 1000L in your timer initialisation.

Pete.

Not at all. Everyhing is white

I changed now to: (still rebooting and not doing what it should xD)
(1)

void Airflow()
  {
    digitalWrite(27, HIGH);
    timer.setTimeout(airflowduration*1000L, []() { //*airflowduration
  digitalWrite(27, LOW);
    });
  }

(2)

  timer.setInterval((airflowdelay*1000L)+(airflowduration*1000L), Airflow);

Is it right if i do it like that? (keeps rebooting)

int humidity;
int temp;
int co2;
int TVOC;
long int airflowduration;
long int airflowdelay;

:slight_smile:

Just use long not long int

I’d suggest that you start commenting-out lines of code until you find out exactly which line is causing the issue.

Pete.

You have 3 timers at 2000 ms , it’s not possible to execute them at the same time, and you stress the MCU , try to stage timers and use more than 10000 ms :thinking:
Have in mind that temperature changes every 15 mn , not 2 secondes :yum:

Thank you for your time responding to me :slight_smile:

i set the timers at 10000ms now.

What do you mean with “try to stage taimers”?

The device has an heatmat built in, the temperature has to be very accurate.

If I use long tenseconds = 10; and long fiveseconds = 5 it works fine. The Fans go on for five seconds and go off for ten seconds.

But if I try to use long airflowduration; and long airflowdelay; it stays in a reboot loop.

long airflowduration; and long airflowdelay; are the values i set on my blynk dashboard, so i can adjust the time remotly for keeping the fans on and off.

The code for controlling the time is seperated into two parts:

Part 1: (void Airflow)

void Airflow()        //swiches Fans on and off for a certain time

  {
    digitalWrite(27, HIGH);     //swiches fan1 on
    digitalWrite(12, HIGH);     //swiches fan2 on

    timer.setTimeout(fiveseconds*1000L, []() {    //delay between swichting fans on and off. int "fiveseconds" should be replaced with int "airflowduration"

  digitalWrite(27, LOW);     //swiches fan1 off
  digitalWrite(12, LOW);     //swiches fan1 off
    });
  }

Part 2: (void setup)

{
  serial.begin(115200);
  pinMode(12, OUTPUT);    //Pin 12 is an Output pin
  pinMode(14, OUTPUT);    //Pin 14 is an Output pin
  pinMode(27, OUTPUT);    //Pin 27 is an Output pin
  BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10000L, CCS811);               //runs void CCS811 every 10000ms
  timer.setInterval(10000L, sensorDataRecive);     //runs void sensorDataRecive every 10000ms
  timer.setInterval(10000L, sensorDataSend);       //runs void sensorDataSend every 10000ms
  timer.setInterval((tenseconds*1000L)+(fiveseconds*1000L), Airflow); //delay between swichting fans on and off. int "tenseconds" and int "fiveseconds"should be replaced with int "airflowdelay" and "airflowduration"
}

Full code

/***************************************************************************

  This is a library for the CCS811 air

  This sketch reads the sensor

  Designed specifically to work with the Adafruit CCS811 breakout

  ----> http://www.adafruit.com/products/3566

  These sensors use I2C to communicate. The device's I2C address is 0x5A

  Adafruit invests time and resources providing this open source code,

  please support Adafruit andopen-source hardware by purchasing products

  from Adafruit!

  Written by Dean Miller for Adafruit Industries.

  BSD license, all text above must be included in any redistribution

 ***************************************************************************/

                                                                                                              //set Serial Monitor ro 115200 baud

// Fill-in information from your Blynk Template here

#define BLYNK_TEMPLATE_ID "TMPL6siRfUN2"

#define BLYNK_DEVICE_NAME "MushcubePrototype"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial

//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h

//#define USE_WROVER_BOARD

//#define USE_TTGO_T7

//#define USE_ESP32C3_DEV_MODULE

//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

#include <Wire.h>

#include "Adafruit_CCS811.h"

#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;

Adafruit_CCS811 ccs;

BlynkTimer timer;

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

 /////////////////////////////////////////////////     INT     /////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

int humidity;

int temp;

int co2;

int TVOC;

long airflowduration;

long airflowdelay;

long tenseconds = 10;

long fiveseconds = 5;

/////////////////////////////////////////////////////////////////////remove LED

BLYNK_WRITE(V100)

{

  int pinValue = param.asInt();

  digitalWrite(12,pinValue);

}

/////////////////////////////////////////////////     HUMIDITY CONTROL   [PIN14]  ///////////////////////////////////////////

BLYNK_WRITE(V4)

{

  Serial.println(" ");

 

  Serial.println("Blynk custom settings:");

  Serial.println(" ");

  int humiditycontrol = param.asInt();

  Serial.print("        RH: ");

  Serial.print(humiditycontrol);

  Serial.print("%");

   if(humiditycontrol>humidity)

  {

    digitalWrite(14, HIGH);

  }

  else

    digitalWrite(14, LOW);

}

/////////////////////////////////////////////////     TEMPERATURE CONTROL    [PIN15]  ////////////////////////////////////////

BLYNK_WRITE(V5)

 {

  int temperaturecontrol = param.asInt();

  Serial.print("        Temp: ");

  Serial.print(temperaturecontrol);

  Serial.println("°C");

  //if (temperaturecontrol > temp)

  //{

   // digitalWrite(15, HIGH);

  //}

  //else

    //digitalWrite(15, LOW);

}

////////////////////////////////////////////////     AIRFLOW_DURATION    [PIN27]  //////////////////////////////////////

BLYNK_WRITE(V6)

{    

  int airflowduration = param.asInt();

  Serial.print("        Airflow - duration: ");

  Serial.print(airflowduration);

  Serial.println("sec");

}

////////////////////////////////////////////////     AIRFLOW_DELAY    [PIN27]  ////////////////////////////////////////

BLYNK_WRITE(V7)

{

  int airflowdelay = param.asInt();

  Serial.print("        Airflow - delay: ");

  Serial.print(airflowdelay);

  Serial.println("min");

  Serial.println(" ");

  Serial.println(" ");

  Serial.println(" ");

  Serial.println("--------------------------------------");

  Serial.println(" ");

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Airflow()        //swiches Fans on and off for a certain time

  {

    digitalWrite(27, HIGH);     //swiches fan1 on

    digitalWrite(12, HIGH);     //swiches fan2 on

    timer.setTimeout(fiveseconds*1000L, []() {    //delay between swichting fans on and off. int "fiveseconds" should be replaced with int "airflowduration"

  digitalWrite(27, LOW);     //swiches fan1 off

  digitalWrite(12, LOW);     //swiches fan1 off

    });

  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 //////////////////////////////////////////////////     VOID SETUP    //////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()

{

   //ccs811 start

  Serial.begin(9600);

  Serial.println("CCS811 test");

  if(!ccs.begin())

  {

    Serial.println("Failed to start sensor! Please check your wiring.");

    while(1);

  }

  // Wait for the sensor to be ready

  while(!ccs.available());

  hdc1080.begin(0x40);

//ccs811 end

  Serial.begin(115200);

  pinMode(12, OUTPUT);    //Pin 12 is an Output pin

  pinMode(14, OUTPUT);    //Pin 14 is an Output pin

  pinMode(27, OUTPUT);    //Pin 27 is an Output pin

  BlynkEdgent.begin();    //runs Blynk Connection Liberary

  timer.setInterval(10000L, CCS811);               //runs void CCS811 every 10000ms

  timer.setInterval(10000L, sensorDataRecive);     //runs void sensorDataRecive every 10000ms

  timer.setInterval(10000L, sensorDataSend);       //runs void sensorDataSend every 10000ms

  timer.setInterval((tenseconds*1000L)+(fiveseconds*1000L), Airflow); //airflowdelay*  //airflowduration*

}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 ////////////////////////////////////////////     VOID SENSOR DATA RECIVE     //////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void sensorDataRecive()

{                                            

  Blynk.syncVirtual(V4); //HumidityControl

  Blynk.syncVirtual(V5); //TemperatureControl

  Blynk.syncVirtual(V6); //AirflowDuration

  Blynk.syncVirtual(V7); //AirflowDelay

  Blynk.syncVirtual(V8); //CO2 Control

  humidity=hdc1080.readHumidity()+3;

  temp=hdc1080.readTemperature()-3.6;

  co2=ccs.geteCO2();

  TVOC=ccs.getTVOC();

}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 ////////////////////////////////////////////     VOID SENSOR DATA SEND     ////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void sensorDataSend()

{

  Blynk.virtualWrite(V0, humidity);

  Blynk.virtualWrite(V1, temp);

  Blynk.virtualWrite(V2, co2);

  Blynk.virtualWrite(V3, TVOC);

}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 /////////////////////////////////////////////////     VOID CCS811     /////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CCS811()

{

      Serial.println("Sensor CJMCU 8118 meassurements:");

      Serial.println(" ");

      Serial.print("        RH=");

      Serial.print(hdc1080.readHumidity());

      Serial.print("%        Temp=");

      Serial.print(hdc1080.readTemperature());

      Serial.println("°C");

  if(ccs.available()){

    if(!ccs.readData()){

      Serial.print("        CO2: ");

      Serial.print(ccs.geteCO2());

      Serial.print("ppm     TVOC: ");

      Serial.println(ccs.getTVOC());

    }

    else{

      Serial.println("ERROR!");

      while(1);

    }

  }

}

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 /////////////////////////////////////////////////     VOID LOOP     ///////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()

 {

  BlynkEdgent.run();

  timer.run();

}

Hey @PeteKnight thanks for replying!

i fixed a part of the problem. I still have issues with replacing the integers from blynk cloud with the test inegers… please have a look on my reply to @Blynk_Coeur.

Thank you so much for helping me out here! It means much to me :slight_smile:

1 Like

Read the “Staggering Timers” section of this tutorial…

Pete.

1 Like

I read your desciption and also the post from @Gunner you linked there.
I have created delays between the timers now, so they are staggered now.

If i use this code, it crashes:

BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10000L, CCS811);               //runs void CCS811 every 10000ms
  delay(200);
  timer.setInterval(10000L, sensorDataRecive);     //runs void sensorDataRecive every 10000ms
  delay(200);
  timer.setInterval(10000L, sensorDataSend);       //runs void sensorDataSend every 10000ms
  delay(200);
  timer.setInterval((airflowdelay*1000L)+(airflowduration*1000L), Airflow);

If i use this code it works:

BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10000L, CCS811);               //runs void CCS811 every 10000ms
  delay(200);
  timer.setInterval(10000L, sensorDataRecive);     //runs void sensorDataRecive every 10000ms
  delay(200);
  timer.setInterval(10000L, sensorDataSend);       //runs void sensorDataSend every 10000ms
  delay(200);
  timer.setInterval((tenseconds*1000L)+(fiveseconds*1000L), Airflow);

so the differenceres are only this two long int. It works with those two:
tenseconds, fiveseconds
With the next one it reboots:
airflowduration, airflowduelay

the int are declared as followed:

int humidity;
int temp;
int co2;
int TVOC;
long airflowduration;
long airflowdelay;
long tenseconds = 10;
long fiveseconds = 5;

i have the int airflowduration, airflowduelay printed out in the serial monitor, to check if they are actually right. They are displayed as numbers like 10 or 15, just normal and I did set them in the blynk dashboard

the int get their value from here:

BLYNK_WRITE(V6)

{    
  long airflowduration = param.asInt();
  Serial.print("        Airflow - duration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}
BLYNK_WRITE(V7)
{
  long airflowdelay = param.asInt();
Serial.print("        Airflow - delay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
}

The values get synced every 10000ms via blynk timer.

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

Okay, think about what’s happening here.

airflowduration and airflowdelay are being initialised as global variables with a null (so therefore zero) value…

In void setup BlynkEdgent.begin(); is called, then immediately afterwards you initialise your timers, so you’re actually saying this…

timer.setInterval((0*1000L)+(0*1000L), Airflow);

or in other words…

timer.setInterval(0, Airflow);

The BlynkTimer library has a hard time with the 0ms duration, and that’s creating an illegal function call within that library.

What you should be doing is removing your last timer from void setup and pitting it in a BLYNK_CONNECTED() function, preceded by a couple of Blynk.syncVirtual commands…

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay 
  timer.setInterval((airflowdelay*1000L)+(airflowduration*1000L), Airflow);
}

If there are other datsterams that you want to synchronise at start-up you can also add them into the BLYNK_CONNECTED() callback function.

But, you also need to fix these issues…

BLYNK_WRITE(V6)
{    
  int airflowduration = param.asInt();   <<<<< Re-declaration of airflowduration as local
  Serial.print("        Airflow - duration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}

BLYNK_WRITE(V7)
{
  int airflowdelay = param.asInt();  <<<<< Re-declaration of airflowdelay as local
  Serial.print("        Airflow - delay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
}

having the int at the begining of int airflowduration and int airflowdelay re-declares a local copy of these variables, and the value that comes from the Blynk datasteram will ONLY be visible within the BLYNK_WRITE() function.
Removing the int will fix this, and the same applies to any other places where you are doing this.

I’m not sure what it is that you’re trying to achieve with this 1000ms re-synchronisation, but whatever it is you need to stop doing it.
Each time the datastrream value changes, the corresponding BLYNK_WRITE(vPin) callback function will be triggered. You don’t need to keep forcing this once every second, and it’s not good as far as either your sketch or the Blynk server is concerned.

Pete.