DHT22 w/ Particle Photon

I am working on a group project to turn on and off lightbulbs with a relay connected to a particle photon. The photon should be connected to a DHT22 temperature and humidity sensor. It must send a signal to the relay to open or close the bulb switch based on the temperature reading. We have all the bulbs/ relay connecterd properly, but cannot get the code to work. I have looked at basically every website I can find and am at a loss. Any help with the code and wiring is helpful. Thank you!

Heres a picture of my wiring and my attached code. I am running the code in the particle web IDE.

CODE 1 (doesnt work, returns -5000 something for temp)

#include <PietteTech_DHT.h>
#include "Adafruit_DHT.h" // DHT library of functions

#define DHTPIN D2  // what pin we are connected to

// DHT TYPE
#define DHTTYPE DHT22		// DHT 22 (AM2302)

// connect pin 1 to 5V
// connect pin 2 to whatever the DHTPIN is
// connect pin 4 to ground
// connect a 10k resistor from pin 2 to pin 1

String sendData; // the string we use to send data

DHT dht(DHTPIN, DHTTYPE); // sets instance

void setup()
{
    dht.begin(); // starts instance
    pinMode(D7,OUTPUT); // output pin for relay switch
}

void loop()
{
    delay(2000); // 2 second delay

    //Read data and store it to variables hum and temp
    float humidity = dht.getHumidity();
    
    float temperature = dht.getTempFarenheit();
    
    sendData = String::format ("%.1f %, %.1f F", humidity, temperature); //formats string sendData to "Humidity to .1% accuracy and Temperature to .1 degree accuracy"
    
    Particle.publish("Humidity and Temperature:", sendData); // the actual publish


    // test conditions for bulbs
    if(temperature < 99.5) // if temp is under 99.5 then turn on bulbs
    {
        digitalWrite(D7, HIGH);
    }
    if(temperature > 100.5) // if temp is over 100.5 then turn off bulbs
    {
        digitalWrite(D7, LOW);
    }
}

CODE 2 (returns -5 for temp and humidity)

#include <PietteTech_DHT.h>

#define DHTTYPE  DHT22
#define DHTPIN   D4

PietteTech_DHT DHT(DHTPIN, DHTTYPE);

double Temp;
double Humidity;

void setup()
{
  Particle.variable("Temp", &Temp, DOUBLE);
  Particle.variable("Humidity", &Humidity, DOUBLE);
  pinMode(D7,OUTPUT); // output pin for relay switch
}

void loop()
{
  Temp = DHT.getFahrenheit();
  Humidity = DHT.getHumidity();
  
  String sendData = String::format ("%.1f %, %.1f F", Humidity, Temp); //formats string sendData to "Humidity to .1% accuracy and Temperature to .1 degree accuracy"

  Particle.publish("Humidity and Temperature:", sendData); // the actual publish
  
  if(Temp < 99.5){
      digitalWrite(D7, HIGH);
  }
  else if(Temp > 100.5){
      digitalWrite(D7, HIGH);

  }
  
  delay(3000);
}

Please help! We are on a big time crunch.

Hi Oliver, could it be the perspective of the picture, but I think your DHT is not connected properly to GND:


Could it be?

EDIT: PLEASE look ONLY at the DHT sensor, the Particle device is outdated.

1 Like

It’s always good to try (and study) one of the library examples to see whether they work.
If they don’t double check your hardware setup (e.g. continuity in your breadboard, correct voltage levels everywhere, …)

@gusgonnet, this fritzing is based on a Core which features a 3*3 pin (capable of powering the sonsor) where the Photon has a VBAT pin which won’t power the sensor.
As far as I can see the sensor seems to be connected properly, but I wouldn’t vouch for the breadboard and jumper contacts without measuring :wink:

BTW, @oswhite02. Your code never acquires the data you want :wink:
The return code -5 stands for DHTLIB_ERROR_ACQUIRING (can be found in the library sources and the library examples).
This isn’t surprising as you haven’t actually acquired the readings.

In your second code you are using D4 while the image shows the sensor connected to D2.
Your first code includes two DHT libraries which isn’t good style and may cause other problems.

1 Like

thank you ScruffR for the correction!

How do I “acquire” the data? If I do that should it publish the correct temperature and humidity?

e.g. Particle Web IDE

I got it thank you so much!