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

This is re-reading the pin, instead of using a global variable to store the current status of the relay - which would be updated only when you changed the status through a digitalWrite.

We’ve both explained this approach multiple times, but you don’t seem to be able to grasp the concept.

Pete.

1 Like

i see, u mean use this ? Sry guys i will learn harder :bowing_man:

The update :

#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

const float MinTemperature = 25.0;
const float MaxTemperature = 28.0;

BlynkTimer timer;

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

void sendSensor()
{

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

    if (isnan(h) || isnan(t)) {
    lcd2.clear();
    lcd2.print(0, 0, "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); //menentukan posisi kursor pada LCD
  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 it Lamp On?" To Blynk
    led1.on();
    led2.off();
    }  
  
  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 it Pump on?" To Blynk
    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();
}

Does it work?

Pete.

It work sir