I have made a project for a mobile home shower where the object is to site a PIR and DHT22 sensor within the shower cubicle The PIR is to switch on a lighting circuit when a person is in the shower and the DHT22 will switch on an extractor fan whenever the humidity is above a certain threshold (60%) within the cubicle.
The sensors detect and the switching is done by two separate relays.
I have used an ESP 32 and have a 16x2 LCD display attached to the project box outside of the cubicle to display the humidity at anytime.
The code is not pretty looking but it works with one exception.
If the PIR is triggered and the relay switching the lights is active, the humidity sensor reading is not updated and the display and humidity driven relay do not activate until the PIR switches off. whereas I wold like them operate simultaneously with each other. Can anyone suggest a tweak to my code , or is it more fundamental that that.?
```cpp
//Camper code for HC-SR501 PIR and DHT22 humidity sensor
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHT22_PIN 23 // ESP32 pin GPIO23 connected to DHT22
#define RELAY_PIN 26 // ESP32 pin GPIO26 connected to PIR Relay's pin
#define RELAY1_PIN 18 // ESP32 pin GPIO18 connected to FAN relay
#define DHT_SENSOR_TYPE DHT22
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
//#define HUM_UPPER_THRESHOLD 90 // upper Humidity threshold not neede
#define HUM_LOWER_THRESHOLD 50 // lower Humidity threshold
DHT dht22(DHT22_PIN, DHT_SENSOR_TYPE);
#define MOTION_SENSOR_PIN 27 // ESP32 pin GPIO27 connected to the OUTPUT pin of motion sensor
int motionStateCurrent = LOW; // current state of motion sensor's pin
int motionStatePrevious = LOW; // previous state of motion sensor's pin
void setup() {
Serial.begin(9600); // initialize serial
dht22.begin(); // initialize the DHT sensor
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 pin to input mode
pinMode(RELAY_PIN, OUTPUT); // set ESP32 PIR pin to output mode
pinMode(RELAY1_PIN, OUTPUT); // set ESP32 FAN pin to output mode
}
void loop() {
motionStatePrevious = motionStateCurrent; // store old state
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state
if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!, turn PIR relay ON");
digitalWrite(RELAY_PIN, HIGH); // turn on
} else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!, turn PIR relay OFF");
digitalWrite(RELAY_PIN, LOW); // turn off
{
float humidity = dht22.readHumidity(); // read humidity as number
if (isnan(humidity)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.print("Humidity is : ");
Serial.println(humidity);
// set cursor to first column, first row
lcd.setCursor(0, 0);
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(humidity);
delay(1000);
if (humidity > HUM_LOWER_THRESHOLD) {
Serial.println("Fan relay on");
digitalWrite(RELAY1_PIN, HIGH); // turn on
//Serial.print("Humidity is : ");
//Serial.println(humidity);
} else if (humidity < HUM_LOWER_THRESHOLD) {
Serial.println("Fan relay off");
digitalWrite(RELAY1_PIN, LOW); // turn off
delay(500);
}
}
}
}
}