ESP 32 : Temperature Monitoring with Blynk and Automatic Temperature Control using 2 Channel Relay Module

As @PeteKnight said , you have to use additional variables like that

int Relay1;
int Relay2;
#define relay1      13   
#define relay2      14   

if (t <= MinTemperature) {
    Relay1=1;
    Relay2=0;
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
  }

You can simplify by using

if (t <= MinTemperature) {
  Relay1 = HIGH;
  Relay2 = LOW;
  digitalWrite(relay1, Relay1);
  digitalWrite(relay2, Relay2);
}

So …

if (Relay1 == HIGH)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (Relay2 == LOW)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  

My relay is Double Throw but its activate (IN LED ON) when low. so my relay is active low ?

Yes, if your relay is a double-throw relay and its IN LED turns on when the input signal is low, then it is an active-low relay.

Correct.j

I’d suggest that you add some serial print statements into your sketch so that you can visualise the values of each of your variables and monitor the program flow.

Pete.

This is my latest update, Any Advice :bowing_man:

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN            "--------------------------------"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

#define DHTPIN 4  
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);


WidgetLCD lcd(V9); //LCD for Blynk

WidgetLED led1(V1);
WidgetLED led2(V2);

float h ;
float t ; // or dht.readTemperature(true) for Fahrenheit

int relay1 ; //Pompa Air
int relay2 ; //Lampu Halogen

#define relay1      14   
#define relay2      13

float MinTemperature = 25.0;
float MaxTemperature = 28.0;

BlynkTimer timer;

char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.

void sendSensor()
{

//relay1 ;
//relay2 ;

h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

    if (isnan(h) || isnan(t)) {
    lcd.clear();
    lcd.print(4, 1, "Gagal membaca data dari sensor");
    return;
    }    
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  if (t <= MinTemperature) {
    
    digitalWrite(relay1, LOW); //Menggunakan Relay Aktif Low
    digitalWrite(relay2, HIGH);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(4, 0, "Suhu Dingin"); 
    led1.on();
  }
  else if (t >= MaxTemperature) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
    lcd.clear(); 
    lcd.print(4, 0, "Suhu Panas"); 
    led2.on();

  }
}
  
void setup()
{
  // Debug console
  Serial.begin(115200);
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  
  dht.begin();  
  
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
    
}

void loop()
{

  timer.run();
    Blynk.run();
}

Can you see the difference between these two pieces of code?

TBH I didn’t really like @Blynk_Coeur’s suggestion of using Relay1 and RELAY2 (first letter in capitals) as variables to track the current status of your relays, because these are too similar to the relay1 and relay2 (first letter in lowercase) aliases that you were already using.

I’d suggest that you use more meaningful variables such as relay1_status or even relay1_on_off_status to track the current status of your relays. That way the variable names are self-explanatory and difficult to get mixed-up with each other.

In addition, you need to update these status tracking variables to reflect the current state of each relay, when you perform a digitalWrite command to that relay, which you aren’t doing in your sketch at the moment, despite @Blynk_Coeur demonstrating how to do this in his code snippets.

Pete.

1 Like

I have change it to

int on ; 
int off ; 

#define RELAY_PIN_1      14   //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2     13 //Pompa Air/Water Pump

and is it better use this for track status of relay ?

  if (RELAY_PIN_1 == on)  { //Tracking Relay 1, Sending to LED Blynk V1 
    led1.on();
    }  
  else if (RELAY_PIN_2 == on) { //Tracking Relay 2, Sending to LED Blynk V2
    led2.on();
    }

Or use

This is the update

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN            "----------------------------"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

#define DHTPIN 4  
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);


WidgetLCD lcd(V9); //LCD for Blynk

WidgetLED led1(V1); //LED for Relay 1
WidgetLED led2(V2); //LED for Relay 2

float h ;
float t ; // or dht.readTemperature(true) for Fahrenheit

int on ; 
int off ; 

#define RELAY_PIN_1      14   //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2     13 //Pompa Air/Water Pump

float MinTemperature = 25.0;
float MaxTemperature = 28.0;

BlynkTimer timer;

char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.

void sendSensor()
{

on = 0;
off = 1 ;

h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

    if (isnan(h) || isnan(t)) {
    lcd.clear();
    lcd.print(4, 1, "Gagal membaca sensor");// Sending to LCD Blynk "Failed to read data from sensor"
    return;
    }    
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  if (t <= MinTemperature) {
    
    digitalWrite(RELAY_PIN_1, on); //Turn on Relay 1 
    digitalWrite(RELAY_PIN_2, off);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(0, 0, "Suhu Dingin"); //Sending " Cold Temperature" To Blynk
    //led1.on(); 
  }
  else if (t >= MaxTemperature) {
    digitalWrite(RELAY_PIN_1, off);
    digitalWrite(RELAY_PIN_2, on);//Turn on Relay 2
    lcd.clear(); 
    lcd.print(0, 0, "Suhu Panas"); //Sending " Hot Temperature" To Blynk
    //led2.on();

  if (RELAY_PIN_1 == on)  { //Tracking Relay 1, Sending to LED Blynk V1 
    led1.on();
    }  
  else if (RELAY_PIN_2 == on) { //Tracking Relay 2, Sending to LED Blynk V2
    led2.on();
    }

  }
}


void setup()
{
  // Debug console
  Serial.begin(115200);
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  
  dht.begin();  
  
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
    
}

void loop()
{

  timer.run();
    Blynk.run();
}

is it use same server betwen Blynk Mobile Dasboard and Web Dashboard ? When i add Temperature data stream to Gauge in Web Dashboard the widget stuck and didnt change.

Change the humidity datastream minimum and maximum values to (0–100) and the temperature datastream minimum and maximum values to (-40–80).

