Time Input and Temperature Sensor

Hello guys;

My program controls the temperature depending on the time input and write to HIGH or LOW output. Everything is working but if statement at the end of code -in main loop- not working.
I checked that; V2=1 and roomTemparature = 30 but V5 and D12 are not HIGH.
Please help me.
Thank you !
@Costas


#include <SimpleTimer.h>           // Allows us to call functions without putting them in loop()

#define BLYNK_PRINT Serial         // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h> 
#include <WidgetRTC.h>
#include <OneWire.h>
#include <DallasTemperature.h> 
#define ONE_WIRE_BUS 14          // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WidgetRTC rtc;

char auth[] = "f6e1f833801a403e95d07842769e7a74";
char ssid[] = "GGKD";
char pass[] = "Kaan.Dila.26";
char Date[16];
char Time[16];
SimpleTimer timer;


float roomTemperature;    
long startinput;           
long stopinput;            
long nowseconds;  


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
    rtc.begin();
    timer.setInterval(30000L, activetoday);     // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk); 
    timer.setInterval(3000L, sendTemps);    // Temperature sensor read interval. 3000 (ms) = 3 seconds.

    pinMode(12,OUTPUT);
    digitalWrite(12,LOW);
}

void activetoday(){        // check if schedule should run today   
     if(year() != 1970){
        Blynk.syncVirtual(V11); // sync timeinput widget
        sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     
        sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second()); 
     }
}
void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V1, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
 
}


BLYNK_WRITE(V11) {  

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    int dayadjustment = -1;
    
    if(weekday() == 1){
        dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startinput = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startinput){
                   
                   if(nowseconds <= startinput + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            //digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2, 1);
                                                          
                            }
                    }
          

          else{
              Serial.println("Relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopinput = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopinput){                
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopinput + 90){   
                          // code here to switch the relay OFF
                          //digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2, 0);
                          Blynk.virtualWrite(V5,0);                        
                          digitalWrite(12,LOW);
                   }
          }
          else{
                  if(nowseconds >= startinput){  // only show if motor has already started today
                              Serial.println("Relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
}
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();
   
   
     
}




void reconnectBlynk() {
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}



void loop()
{
  Blynk.run();
  timer.run();
  if((V2==1) && (roomTemperature >= 26)){
  Blynk.virtualWrite(V5, 1); digitalWrite(12,HIGH);}
  else{Blynk.virtualWrite(V5, 0); digitalWrite(12,LOW);}
   
}

Just the first 2 lines of code in loop(), NEVER use virtualWrite in loop().

1 Like

Read this:


and pay particular attention to the bit that says:

Avoiding the void
:point_up: :fire: :warning: VERY IMPORTANT: You can’t send sensor data in your void loop()

– Why?
– Because Blynk sends data over the Internet, and when you put something into void loop(), your microcontroller will execute it :scream_cat: gazillion number of times. Which means that Blynk Cloud will be flooded with gazillion messages from your hardware.

And when Blynk Cloud notices that, it automatically :scissors:︎ cuts your connection. Sorry…

– OK, what should I do then?
– Send sensor data in intervals!

Your void loop() contains an IF/ELSE statement that will always send data to the Blynk server.

Pete.

I tried it this way but it still did not work.

#include            // Allows us to call functions without putting them in loop()

#define BLYNK_PRINT Serial         // Comment this out to disable prints and save space
#include  
#include 
#include 
#include  
#define ONE_WIRE_BUS 14          // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WidgetRTC rtc;

char auth[] = "f6e1f833801a403e95d07842769e7a74";
char ssid[] = "GGKD";
char pass[] = "Kaan.Dila.26";
char Date[16];
char Time[16];
SimpleTimer timer;


float roomTemperature;    
long startinput;           
long stopinput;            
long nowseconds;  


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
    rtc.begin();
    timer.setInterval(30000L, activetoday);     // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk); 
    timer.setInterval(3000L, sendTemps);    // Temperature sensor read interval. 3000 (ms) = 3 seconds.
    timer.setInterval(10000L, decision);
    pinMode(12,OUTPUT);
    digitalWrite(12,LOW);
}

void activetoday(){        // check if schedule should run today   
     if(year() != 1970){
        Blynk.syncVirtual(V11); // sync timeinput widget
        sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     
        sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second()); 
     }
}
void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V1, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
 
}


