Hey guys, this is my first post here
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!