#include <WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <ESP32Servo.h>
#include <FirebaseESP32.h>
// #include <addons/RTDBHelper.h>

#define btnPin 4
const int servoPin = 18;
#define buzz 16
int Door1;
Servo servo;


int PWM_FREQUENCY = 5000;
int PWM_CHANNEL_2 = 2;
/* 1. Define the WiFi credentials */
// #define WIFI_SSID "Wokwi-GUEST"
// #define WIFI_PASSWORD ""

// /* 2. If work with RTDB, define the RTDB URL and database secret */
// #define DATABASE_URL "smartdoorlock-aec8c-default-rtdb.asia-southeast1.firebasedatabase.app" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
// #define DATABASE_SECRET "VNqftOizmFgBn0bQNA10ntV45nb9wNgOo2sIDeZO"

// /* 3. Define the Firebase Data object */
// FirebaseData fbdo;

// /* 4, Define the FirebaseAuth data for authentication data */
// FirebaseAuth auth;

// /* Define the FirebaseConfig data for config data */
// FirebaseConfig config;

// unsigned long dataMillis = 0;
// int count = 0;

// #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
// WiFiMulti multi;
// #endif

void setup() {
//   Serial.begin(115200);

// #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
//     multi.addAP(WIFI_SSID, WIFI_PASSWORD);
//     multi.run();
// #else
//     WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// #endif

//     Serial.print("Connecting to Wi-Fi");
//     unsigned long ms = millis();
//     while (WiFi.status() != WL_CONNECTED)
//     {
//         Serial.print(".");
//         delay(300);
// #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
//         if (millis() - ms > 10000)
//             break;
// #endif
    // }
//     Serial.println();
//     Serial.print("Connected with IP: ");
//     Serial.println(WiFi.localIP());
//     Serial.println();

//     Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

//     /* Assign the certificate file (optional) */
//     // config.cert.file = "/cert.cer";
//     // config.cert.file_storage = StorageType::FLASH;

//     /* Assign the database URL and database secret(required) */
//     config.database_url = DATABASE_URL;
//     config.signer.tokens.legacy_token = DATABASE_SECRET;

//     // Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
//     Firebase.reconnectNetwork(true);

//     // Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
//     // Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred.
//     fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);

//     // The WiFi credentials are required for Pico W
//     // due to it does not have reconnect feature.
// #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
//     config.wifi.clearAP();
//     config.wifi.addAP(WIFI_SSID, WIFI_PASSWORD);
// #endif

//     /* Initialize the library with the Firebase authen and config */
//     Firebase.begin(&config, &auth);

  servo.attach(servoPin, 500, 2400);
  pinMode(btnPin, INPUT);
  ledcAttachPin(buzz, PWM_CHANNEL_2); 

  ledcWriteNote(PWM_CHANNEL_2, NOTE_E, 7);
  delay(300);
  ledcDetachPin(buzz);

 }
 
void loop() {
  // if (Firebase.getInt(fbdo, "/Hotel/Room1/Door1"))
  // {
  //   if(fbdo.dataType() =="int"){
  //     // Serial.print("Door1 : "); Serial.println(((fbdo.intData() == 0)? "Unlock" : "Lock"));
  //     Door1 = fbdo.intData();
  //   }
  // } 
  // else {
  //   Serial.println(fbdo.errorReason());
  // }

  //   // digitalWrite(servoPin, Door1); // Unlock
  //   Serial.print(Door1);

int btn = digitalRead(btnPin);
  if(btn == 1){
    // Firebase.setInt(fbdo, "/Hotel/Room1/Door1", 1);
    Serial.println("Unlocked");
    servo.write(180);
    ledcAttachPin(buzz, PWM_CHANNEL_2); 
    ledcWriteNote(PWM_CHANNEL_2, NOTE_E, 7);
    delay(110);
    ledcDetachPin(buzz);
    delay(4890);

  }
  
    servo.write(0);
}