Advertisement
safwan092

Untitled

Dec 29th, 2023
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  2. #include "DHT.h" // https://github.com/adafruit/DHT-sensor-library/
  3. #include <WiFi.h>
  4. #include <FirebaseESP32.h>
  5. #include <addons/TokenHelper.h>
  6. #include <addons/RTDBHelper.h>
  7.  
  8. #define LOCK_Relay_PIN 5
  9. #define DHT11_SENSOR_PIN 18
  10. #define DOOR_SENSOR_PIN 19
  11. #define PACKAGE_SENSOR_PIN 34
  12.  
  13.  
  14. #define WIFI_SSID "WIFI_AP"
  15. #define WIFI_PASSWORD "WIFI_PASSWORD"
  16. #define API_KEY "API_KEY"
  17. #define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
  18. #define USER_EMAIL "USER_EMAIL"
  19. #define USER_PASSWORD "USER_PASSWORD"
  20.  
  21.  
  22. FirebaseData fbdo;
  23. FirebaseAuth auth;
  24. FirebaseConfig config;
  25. unsigned long sendDataPrevMillis = 0;
  26. unsigned long getDataPrevMillis = 0;
  27. String password_To_Open_the_door;
  28. int doorState;
  29. int packageState;
  30. float h;
  31. float t;
  32.  
  33. int package_threshold = 2800;
  34.  
  35. DHT dht(DHT11_SENSOR_PIN, DHT11);
  36.  
  37. void setup() {
  38. Serial.begin(9600);
  39. Connect_To_WiFi();
  40. Setup_Sensors_and_Inputs_and_Outputs();
  41. Connect_To_Firebase();
  42.  
  43. }
  44.  
  45. void loop() {
  46. ReConnect_To_WiFi();
  47. Read_Password_from_Database(1000);
  48.  
  49. //digitalWrite(LOCK_Relay_PIN, HIGH);
  50. //Read_DHT11_Sensor();
  51. //Read_Package_Sensor();
  52. //Read_Door_Sensor();
  53. }
  54.  
  55. void Setup_Sensors_and_Inputs_and_Outputs() {
  56. dht.begin();
  57. pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
  58. pinMode(PACKAGE_SENSOR_PIN, INPUT);
  59. pinMode(LOCK_Relay_PIN, OUTPUT);
  60. digitalWrite(LOCK_Relay_PIN, LOW);
  61. }
  62.  
  63.  
  64. void ReConnect_To_WiFi() {
  65. while (WiFi.status() != WL_CONNECTED) {
  66. //delay(500);
  67. Serial.print(".");
  68. Connect_To_WiFi();
  69. }
  70. }
  71.  
  72. void Connect_To_WiFi() {
  73. WiFiManager wm;
  74. bool res;
  75. res = wm.autoConnect("Smart P.O. Box [Setup WiFi]"); // password protected ap
  76.  
  77. if (!res) {
  78. Serial.println("Failed to Connect!!");
  79. // ESP.restart();
  80. }
  81. else {
  82. //if you get here you have connected to the WiFi
  83. Serial.println("Connected...");
  84. }
  85. }
  86.  
  87. void Read_Door_Sensor() {
  88. doorState = digitalRead(DOOR_SENSOR_PIN);
  89. if (doorState == HIGH) {
  90. Serial.println("The door is open");
  91. }
  92. else if (doorState == LOW) {
  93. Serial.println("The door is closed");
  94. }
  95. }
  96.  
  97. void Read_Package_Sensor() {
  98. packageState = analogRead(PACKAGE_SENSOR_PIN);
  99. //Serial.println(packageState);
  100. if (packageState < package_threshold) {
  101. Serial.println("Package Inside");
  102. }
  103. else if (packageState > package_threshold) {
  104. Serial.println("P.O. Box is EMPTY");
  105. }
  106. }
  107.  
  108. void Read_DHT11_Sensor() {
  109. h = dht.readHumidity();
  110. t = dht.readTemperature();
  111. Serial.print(F("Humidity: "));
  112. Serial.print(h);
  113. Serial.print(F("% Temperature: "));
  114. Serial.print(t);
  115. Serial.println(F("°C "));
  116. }
  117.  
  118. void Read_Password_from_Database(unsigned long timeE) {
  119. if (Firebase.ready() && (millis() - getDataPrevMillis > timeE || getDataPrevMillis == 0))
  120. {
  121. getDataPrevMillis = millis();
  122. password_To_Open_the_door = Firebase.getString(fbdo, F("/password"));
  123. Serial.print("Password is Set to: ");
  124. Serial.println(password_To_Open_the_door);
  125. }
  126. }
  127.  
  128. void Connect_To_Firebase() {
  129. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  130. config.api_key = API_KEY;
  131. auth.user.email = USER_EMAIL;
  132. auth.user.password = USER_PASSWORD;
  133. config.database_url = DATABASE_URL;
  134. config.token_status_callback = tokenStatusCallback;
  135. Firebase.begin(&config, &auth);
  136. Firebase.reconnectWiFi(true);
  137. Firebase.setDoubleDigits(5);
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement