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

Detail :

MCU : ESP 32

Wired
DHT 22:
VCC : 3.3 V
Data : GPIO 4 / D4
GND : Gnd

Relay 2 Channel :
VCC : 5 V
IN1 : GPIO 12 / D12
IN2 : GPIO 13 / D13
GND : Gnd

Problem : DHT 22 didnt send any data to Blynk, but Blynk is Online.

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN            "------------------------------------"
#define WIFI_SSID "-----------------------------"             //Enter Wifi Name
#define WIFI_PASS "-------------------------t"         //Enter wifi Password

#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

int relay1 = 12;
int relay2 = 13;

DHT dht(4, DHT22);

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

BlynkTimer timer;
char ssid[] = "-------------------";// Your WiFi credentials.
char pass[] = "------------------";// Set password to "" for open networks.

void sendSensor()
{
    if (isnan(h) || isnan(t)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT 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);
  
}

void relayonoff ()
{
  if (t >= 27.5 && t <= 29.5)  {
    digitalWrite(relay1, LOW); 
    digitalWrite(relay2, LOW); 
  }
  else if (t <= 25 && t >= 27) { // Lampu Menyala
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
  }
  else if (t >= 30 && t <= 32) { // Pompa Air Menyala
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW); 
  }
}

void display()
{
  if (relay1 == HIGH)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  //Water Pump On
  }
  
  else if (relay2 == LOW)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  //Halogen Lamp On
  }  
}

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

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);

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

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

Thanks For Your Time :bowing_man:

void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); 
if (isnan(h) || isnan(t)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
    return;
    }
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}

Sry, What do you suggest??

Your version of SendSensor() is missing the code to take temperature and humidity readings from the DHT sensor.

@Blynk_Coeur’s version adds this in, but re-declares the t and h variables as local, which won’t work correctly in the relayonoff function, so your SendSensor should look like this…

void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature(); 
if (isnan(h) || isnan(t)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
    return;
    }
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}

Pete.

1 Like

It makes no sense to call relay function every 500 ms
It’s better to integrate relay condition into sensor function.

1 Like

if (t <= 25 && t >= 27)
It will never match :joy:

1 Like

Okoke, I have edit it and ~

~I have try with just using DHT22 Modul it work but when i try DHT22 + Relay 2 Channel the DHT22 didnt send any data.

Thanks for showing it

1 Like

What happens if t= 29.6 ? :thinking:
In this case, neither the lamp nor the water pump will be turned on as the temperature is outside the specified range.

Additionally, you have to add hysteresis to the system, which will cause the output to stay in one state until the input has changed by a certain amount, thus preventing rapid switching.

Maybe if you shared your latest code, and details of how you are powering your relays, we would have a better idea of the problem.

Pete.

Thanks for the advice, i edit it and try. once more when i just use DHT22 Module its fine but when i use DHT22+Relay 2 Channel the DHT22 didnt send data.

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN            "MVL0V-6emdEBLETnnYMWS3ho7SvKVYxf"
//#define WIFI_SSID "TexasKost"             //Enter Wifi Name
//#define WIFI_PASS "eviudahpwt"         //Enter wifi Password
//#define BLYNK_TEMPLATE_ID           "TMPLv5vCaiaj"
//#define BLYNK_DEVICE_NAME           "Control relay"

#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>

#define DHTPIN 4  
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);

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

int relay1 = 12;
int relay2 = 13;
int temperatureSensor = A0;
int thresholdLow = 25;
int thresholdHigh = 28;

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)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT 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);

  int temperature = analogRead(temperatureSensor);
  temperature = map(temperature, 0, 1023, 0, 100);

  if (temperature <= thresholdLow) {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
  }
  else if (temperature >= thresholdHigh) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
  }
  

  
}


void display()
{
  if (relay1 == HIGH)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (relay2 == LOW)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  
}

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();
}

I don’t understand this code at all.
What sort of sensor is attached to your analog pin, and what is it used for.
What is its role compared to the DHT sensor, and why have you created variables called temperature and humidity instead of the t and h variables used to store the DHT readings?

Pete.

I have change it =

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

#define DHTPIN 4  
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);

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

int relay1 = 12;
int relay2 = 13;

int thresholdLow = 25;
int thresholdHigh = 28;

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)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT 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);

  t = map(t, 0, 1023, 0, 100);

  if (t <= thresholdLow) {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
  }
  else if (t >= thresholdHigh) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
  }
  
}

void display()
{
  if (relay1 == HIGH)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (relay2 == LOW)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  
}

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();
}

What’s the purpose of this line of code?

These lines that are near the top of the sketch should simply say…

float h; // humidity 
float t; // temperature 

Pete.

Its say to change scale of censor data from 0-1023 to 0-100.

I have change my sketch, it work Blynk, Relay, Dht22. But there’s error when Temperature >= thresholdHigh Relay 1 is On, in sketch relay 2 should be On.

#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);

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

int relay1 ;
int relay2 ;

#define relay1      13   
#define relay2      14   

int thresholdLow = 25;
int thresholdHigh = 28;

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)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT 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 <= thresholdLow) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
  }
  else if (t >= thresholdHigh) {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
  }
 
  
}


void display()
{
  if (relay1 == HIGH)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (relay2 == LOW)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  
}

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();
}

Well, this piece of code makes no sense because you are using relay1 and relay2 as aliases for the pins the relays are connected to…

C++ treats LOW as 0 and HIGH as 1 so your void display() is actually being interpreted as…

void display()
{
  if (13 == 1)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (14 == 0)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  
}

If you want to track the current status of each relay then you need to add two additional variables to do this.

Also, are your relays active HIGH or active LOW ?

Pete.

Active high

i have change this Define to

int relay1 = 13; /D13
int relay2 = 14; /D14

I try this sketch now

#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);

float MinTemperature = 25;
float MaxTemperature = 28;

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

int relay1 = 13;
int relay2 = 14;


//#define relay1      13   
//#define relay2      14   

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)) {
    Blynk.virtualWrite(V9, "Failed to read from DHT 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, HIGH);
    digitalWrite(relay2, LOW);
  }
  else if (t >= MaxTemperature) {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
  }
  
  if (relay1 == 1)  {
  Blynk.virtualWrite(V9, "Pompa Air Menyala");  
  }
  
  else if (relay2 == 0)  {
  Blynk.virtualWrite(V9, "Lampu Halogen Menyala");  
  }  
  
}

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();
}

That it will make no difference to the functionality as you’ve not added additional variables to track the state of your relays.

Try re-reading what I wrote until you understand the point I was making.

Pete.