#include <Servo.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
Servo doorLock; // Servo object to control the door lock
const int buttonPin = 2; // Pin connected to the push button
const int ledPin = 13; // Pin connected to the LED
bool readyForInput = false; // Flag to check if we are ready for input
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int familyMembers[] = {1, 7, 4, 9}; // Array of recognized family member codes
int numFamilyMembers = 4; // Number of family members
const char* ssid = "wokvi-GUEST";
const char* password = "";
const char* apiKey = "AIzaSyAoxDceQvzB8OZ5u69E3GzudVwfu-ItCL0";
const char* calendarId = "7a11207b2e0d3dd81078b9cb54d88e616a67834b885d5f53e3464a2c6b3eb50a@group.calendar.google.com";
void setup() {
doorLock.attach(9); // Attach servo to pin 9
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
doorLock.write(0); // Make sure the door is initially closed
Serial.begin(9600);
Serial.println("System ready. Press button to enter a code.");
connectToWiFi();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
scheduleAppointment("Family Meeting", "2024-04-25T10:00:00", "2024-04-25T11:00:00");
delay(60000); // Wait for 1 minute before scheduling the next appointment
}
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void processCode(int code) {
bool isRecognized = false;
for (int i = 0; i < numFamilyMembers; i++) {
if (code == familyMembers[i]) {
isRecognized = true;
break;
}
}
if (isRecognized) {
Serial.print("Code ");
Serial.print(code);
Serial.println(" recognized. Opening door...");
openDoor();
delay(5000); // Keep the door open for 5 seconds
} else {
Serial.print("Code ");
Serial.print(code);
Serial.println(" NOT recognized. Access Denied.");
accessDenied();
}
closeDoor(); // Always close the door after action
}
void openDoor() {
doorLock.write(90); // Open the door
digitalWrite(ledPin, HIGH); // Turn on the LED to indicate door is open
}
void closeDoor() {
doorLock.write(0); // Close the door
digitalWrite(ledPin, LOW); // Turn off the LED
}
void accessDenied() {
for (int i = 0; i < 5; i++) { // Blink LED to indicate access denied
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
void scheduleAppointment(const char* summary, const char* startDateTime, const char* endDateTime) {
WiFiClient client;
String url = "https://www.googleapis.com/calendar/v3/calendars/" + String(calendarId) + "/events?key=" + String(apiKey);
HTTPClient http;
http.begin(client, url);
http.addHeader("Content-Type", "application/json");
// Create JSON object for the request body
StaticJsonDocument<200> doc;
doc["summary"] = summary;
doc["start"]["dateTime"] = startDateTime;
doc["end"]["dateTime"] = endDateTime;
String requestBody;
serializeJson(doc, requestBody);
int httpResponseCode = http.POST(requestBody);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Appointment scheduled:");
Serial.println(response);
} else {
Serial.print("Error scheduling appointment. HTTP error code: ");
Serial.println(httpResponseCode);
}
http.end();
}