Room thermostat: how compare two variables

Dear all, sorry for the banality of my question.

I simply would like to realize a “room thermostat” that is able to close a contact when the temperature is lower than the one I set up.
To do this I used a “step widget” to set up the temperature desired (set_temp) and use a function to take over the ambient temp that is sent every 1 sec to WeMos.

The question is: how can I compare the set_temp with real ambient temp in order to set at level HIGH a PIN?? It seems that “set_temp” is a local variable inside BLYNK_WRITE(V1) and I’m not able to compare this with other variables.
I tried to use IF in void loop() but “set_temp” is not recognized.

Sorry for this very banal question, but can someone help me?

Thanks!!


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetRTC.h>

char auth[] = "0626d4c5d0314XXXXXXXXXXXXXXXX0";
char ssid[] = "XXXXXXX";
char pass[] = "XXXXXXXX";

#define pin_temp A0
#define pinout_d6 D6

BlynkTimer timer;
WidgetRTC rtc;

void setup()
{
  pinMode(pin_temp, INPUT);
  pinMode(pinout_d6, OUTPUT);
  
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  rtc.begin();
  timer.setInterval(1000L, inviaMisura);
  timer.setInterval(10000L, clockDisplay);

}

// recupera tutti i valori dal server nel caso in cui WeMas si spenga o resetti
BLYNK_CONNECTED() {
Blynk.syncAll();
}

BLYNK_WRITE(V1) // Step Widget writes to Virtual Pin V1 to set the temp you want
{
  float set_temp = param.asFloat();
  Blynk.virtualWrite(2, set_temp);
}
 
// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute(); // + ":" + second();
  String currentDate = String(day()) + " " + month(); // + " " + year();
  Blynk.virtualWrite(V50, currentTime);
  Blynk.virtualWrite(V51, currentDate);
}

void inviaMisura()  
{  

 /*misuro la temperatura in ingresso =================*/
  float t = 0.00;
  for (byte i = 0; i < 5; i++) { //Esegue l'istruzione successiva 5 volte
      t += (analogRead(pin_temp));    
  }
  t /= 5; //Calcola la media dei  valori di temperatura
  /*===================================================*/

  //Serial.println(t);
  float t_reale = t * ((3.3/1023)*100);
  Blynk.virtualWrite(3, t_reale);              // invia al pin virtuale Blynk 3 il valore della temperatura  

  }

void loop()
{
  Blynk.run();
  timer.run(); // esegue timer 

  if(t_reale < set_temp) {    
  digitalWrite(pinout_d6, HIGH);
  }
  else {    
  digitalWrite(pinout_d6, LOW);

  }

}

You are setting this as a local variable, define it as a global.

Also keep loop() to just 2 lines of code like PUSH_DATA example until you gain experience with Blynk.

1 Like

Thanks Costas, I got it. Now it’s ok

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetRTC.h>

char auth[] = "XXXXXXXXXXXXXXXXXXX";
char ssid[] = "XXXXXX";
char pass[] = "XXXXXXXX";

#define pin_temp A0
#define pinout_d6 D6
float set_temp;
float t_reale;

BlynkTimer timer;
WidgetRTC rtc;

void setup()
{
  pinMode(pin_temp, INPUT);
  pinMode(pinout_d6, OUTPUT);
  
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  rtc.begin();
  timer.setInterval(1000L, inviaMisura);
  timer.setInterval(10000L, clockDisplay);

}

// recupera tutti i valori dal server nel caso in cui WeMas si spenga o resetti
BLYNK_CONNECTED() {
Blynk.syncAll();
}

BLYNK_WRITE(V1) // Step Widget writes to Virtual Pin V1 to set the temp you want
{
  set_temp = param.asFloat();
  Blynk.virtualWrite(2, set_temp);
}
 
// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute(); // + ":" + second();
  String currentDate = String(day()) + " " + month(); // + " " + year();
  Blynk.virtualWrite(V50, currentTime);
  Blynk.virtualWrite(V51, currentDate);
}

void inviaMisura()  
{  

 /*misuro la temperatura in ingresso =================*/
  float t = 0.00;
  for (byte i = 0; i < 5; i++) { //Esegue l'istruzione successiva 5 volte
      t += (analogRead(pin_temp));    
  }
  t /= 5; //Calcola la media dei  valori di temperatura
  /*===================================================*/

  //Serial.println(t);
  t_reale = t * ((3.3/1023)*100);
  Blynk.virtualWrite(3, t_reale);              // invia al pin virtuale Blynk 3 il valore della temperatura  

  }

void loop()
{
  Blynk.run();
  timer.run(); // esegue timer 

  if(t_reale < set_temp) {    
  digitalWrite(pinout_d6, HIGH);
  }
  else {    
  digitalWrite(pinout_d6, LOW);

  }

}
1 Like