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.