Why would you do that?
Didn’t anything I wrote in my last post make sense to you?

Pete.

My bad, English my 3rd language. Its unnecessary using on = 0, off =1 make it easy to understand ? :bowing_man:

int on ; 
int off ; 

int RELAY_PIN_1;
int RELAY_PIN_2;

#define RELAY_PIN_1      14   //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2     13 //Pompa Air/Water Pump
``

i have add this ti track relay status :

int statusRelay1 = digitalRead(RELAY_PIN_1);
int statusRelay2 = digitalRead(RELAY_PIN_2);

Well, whatever translation method you’re using to convert what I wrote into your native language isn’t working very well, because you obviously haven’t understood any of what I said in post #26

Have you tried apps like Deepl or Google translate?

Pete.

Im using deepL sometime, i understand some of your text, but what make me miss understood is because theres some idk its like

Because im just learning iot, so im just Guessing what you mean in that #26 post.

this is my update, i add 1 more module LCD I2C for Local monitoring

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN            "-----------------------------------------"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 4          // What digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
DHT dht(DHTPIN, DHTTYPE);

WidgetLCD lcd2(V9); //Blynk LCD
WidgetLED led1(V2); //LED for Relay 1
WidgetLED led2(V3); //LED for Relay 2

float h ;
float t ; // or dht.readTemperature(true) for Fahrenheit


const int RELAY_PIN_1 = 14; //Lampu Halogen/Halogen Lamp
const int RELAY_PIN_2 = 13; //Pompa Air/Water Pump

//SDA pin on I2C module to pin D21 on ESP32
//SCL pin on I2C module to pin D22 on ESP32

//#define RELAY_PIN_1      14   //Lampu Halogen/Halogen Lamp
//#define RELAY_PIN_2     13 //Pompa Air/Water Pump

float MinTemperature = 25.0;
float MaxTemperature = 28.0;


BlynkTimer timer;

char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.

void sendSensor()
{

int statusRelay1 = digitalRead(RELAY_PIN_1);
int statusRelay2 = digitalRead(RELAY_PIN_2);


h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

    if (isnan(h) || isnan(t)) {
    lcd2.clear();
    lcd2.print(4, 1, "Gagal membaca sensor");// Sending to LCD Blynk "Failed to read data from sensor"
    return;
    }    
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  lcd.setCursor(0, 0); 
  lcd.print("H : ");
  lcd.print(h);
  lcd.print("%");
  
  lcd.setCursor(0, 1);
  lcd.print("T : ");
  lcd.print(t);
  lcd.print("C");

  if (t <= MinTemperature) {
    
    digitalWrite(RELAY_PIN_1, LOW); //Turn on Relay 1 
    digitalWrite(RELAY_PIN_2, HIGH);
    lcd2.clear(); //Use it to clear the LCD Widget
    lcd2.print(0, 0, "Suhu Dinginn"); //Sending " Cold Temperature" To Blynk
    lcd2.print(0, 1, "Apakah Lampu On?"); //Sending " Is that Lamp On?" To Blynk
  }
  
  else if (t >= MaxTemperature) {
    digitalWrite(RELAY_PIN_1, HIGH);
    digitalWrite(RELAY_PIN_2, LOW);//Turn on Relay 2
    lcd2.clear(); 
    lcd2.print(0, 0, "Suhu Panass!"); //Sending " Hot Temperature" To Blynk
    lcd2.print(0, 1, "Apakah Pompa On?"); //Sending " Is that Pump On?" To Blynk
  }

  if (statusRelay1 == 0)  { //Tracking Relay 1 
    led1.on();
    led2.off();
    }  
  else if (statusRelay2 == 0) { //Tracking Relay 2
    led1.off();
    led2.on();

    }
  
  }



void setup()
{
  // Debug console
  Serial.begin(115200);
  lcd.begin();
  lcd.backlight();
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  
  dht.begin();  
  
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
    
}

void loop()
{

  timer.run();
    Blynk.run();
}

Any advice :bowing_man:

You’ve declared these variables inside the sendSensor function, making them local to that function, and they are re-declared (and their previous values thrown away) each time that function is called.

If your intention is to track the current status of the relays, without the need to constantly re-read the status of those GPIO pins then you are taking the wrong approach.
If you simply want to interrogate those pins each time to discover their status then the status tracking variables are not needed.

Pete.

This way is better ?

 if (digitalRead(RELAY_PIN_1) == 0)

I guess that if that approach works for you then go for it.

Pete.

Ok , that works, but you have better use a flag as we told you instead of reading the digital pin📌

Then i will use this, so it will not constantly re-read ?

  if (t <= MinTemperature) {
    
    digitalWrite(RELAY_PIN_1, LOW); //Turn on Relay 1 
    digitalWrite(RELAY_PIN_2, HIGH);
    lcd2.clear(); //Use it to clear the LCD Widget
    lcd2.print(0, 0, "Suhu Dinginn"); //Sending " Cold Temperature" To Blynk
    lcd2.print(0, 1, "Apakah Lampu On?"); //Sending " Hot Temperature" To Blynk
    
      if (digitalRead(RELAY_PIN_1) == 0)  { //Tracking Relay 1 
        led1.on();
        led2.off();
    }  
  }

okok let me research first, and will update

if (t <= MinTemperature) {
    digitalWrite(RELAY_PIN_1, LOW); //Turn on Relay 1 
    digitalWrite(RELAY_PIN_2, HIGH);
    lcd2.clear(); //Use it to clear the LCD Widget
    lcd2.print(0, 0, "Suhu Dinginn"); //Sending " Cold Temperature" To Blynk
    lcd2.print(0, 1, "Apakah Lampu On?"); //Sending " Hot Temperature" To Blynk
    led1.on();
    led2.off();
    }