Problem with dht11 signaling 5v relay at a specific temperature

Hello I am struggling to write code that triggers a 5v relay when my dht11 hits a certain temp. A background on the project if you care: I live in a house without AC, I have a ferret and he does not do well in the heat. I have modified a 12v portable cooler and attached it to his cage for him to hang out in. My UNO already monitors external temp, and internal temp/humidity. Problem is if it gets too cold he won't go in because he's a picky lil guy.

tl;dr

I want the relay to shut off when it passes the internal temperature threshold. Ideally it would only kick on after it went above 60 and shut off if it got below 50, but as long as it doesn't get too cold in there that is my goal. Here is my code:

// include the library code:
#include <LiquidCrystal.h>
#include <DHT.h>

#include <Adafruit_RGBLCDShield.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <utility/Adafruit_MCP23017.h>

#define Grove_Water_Sensor 6
#define ONE_WIRE_BUS 8
#define DHTPIN 7 // set the DHT Pin
#define relayPin 9
#define relayPin2 2


#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

OneWire P0(ONE_WIRE_BUS);
DallasTemperature sensors(&P0);
DeviceAddress Probe0 = {0x28, 0xFF, 0xC3, 0xC4, 0x84, 0x16, 0x05, 0x96};

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

int chk;
int temp = dht.readTemperature();

void setup() 
{
  // set up the LCD's number of columns and rows: 
  
  sensors.begin();
  Serial.begin(9600);

  pinMode(6, INPUT); //Water Sensor
  //pinMode(7, OUTPUT);
  
  pinMode(9, OUTPUT); //Relay pin
  pinMode(2, OUTPUT); //Relay pin2
  
  sensors.setResolution(Probe0, 2);
  lcd.begin(16, 2);
  dht.begin();
  
  // Print a message to the LCD.
  //         0123456789ABCDEF
  lcd.print("Int:  H%:  Ext:");
  
  


int deviceCount = 0; //RTTC NE
int tempF0 = 0; // ""
int tempC = 0; // ""
int tempF = 0; // ""

}

void loop() 
{
  //int chk = DHT.read11(DHTPIN);
  //temp = dht.readTemperature();
  
  sensors.requestTemperatures();

  if(temp<61) {
    digitalWrite(relayPin2, HIGH);
  }else {
    digitalWrite(relayPin2, LOW);
  }


  if(digitalRead(Grove_Water_Sensor) == LOW) {
    digitalWrite(relayPin, LOW);
  }else {
    digitalWrite(relayPin, HIGH);
  }  
          
    lcd.setCursor(0, 1);
  // read humidity
  int h = dht.readHumidity();
  //read temperature in Fahrenheit
  int f = dht.readTemperature(true);

  lcd.setCursor(11,1);
  displayTemperature(Probe0); 
  
  if (isnan(h) || isnan(f)) {
    lcd.print("ERR");
    return;
    
      
      
    }
  
  lcd.setCursor(0,1);
  lcd.print(f);
  lcd.setCursor(6,1);
  lcd.print(h);

  delay(2500);
}

/*-----( Declare User-written Functions )-----*/
void displayTemperature(DeviceAddress deviceAddress)
{

int tempF = sensors.getTempF(deviceAddress);

   if (tempF == -196) // Measurement failed or no device found
   {
    lcd.print("ERR");
   } 
   else
   {

   lcd.print(tempF);
   
   }
}

Thanks for any help you can provide!

look at your displayTemperature() function. It returns the current temperature

int tempF = sensors.getTempF(deviceAddress);

look at your loop() code

  sensors.requestTemperatures();

  if(temp<61) {
    digitalWrite(relayPin2, HIGH);
  }else {
    digitalWrite(relayPin2, LOW);
  }

do you see anywhere that the variable 'temp' is getting set? I don't.

Thanks! I don't know how to set the "temp" variable to look at the DHT11 sensor. I have a Dallas water proof temperature sensor. Excuse my ignorance because the code has been cobbled together from other code I found on the internet but I thought the int tempF = sensors pertain to the Dallas sensor, not the dht11. The Dallas sensor is external temp, which isn't used for triggering the relay.

It appears you are trying to run before you can even crawl. Slow down and take a look at the examples that come with the libraries you are using. In the IDE, Go to File->Examples->DHT Sensor Library and look at the examples. Get them working with your sensor. Then start to integrate that working code into your real project.

FYI... variables don't "look" at sensors. You read a sensor, by calling a function and the return value is then assigned to a variable that you can use later... It sounds like you might benefit from some C/C++ basic tutorials as well.

