#include <WiFi.h>
#include <ThingSpeak.h>
#include <ESP32Servo.h>
#define DOOR_SENSOR_THRE 200
#define DISTANCE_SENSOR_TRIG_PIN 2
#define DISTANCE_SENSOR_ECHO_PIN 4
#define DOOR_AUTO_CLOSE_THRESHOLD (10*1000) // 10 sec
#define DOOR_CONTROL_PIN 32
#define DOOR_CLOSE_POS 0
#define DOOR_OPEN_POS 90
#define DOOR_MOTION_SENSOR_PIN 14
#define OPEN true
#define CLOSED false
Servo door;
// Ultrasonic declaration
// const int echo = 4;
// const int trig = 2;
// float distance;
// float duration;
// //LED declaration
const int ledPin = 13;
// // PIR declaration
// const int pirpin = 14;
// WiFi declaration
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
// Thingspeak declaration
unsigned long myChannelNumber = 2264423;
const char *myWriteAPIKey = "L3UJDG459I78V3RX";
int statusCode;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(DISTANCE_SENSOR_TRIG_PIN, OUTPUT);
pinMode(DISTANCE_SENSOR_ECHO_PIN, INPUT);
// Door control
door.attach(DOOR_CONTROL_PIN);
door.write(0);
// // Ultrasonic setup
// pinMode(trig, OUTPUT);
// pinMode(echo, INPUT);
// // PIR setup
// pinMode(pirpin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
// WiFi setup
WiFi.mode(WIFI_STA);
// Thingspeak setup
ThingSpeak.begin(client);
delay(1000);
}
// Ultrasonic function
// float getdistance()
// {
// digitalWrite(trig, LOW);
// delayMicroseconds(2);
// digitalWrite(trig, HIGH);
// delayMicroseconds(10);
// digitalWrite(trig, LOW);
// duration = pulseIn(echo, HIGH);
// return duration * 0.034 / 2;
// }
// void controlDoor(void)
void loop() {
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect...");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected");
}
static bool doorStatus = CLOSED; // initially the door is closed. by default
static unsigned long startTime = 0;
static int previousState = LOW;
// Start a new measurement:
//1. Set the Trigger PIN to HIGH
digitalWrite(DISTANCE_SENSOR_TRIG_PIN, HIGH);
//2. Let the Trigger PIN HIGH for 10 microseconds
delayMicroseconds(10);
//3. Set the Trigger PIN to LOW
digitalWrite(DISTANCE_SENSOR_TRIG_PIN, LOW);
// Thus, the Trigger PIN was made HIGH for
//10microseconds to trigger a new distance measuremnent.
// Read the result:
//ECHO pin goes high, and count the time it stays high (pulse length).
//The length of the ECHO high pulse is proportional to the distance
int duration = pulseIn(DISTANCE_SENSOR_ECHO_PIN, HIGH);
//calculates the distance in centimeters by formula
float distance = duration / 58.0;
//Initially there is no motion
bool motionDetected = false;
//check if there is any motion
int val = digitalRead(DOOR_MOTION_SENSOR_PIN);
if (val == HIGH)
{
if (previousState == LOW)
{
//Motion Detected
Serial.println("Motion Detected !");
previousState = HIGH;
motionDetected = true;
}
}
else
{
if(previousState == HIGH)
{
Serial.println("Motion ended!!");
previousState = LOW;
}
}
//Triggering the sensor will drive the OUT pin high for 5 seconds (delay time),
//and then go low again.
//The sensor will ignore any further input for the next 1.2 seconds (inhibit time),
// and then start sensing for motion again.
//IF There is a motion in the proximity of door
// THEN The door must open if initially closed
if(motionDetected == true && distance <= 200)
{
if(doorStatus == CLOSED)
{
door.write(90); //rotate the servo motor by 90-degree
doorStatus = OPEN; //the door is opened now
Serial.println("Motion detected and < 200 cm proximity ... opening door");
startTime = millis(); // start calculating time for which door is opened
}
// To calculate the time for which door is opened, using millis().
else // IF motion detected in proximity of door and door is open
{ //THEN keep the door open, just start the timer again
Serial.println("Motion is detected again and < 200cm proximity ...\
Keeping the door open");
startTime = millis(); //calculate the latest time door is opened
//The door will remain open if the distance is continues to be
//less than 200 cms as people are keep coming.
}
}
else if (doorStatus == OPEN) // IF No motion near the door and the door is OPEN
{ // THEN close the door after certain time
// when there is no motion near the door.
if ((millis() - startTime) > DOOR_AUTO_CLOSE_THRESHOLD)
{
door.write(0); // change the servo position back to initial and close the door
//Autoclosing of door
doorStatus = CLOSED; // the door is closed
}
}
// Serial.println("Distance:" + String(distance));
// Serial.println("<-------------------->");
// Data uploading to ThingSpeak
ThingSpeak.setField(1, distance);
ThingSpeak.setField(2, motionDetected);
ThingSpeak.setField(3, doorStatus);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
// // Check PIR sensor
// int motion = digitalRead(pirpin);
// Serial.println(motion == HIGH ? "Motion detected!" : "Motion not detected!");
// Serial.println("PIR Status: " + String(motion));
// // Turn on the LED when motion is detected
// digitalWrite(ledPin, motion);
// // WiFi connection
// // Read distance
// distance = getdistance();
// ThingSpeak.setField(1, data.temperature);
// ThingSpeak.setField(2, data.humidity);
// ThingSpeak.setField(4, motion);
// statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// if (statusCode == 200)
// {
// Serial.println("Channel update successful");
// }
// else
// {
// Serial.println("Problem Writing data: HTTP error code:" + String(statusCode));
// }
// delay(15000);