ESP8266+Relay+2 Temperature sensors

Hi,

I need to control a relay based on two temperature sensors.

For example, if the temperature 1 is higher than temperature 2 turn on the relay…or something like that.

Can you help me, please ?

You cannot do this with single trigger, but can two separate trigger to turn ON two button and then read it in the code and only turn ON the relay if both button are true.

I just want to do that with ESP8266. I dont need to use Cayene for this prject.

I need the code for ESP8266.

Hi!
You just read the temps and compare them in order to turn the relay on/off
Ex:
if tempC01 >= tempC02
{
digitalWrite(Relay, HIGH);
}
else {
digitalWrite(Relay, LOW);
}

You need to specify this thing and you can use @grigoreeugen1 very simple code.

This code is working…it measure the temperature and humidity from 2 sensors (AM2301).
But i cant turn on/off the relay. I used the code of @grigoreeugen1, but i dont know why its not working.
I need to turn on the relay if the temperature 1 is 4 grade Celsius higher than temperature 2.

#include “DHT.h”

#define DHT1PIN 2 // what pin we’re connected to
#define DHT2PIN 3

const int relayPin = 8;

#define DHT1TYPE DHT21 // DHT 21 (AM2301)
#define DHT2TYPE DHT21 // DHT 21 (AM2301)

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

void setup() {
Serial.begin(9600);
Serial.println(“DHTxx test!”);

dht1.begin();
dht2.begin();
}

void loop() {

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1)) {
Serial.println(“Failed to read from DHT #1”);
} else {
Serial.print(“Humidity 1: “);
Serial.print(h1);
Serial.print(” %\t”);
Serial.print(“Temperature 1: “);
Serial.print(t1);
Serial.println(” *C”);
}
if (isnan(t2) || isnan(h2)) {
Serial.println(“Failed to read from DHT #2”);
} else {
Serial.print(“Humidity 2: “);
Serial.print(h2);
Serial.print(” %\t”);
Serial.print(“Temperature 2: “);
Serial.print(t2);
Serial.println(” *C”);
}
Serial.println();

if (t1 >= t2 ) {
digitalWrite(relayPin, HIGH);
}
else {
digitalWrite(relayPin, LOW);
}
delay(1000);// one second delay
}

Hi, please try
#include “DHT.h”

#define DHT1PIN 2 // what pin we’re connected to
#define DHT2PIN 3

#define relayPin 8

#define DHT1TYPE DHT21 // DHT 21 (AM2301)
#define DHT2TYPE DHT21 // DHT 21 (AM2301)

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

void setup() {
Serial.begin(9600);
Serial.println(“DHTxx test!”);

dht1.begin();
dht2.begin();
}

void loop() {

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1))
{
Serial.println(“Failed to read from DHT #1”);
}
else
{
Serial.print(“Humidity 1: “);
Serial.print(h1);
Serial.print(” %\t”);
Serial.print(“Temperature 1: “);
Serial.print(t1);
Serial.println(” *C”);
}

if (isnan(t2) || isnan(h2))
{
Serial.println(“Failed to read from DHT #2”);
}
else
{
Serial.print(“Humidity 2: “);
Serial.print(h2);
Serial.print(” %\t”);
Serial.print(“Temperature 2: “);
Serial.print(t2);
Serial.println(” *C”);
}
Serial.println();

if (t1 > t2 + 4 )
{
digitalWrite(relayPin, HIGH);
}
else
{
digitalWrite(relayPin, LOW);
}

delay(1000);// one second delay
}

I have tried that, but its not working.

Maybe @shramik_salgaonkar can help us.

what is not working? the code is correct, are you getting reading from both sensors (t1 and t2)? has t1 gone 4C above t2??

I can read data from two sensors but the relay doesn’t change the state

that does not answer my question. can you share a screenshot of the serial monitor where t1 > t2 +4

I don’t have 2 DHTs but I tried with two DS18B20 and the code is working perfectly :slight_smile: image

I dont know why its not working.

have you assigned and set the relaypin as output?

1 Like

Are you using the same code ?

Can you share the code please ?

hmm i dont know. Can you help me with that please ?

Yeah I think I forgot to tell him that :frowning:

tell me please

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

https://www.arduino.cc/en/Tutorial/DigitalPins

Put
pinMode(relayPin, OUTPUT);

in void setup()