#include <DHT.
h>
#define DHTPIN 5 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define FAN_PIN 2 // FAN RELAY
//WidgetLED FAN (VO) ;
float humDHT = 0;
float tempDHT = 0;
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
#define timeSeconds 2
// Set GPIOs for LED and PIR Motion Sensor
const int led = 26;
const int motionSensor = 27;
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
Serial.println("MOTION DETECTED!!!");
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
void setup() {
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
Serial.println(F("DHTxx test!"));
dht.begin();
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set
RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement,
RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
void loop() {
delay(250);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humDHT = dht.readHumidity();
// Read temperature as Celsius (the default)
tempDHT = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humDHT) || isnan(tempDHT))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print(F("Temperature: "));
Serial.print(tempDHT);
Serial.print(F("°C "));
Serial.println();
Serial.print(F("Humidity: "));
Serial.print(humDHT);
Serial.print(F("%"));
Serial.println();
Serial.println("***********************");
Serial.println();
// Compare Threshold value from Blynk and DHT Temperature value.
if (tempDHT>31)
{
digitalWrite(FAN_PIN, HIGH);
}
else {
digitalWrite(FAN_PIN, LOW);
}
// Current time
now = millis();
// Turn off the LED after the number of seconds defined in the timeSeconds
variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
}
}