DHT11 & Relay Arduino Uno

Hi All,

Wondering of anyone could shed some light on why the relay is not activating with this code.

Sorry of this is simple, I’m new to Arduino.

#include <dht.h>
dht DHT;

#define DHT11_PIN 7
int relay = 8;

void setup(){
 Serial.begin(9600);
 pinMode(relay, OUTPUT);
}

void loop() 
{
 int chk = DHT.read11(DHT11_PIN);
 Serial.print("Temperature = ");
 Serial.println(DHT.temperature);
 Serial.print("Humidity = ");
 Serial.println(DHT.humidity);
 delay(5000);
   
if("Temperature" > 31, relay, HIGH);
if("Temperature" < 16, relay, HIGH); //Sets off relay if Temperature is out of range

delay (8000);
digitalWrite(relay, LOW);

}

Please read the “How to use this forum” post and please use code tags when posting code.

if("Temperature" > 31, relay, HIGH);

You’re comparing a string of text to a number. That’s not making any sense.

if(DHT.temperature > 31, relay, HIGH);

That might make more sense. That’s where you’re getting the number from in the other part of the code where you printed it to the serial monitor.

If using code tages (</>), your program codes would appear like this (minor changes are done):

#include <dht.h>
dht DHT;

#define DHT11_PIN 7
int relay = 8;

void setup() 
{
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(5000);

  if (DHT.temperature > 31)//("Temperature" > 31, relay, HIGH);
  {
    digitalWrite(relay, HIGH);
  }

  if (DHT.temperature < 16)//("Temperature" < 16, relay, HIGH); //Sets off relay if Temperature is out of range
  {
    digitalWrite(relay, LOW);
  }
  delay (8000);
  //digitalWrite(relay, LOW);
}
if("Temperature" > 31, relay, HIGH);

Once you fix the problem of comparing a number with a string check that the brackets in your test are in the right place

if (something > 31)  //that's the test
{
  //the code to run if the test returns true goes here outside of the brackets of the test
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Thanks for the help, works great

Ok so I’ve taken the same idea and applied it to a Thinger code so I can monitor from the web.

Getting an error I haven’t come across before?

In function ‘global constructors keyed to 65535_0_thinger_trial_testing_DHT11_v1.1.ino.cpp.o’:
lto1.exe: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See http://gcc.gnu.org/bugs.html for instructions.
lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/…/lib/gcc/avr/5.4.0/…/…/…/…/avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

Anyone shed some light on this? Thanks

#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <ThingerEthernet.h>

// dht config
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int relay = 10;


// thinger.io config
ThingerEthernet thing("username", "xxxxxx", "xxxx");

void setup() {
  dht.begin();
  pinMode(relay, OUTPUT); //sets pin8 as relay output
  
  thing["RoomMonitor"] >> [](pson& out){
    out["Humidity"] = dht.readHumidity();
    out["Temperature"] = dht.readTemperature();
    
};
}

void loop() {  
    thing.handle();

if(dht.readTemperature() < 16) digitalWrite(relay, HIGH);

if(dht.readTemperature() > 31) digitalWrite(relay, HIGH);

delay (10000);

digitalWrite(relay, LOW);//Reset Alarm Relay
}