BLYNK_WRITE(V11) {  

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    int dayadjustment = -1;
    
    if(weekday() == 1){
        dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startinput = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startinput){
                   
                   if(nowseconds <= startinput + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            //digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2, 1);
                                                          
                            }
                    }
          

          else{
              Serial.println("Relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopinput = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopinput){                
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopinput + 90){   
                          // code here to switch the relay OFF
                          //digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2, 0);
                          Blynk.virtualWrite(V5,0);                        
                          digitalWrite(12,LOW);
                   }
          }
          else{
                  if(nowseconds >= startinput){  // only show if motor has already started today
                              Serial.println("Relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
}
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();
   
   
     
}
void decision (){
   if((V2==1) && (roomTemperature >= 26)){
  Blynk.virtualWrite(V5, 1); digitalWrite(12,HIGH);}
  else{Blynk.virtualWrite(V5, 0); digitalWrite(12,LOW);}
 }



void reconnectBlynk() {
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}



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

Is V2 a button?

If you are checking 3 criteria (button, time and temperature) you need to put the if statement within V11.

V2 is not a button. V2 will be HIGH when time input start time is equal to now time.
I put the if statement in V11 but not working.

#include            // Allows us to call functions without putting them in loop()

#define BLYNK_PRINT Serial         // Comment this out to disable prints and save space
#include  
#include 
#include 
#include  
#define ONE_WIRE_BUS 14          // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WidgetRTC rtc;

char auth[] = "f6e1f833801a403e95d07842769e7a74";
char ssid[] = "GGKD";
char pass[] = "Kaan.Dila.26";
char Date[16];
char Time[16];
SimpleTimer timer;


float roomTemperature;    
long startinput;           
long stopinput;            
long nowseconds;  


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
    rtc.begin();
    timer.setInterval(30000L, activetoday);     // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk); 
    timer.setInterval(3000L, sendTemps);    // Temperature sensor read interval. 3000 (ms) = 3 seconds.
    pinMode(12,OUTPUT);
    digitalWrite(12,LOW);
}

void activetoday(){        // check if schedule should run today   
     if(year() != 1970){
        Blynk.syncVirtual(V11); // sync timeinput widget
        sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     
        sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second()); 
     }
}
void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V1, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
 
}


BLYNK_WRITE(V11) {  

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    int dayadjustment = -1;
    
    if(weekday() == 1){
        dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startinput = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startinput){
                   
                   if(nowseconds <= startinput + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            //digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2, 1);
                                                          
                            }
                    }
          

          else{
              Serial.println("Relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopinput = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopinput){                
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopinput + 90){   
                          // code here to switch the relay OFF
                          //digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2, 0);
                          Blynk.virtualWrite(V5,0);                        
                          digitalWrite(12,LOW);
                   }
          }
          else{
                  if(nowseconds >= startinput){  // only show if motor has already started today
                              Serial.println("Relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
}
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();
   
   if((V2==1) && (roomTemperature >= 26)){
  Blynk.virtualWrite(V5, 1); digitalWrite(12,HIGH);}
  else{Blynk.virtualWrite(V5, 0); digitalWrite(12,LOW);}
     
}



void reconnectBlynk() {
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}



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

Remove the if else statement, you can’t just put it in any random location.

The Time Input code you are working with will trigger on and off at your chosen times.

Not before and not after.

Is that part of the code working for you?

Then explain what you want to do regarding temperature within the parameters of what you know about Time Input.

I want do this ;

when my time input is equal to now time and temperature is higher than 30 degrees, then relay on.

@Kaaneses is it working without your temperature requirement?

Yes it is working without temperature.

Good.

I could give you the 1 line of code to do this but presumably you also want to turn the relay off when temperature falls below a certain temperature.

What I really need to know is how you want time and temperature to work together.

I want my room to stay in the temperature I want at the time I want.
For example, I would like my room to be 25 degrees in the evening, but I want it to be 30 degrees in the morning.

Ok I see your problem.

Don’t use the virtualWrite() for V2, use a global variable.

unsigned int ontime = 0;

In TimeInput set ontime=1 at the relevant point and 0 in the off time.
Then you can go back to your original function, but not in loop as:

void decision (){
  if((ontime==1) && (roomTemperature >= 26))
  {
     Blynk.virtualWrite(V5, 1); 
     digitalWrite(12,HIGH);
  }
  else
  {
    Blynk.virtualWrite(V5, 0); 
    digitalWrite(12,LOW);
  }
 }
1 Like

Thank you very much @Costas. It works now.

#include            // Allows us to call functions without putting them in loop()

#define BLYNK_PRINT Serial         // Comment this out to disable prints and save space
#include  
#include 
#include 
#include  
#define ONE_WIRE_BUS 14          // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WidgetRTC rtc;

char auth[] = "f6e1f833801a403e95d07842769e7a74";
char ssid[] = "GGKD";
char pass[] = "Kaan.Dila.26";
char Date[16];
char Time[16];
SimpleTimer timer;

unsigned int ontime = 0;
float roomTemperature;    
long startinput;           
long stopinput;            
long nowseconds;  


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
    rtc.begin();
    timer.setInterval(30000L, activetoday);     // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk); 
    timer.setInterval(3000L, sendTemps);    // Temperature sensor read interval. 3000 (ms) = 3 seconds.
    timer.setInterval(30000L, decision);
    pinMode(12,OUTPUT);
    digitalWrite(12,LOW);
}

void activetoday(){        // check if schedule should run today   
     if(year() != 1970){
        Blynk.syncVirtual(V11); // sync timeinput widget
        sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     
        sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second()); 
     }
}
void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V1, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
 
}


BLYNK_WRITE(V11) {  

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    int dayadjustment = -1;
    
    if(weekday() == 1){
        dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startinput = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startinput){
                      
                   if(nowseconds <= startinput + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            //digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2, 1);
                            ontime=1;
                                                          
                            }
                    }
          

          else{
              Serial.println("Relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopinput = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopinput){                
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopinput + 90){   
                          // code here to switch the relay OFF
                          //digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2, 0);                                               
                          ontime=0;
                   }
          }
          else{
                  if(nowseconds >= startinput){  // only show if motor has already started today
                              Serial.println("Relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
}
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();
   

     
}
void decision (){
  if((ontime==1) && (roomTemperature >= 27))
  {
      
     digitalWrite(12,HIGH);
  }
  else
  {
     
    digitalWrite(12,LOW);
  }
 }


void reconnectBlynk() {
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}



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

2 Likes