Arduino Attic Fan Controller

Need some Arduino coding help. The following code should turn a relay on and off based on the reading from 2 DHT22’s. Relay turns on but never off… Any help?

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “Blank”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “wifi”;
char pass[] = “pasword”;

#define RELAYON 1
#define RELAYOFF 0
#define DHT1PIN D5 // What digital pin we’re connected to;
#define DHT2PIN D6 // What digital pin we’re connected to;
#define DHT1TYPE DHT22 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
#define DHT2TYPE DHT22 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
BlynkTimer timer;
float t;
float t2;
float h;
float h2;
int FanRelay = D7;

int FanRelayState = RELAYOFF;

void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht1.begin();
dht2.begin();
pinMode( FanRelay, OUTPUT );

FanRelayState = RELAYOFF;

digitalWrite(FanRelay, FanRelayState);

timer.setInterval(1000L, sendSensor);
}

void loop()
{
Blynk.run();
timer.run();

if (dht1.readTemperature() < dht2.readTemperature()) {
FanRelayState = RELAYOFF;
digitalWrite( FanRelay, FanRelayState );
}

if (dht1.readTemperature() > dht2.readTemperature()) {
FanRelayState = RELAYON;
digitalWrite( FanRelay, FanRelayState );
}

}

// This function sends Arduino’s up time every second to Virtual Pin (5).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
h = dht1.readHumidity();
h2 = dht2.readHumidity();
t = dht1.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
t2 = dht2.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
// l = analogRead(LDR);
if (isnan(h) || isnan(t)) {
Serial.println(“Failed to read from DHT sensor!”);
return;}

// You can send any value at any time.
// Please don’t send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V7, h2);
Blynk.virtualWrite(V8, dht2.readTemperature(true));
Blynk.virtualWrite(V9, FanRelayState);
}

The first thing I’d do is write a dead-simple application that switches the relay on and off based on a single input like a pushbutton switch or perhaps cycles the fan on and off with a x-second timer. Basically a “Hello Fan!” program. Get all of that other code out of the mix to determine whether it’s actually a code problem.

You didn’t say what type of relay you’re using but there are a number of reasons why a solid state relay can fail to turn off when commanded. See: http://www.omron-ap.com/service_support/FAQ/FAQ02155/index.asp

If you’re using an electromechanical relay the above won’t apply but as a side note you should be sure to include some sort of snubber since you’re switching an inductive load.

I think you need an “else if” for your second conditional. As I read it, your code will always go through the first “if” and then always go through your second “if” rather than choose between the two options.

Here’s the Arduino doc on the syntax:
https://www.arduino.cc/reference/en/language/structure/control-structure/else/

Thank you mattarmstrong. I used the else statement by itself

2 Likes

I don’t agree. Those two if… statements have no effect on each other. They will both be executed.