Timer function in IOT project won't work. Thanks for help 🙏

Hey guys, I have troubles with my project..

This project reads data from a sensor sends it to an ESP32 and displays it on a web dashboard via the cloud iot servie called blynk.
Over the web dashboard I can control humidity, temperature, etc. I am currently trying to implement a timer for swiching on and off a devices (Pin 12 and Pin 27) which can be customised over the web dashboard. I want to modify the duration for the device is running and i want to modify how long the device should stop running.
I tried to solve this with the two int: airflowduration and airflowdelay
The value of those two int can be changed anytimes and gets displayed correctly in the serial monitor. But however the timer does not work. When i replace the two int with normal numbers it works.
row 138: timer.setTimeout(airflowduration, []()
row 195: timer.setInterval((airflowdelay + airflowduration), Airflow);

Thats the 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 humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;

int humiditycontrol;     // Now declared globally
int temperaturecontrol;  // Now declared globally


BLYNK_WRITE(V4)
{
  //Controlls Humidity on Pin14

  humiditycontrol = param.asInt();
  Serial.print("BLYNK_WRITE(V4)triggered - incoming value = ");
  Serial.println(humiditycontrol);
}


BLYNK_WRITE(V5)
 {
   //Controlls Temperature on PinX       !To enanble this funtion, assign new pin and remove //

  temperaturecontrol = param.asInt();
  Serial.print("BLYNK_WRITE(V5)triggered - incoming value = ");
  Serial.println(temperaturecontrol);

  
  // Don't do this here, do it in sensorDataRecive()...
  
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}


BLYNK_WRITE(V6)
{ 
  //Sets value for airflowduration from the cloud and prints it in serial monitor

  airflowduration = param.asInt();  
  Serial.print("BLYNK_WRITE(V6)triggered - incoming value = ");
  Serial.println(airflowduration);
  airflowduration = airflowduration * 1000;
  Serial.print("airflowduration in ms = ");
  Serial.println(airflowduration); 
}


BLYNK_WRITE(V7)
{
  //Sets value for airflowdelay from the cloud and prints it in serial monitor

  airflowdelay = param.asInt();
  Serial.print("BLYNK_WRITE(V7)triggered - incoming value = ");
  Serial.println(airflowdelay);
  airflowdelay = airflowdelay * 1000;
  Serial.print("airflowdelay in ms = ");
  Serial.println(airflowdelay);
}


BLYNK_WRITE(V100)
{
  //Controlls TestLED on Pin12

  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
  Serial.print("BLYNK_WRITE(V100)triggered - incoming value = ");
  Serial.println(pinValue);
}


void Airflow()    //swiches Fans on and off for a certain time
{
  digitalWrite(27, HIGH);     //swiches fan1 on
  digitalWrite(12, HIGH);     //swiches fan2 on
  Serial.println();
  //airflowduration = * 1000;
  Serial.print("In void Airflow - airflowduration in ms = ");
  Serial.print(airflowduration);
  Serial.println(" Timer starting");

  timer.setTimeout(airflowduration, []()
{
  // When the timer completes, any code here will be executed
  digitalWrite(27, LOW);     //swiches fan1 off
  digitalWrite(12, LOW);     //swiches fan1 off
  Serial.println();
  Serial.print("Timer complete - AIRFLOW OFF FOR ");
  Serial.print(airflowdelay);
  Serial.println(" ms");
  });   
}
  

void setup()
{
    Serial.begin(115200);

   //ccs811 start
  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);    //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(20*1000L, SerialMonitor);               //runs void CCS811 every X*1000ms
  //delay(200);
  timer.setInterval(10*1000L, sensorDataRecive);     //runs void sensorDataRecive every X*1000ms
  //delay(200);
  //timer.setInterval(10*1000L, sensorDataSend);       //runs void sensorDataSend every X*1000ms
}


BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //AirflowDuration
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay 
  
  Serial.print("Setting timer to ");
  Serial.print(airflowdelay + airflowduration);
  Serial.println(" ms");

  // note that this timer duration won't change until the device re-starts.
  // probably need to delete the timer and re-create if different functionality is needed...

  timer.setInterval((airflowdelay + airflowduration), Airflow);
}


void sensorDataSend()
{
 // this code moved into sensorDataRecive
}

void SerialMonitor()    // Timer that trtiggers this is disabled
{
//BLYNK_WRITE (V4)
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println("                  Additional Outputs                  ");
  Serial.println("------------------------------------------------------");  
  Serial.println(" ");
  Serial.println("        Blynk user settings:");
  Serial.println(" ");
  Serial.print("        RH: ");
  Serial.print(humiditycontrol);
  Serial.print("%");
//BLYNK_WRITE (V5)
  Serial.print("        Temp: ");
  Serial.print(temperaturecontrol);
  Serial.println("°C");
  Serial.println(" ");
//BLYNK_WRITE (V6)
  Serial.print("        Airflow - duration set by user at: ");
  Serial.print(airflowduration);
  Serial.println("sec");
//BLYNK_WRITE (V7)
  Serial.print("        Airflow - delay set by user at: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  Serial.println(" ");  
//Sensor CJMCU 8118
  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);
    }
  }
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println(" ");
  Serial.println(" ");  
} 


void sensorDataRecive()
{   
  // You can't do this, leave these commented-out...                                         
  //Blynk.syncVirtual(V4); //HumidityControl
  //Blynk.syncVirtual(V5); //TemperatureControl
  //Blynk.syncVirtual(V6); //AirflowDuration
  //Blynk.syncVirtual(V7); //AirflowDelay
  //Blynk.syncVirtual(V8); //CO2 Control
  
  humidity=hdc1080.readHumidity()+4;
  temp=hdc1080.readTemperature()-4;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();

  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);

  if (humiditycontrol > humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
  {
    digitalWrite(14, LOW);
  }
}


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

Thats what the serial monitor says:

19:31:20.810 -> [17002] CONNECTING_NET => CONNECTING_CLOUD
19:31:20.848 -> [17033] Connecting to blynk.cloud:443
19:31:22.963 -> [19154] Certificate OK
19:31:22.997 -> [19186] Ready (ping: 30ms).
19:31:23.148 -> BLYNK_WRITE(V4)triggered - incoming value = 23
19:31:23.224 -> BLYNK_WRITE(V5)triggered - incoming value = 28
19:31:23.295 -> BLYNK_WRITE(V6)triggered - incoming value = 11
19:31:23.295 -> airflowduration in ms = 11000
19:31:23.328 -> Setting timer to 11000 ms
19:31:23.328 -> [19527] CONNECTING_CLOUD => RUNNING
19:31:23.328 -> BLYNK_WRITE(V7)triggered - incoming value = 21
19:31:23.362 -> airflowdelay in ms = 21000
19:31:34.341 -> 
19:31:34.341 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:31:45.311 -> 
19:31:45.311 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:31:45.311 -> 
19:31:45.311 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:31:56.325 -> 
19:31:56.325 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:31:56.325 -> 
19:31:56.325 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:32:07.305 -> 
19:32:07.305 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:32:07.338 -> 
19:32:07.338 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:32:18.432 -> 
19:32:18.432 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:32:18.432 -> 
19:32:18.432 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:32:29.339 -> 
19:32:29.339 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:32:29.424 -> 
19:32:29.424 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:32:40.327 -> 
19:32:40.327 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:32:40.327 -> 
19:32:40.327 -> Timer complete - AIRFLOW OFF FOR 21000 ms
19:32:51.301 -> 
19:32:51.301 -> In void Airflow - airflowduration in ms = 11000 Timer starting
19:32:51.340 -> 
19:32:51.340 -> Timer complete - AIRFLOW OFF FOR 21000 ms

I guess your problem is that setInterval() is adding an interval, you thought it replaces an already existing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.