Thanks for the response, I know I am a little out of my depth. I wouldn't be doing this if I wasnt desperate. Thank you for listing the library, it helped with some understanding but I am still struggling.

I realize my coding vocabulary is not complete, let alone robust. I know I have to make a request of the DHT11 sensor, and compare the result to a set value and then trigger the relay. I just don't know how to translate this in to code...

I know I have to make a request of the DHT11 sensor

There are several libraries dealing with this sensor. All of them provide a function with a name that clearly indicates that it is getting the temperature.

   float theCurrentTemp = someInstanceOfSomeDHTClass.getMeTheCurrentTemperature();

You'll need to replace the first part with the instance name that you choose when instantiating the class, and you'll need to replace the second part with the correct method name.

and compare the result to a set value

   if(theCurrentTemp < tooCold)
   {
   }
   else if(theCurrentTemp > tooHot)
   {
   }

and then trigger the relay.

If it is too hot? Too cold? Which pin is relay connected to? Does HICH energize the relay/complete the circuit? Or, does LOW do that?

Whatever the answers are, digitalWrite() needs to be used, with the appropriate pin number, and the appropriate state, and needs to be called in the appropriate place. Where that is should be pretty obvious.

Thank you everyone! I figured it out! I wanted the relay controlling the power to the 12v cooler to be on the NC terminal incase there were any failures of the arduino unit it would cool by default.

I found that the problem was not calling the right instance(?) which I was trying to define as "temp" in earlier versions of the code. I had tried putting "f" in to this portion of the code before:

if(f<58) {
    digitalWrite(relayPin2, HIGH);
  }else {
    digitalWrite(relayPin2, LOW);
  }

For whatever reason the code wouldn't recognize the "f" instance(?) until I moved the if statement controlling the relay to after the section where it was defined. For whatever reason, this made the code work. This is the working code for those who are interested:

// include the library code:
#include <LiquidCrystal.h>
#include <DHT.h>


#include <Adafruit_RGBLCDShield.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <utility/Adafruit_MCP23017.h>

#define Grove_Water_Sensor 6
#define ONE_WIRE_BUS 8
#define DHTPIN 7 // set the DHT Pin
#define relayPin 9
#define relayPin2 2


#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

OneWire P0(ONE_WIRE_BUS);
DallasTemperature sensors(&P0);
DeviceAddress Probe0 = {0x28, 0xFF, 0xC3, 0xC4, 0x84, 0x16, 0x05, 0x96};

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

int chk;
int temp = dht.readTemperature(true);

void setup() 
{
  // set up the LCD's number of columns and rows: 
  
  sensors.begin();
  Serial.begin(9600);

  pinMode(6, INPUT); //Water Sensor
  //pinMode(7, OUTPUT);
  
  pinMode(9, OUTPUT); //Relay pin
  pinMode(2, OUTPUT); //Relay pin2
  
  sensors.setResolution(Probe0, 2);
  lcd.begin(16, 2);
  dht.begin();
  
  // Print a message to the LCD.
  //         0123456789ABCDEF
  lcd.print("Int:  H%:  Ext:");
  
  


int deviceCount = 0; //RTTC NE
int tempF0 = 0; // ""
int tempC = 0; // ""
int tempF = 0; // ""

}

void loop() 
{
  sensors.requestTemperatures();

  if(digitalRead(Grove_Water_Sensor) == LOW) {
    digitalWrite(relayPin, LOW);
  }else {
    digitalWrite(relayPin, HIGH);
  }  
          
    lcd.setCursor(0, 1);
  // read humidity
  int h = dht.readHumidity();
  //read temperature in Fahrenheit
  int f = dht.readTemperature(true);

  lcd.setCursor(11,1);
  displayTemperature(Probe0); 
  
  if (isnan(h) || isnan(f)) {
    lcd.print("ERR");
    return;
    
      
      
    }
  
  lcd.setCursor(0,1);
  lcd.print(f);
  lcd.setCursor(6,1);
  lcd.print(h);

  if(f<58) {
    digitalWrite(relayPin2, HIGH);
  }else {
    digitalWrite(relayPin2, LOW);
  }

  delay(2500);
}

/*-----( Declare User-written Functions )-----*/
void displayTemperature(DeviceAddress deviceAddress)
{

int tempF = sensors.getTempF(deviceAddress);

   if (tempF == -196) // Measurement failed or no device found
   {
    lcd.print("ERR");
   } 
   else
   {

   lcd.print(tempF);
   
   }
}

Once again thanks to y'all for pointing me in the right direction. I am going to keep plugging away at coding until I can help others like you do.