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);
}