/*////////////////////////////////////////////////////////////////////////////
DESCRIPTION:
Smart door system uses ultrasonic sensor to sense and confirm the arrival of
guests at your doorstep and automates the doorbell to inform the inhabitant
of the household. Additionally it also provides remote app control with WiFi
features using MIT App Inventor to enable or disable the door lock which,
here, is implemented using a Servo motor.
////////////////////////////////////////////////////////////////////////////*/
#include <WiFi.h>
#include <ESP32Servo.h> // Cambiado de <Servo.h> a <ESP32Servo.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <ThingSpeak.h>
const char* SSID = "Wokwi-GUEST";
const char* PWD = "";
unsigned long int ChannelNumber = 2579993;
const char* writeAPIKey = "YSRRAER2SWJ593WE";
const char* readAPIKey = "J7VOV2VA42EHXIZ4";
WiFiClient client;
// Pin configuration - Ultrasonic Sensor
#define echoPin 18 // Echo pin connected to GPIO 18
#define trigPin 19 // Trigger pin connected to GPIO 19
#define buzzerPin 32 // Buzzer pin connected to GPIO 32
int echoDuration;
int distance;
// Linking Servo Motor
Servo doorServo; // Servo motor for door
//setup OLED display
#define SCREEN_WIDTH 128 //pixels
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
// Setting up Pin functions
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialising WiFi
WiFi.begin(SSID, PWD);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi Connected.");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
// Initialising Servo Motor
doorServo.attach(23); // Attach the Servo motor to pin 23 as per the diagram
//initializing OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
while(1);
}
display.clearDisplay();// Clearing buffer
// Initialzing the OLED display
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,30);
display.println("Smart Door Lock");
display.display();
doorServo.write(0); // Initial position of the Servo motor
}
void writeOled(char* str) {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setCursor(0,30);
display.println(str);
display.display();
}
int findDistance() {
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
echoDuration = pulseIn(echoPin,HIGH);
distance = (0.034 * echoDuration)/2;
return distance;
}
float avg(float *lst, int len) {
float s = 0;
for (int i = 0; i < len; i++) {
s += lst[i];
}
return s / len;
}
float calcDist() {
int i = 0;
int rep = 5;
float dists[rep+1];
while (i <= rep) {
float distance1 = findDistance();
//Serial.println(distance1);
dists[i] = distance1;
delay(200);
i++;
}
return avg(dists, rep+1);
}
bool checkGuest() {
float distlst[4];
for (int i = 0; i < 4; i++) {
distlst[i] = calcDist();
delay(500);
}
float confirm = avg(distlst, 4);
if (confirm <= 40) {
Serial.print("Guest Alert at ");
Serial.println(confirm);
return true;
} else {
return false;
}
}
void ringDoorBell() {
for(int i=1;i<=3;i++){
writeOled("Ringing");
tone(buzzerPin,2500,1000);
delay(1000);
writeOled("Ringing.");
tone(buzzerPin,1000,2000);
delay(2000);
writeOled("Ringing..");
digitalWrite(buzzerPin,LOW);
delay(2000);
writeOled("Ringing...");
}
display.clearDisplay();
writeOled("Smart Door Lock");
}
void loop() {
int checkDist = findDistance();
if (checkDist <= 40) {
float dist = calcDist();
if (dist <= 40) {
bool check = checkGuest();
if (check) {
//send data to thingSpeak
ThingSpeak.setField(1,dist);
ThingSpeak.writeFields(ChannelNumber,writeAPIKey);
ringDoorBell();
delay(1000);
}
}
}
int A = ThingSpeak.readLongField(ChannelNumber, 2, readAPIKey);
Serial.println(A);
if (A==1) {
doorServo.write(0); // Lock position
writeOled("Hola");
}
else if (A==0) {
doorServo.write(90); // Open position
writeOled("Door Open");
}
delay(1500);
}