/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*//*
PIR sensor tester
*/
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
int ledPin = 2; // choose the pin for the LED
int inputPin = 14; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
#include <ESP32Servo.h>
const int servoPin = 18;
Servo servo;
void setup(){
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
} {
pinMode(2, OUTPUT);
pinMode(13, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
// attach servo pin with a min/max timings of 500 microseconds and 2400 microseconds
servo.attach(servoPin, 500, 2400);
}
// initialize position of 0 degrees
int pos = 0;
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}{
int sw= digitalRead(13);
if( sw == LOW){
digitalWrite(2, HIGH);
delay(500);
}
else
{
digitalWrite(2,LOW);
delay(500);
}
// put your main code here, to run repeatedly:
delay(100); // this speeds up the simulation
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
// for loop that increases the angle by 1 from 0 to 180 with a delay of 10 ms per angle
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(10);
}
// for loop that decreases the angle by 1 from 180 to 0 with a delay of 10 ms per angle
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(10);
}
}