Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
- #include "DHT.h" // https://github.com/adafruit/DHT-sensor-library/
- #include <WiFi.h>
- #include <FirebaseESP32.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- #define LOCK_Relay_PIN 5
- #define DHT11_SENSOR_PIN 18
- #define DOOR_SENSOR_PIN 19
- #define PACKAGE_SENSOR_PIN 34
- #define WIFI_SSID "WIFI_AP"
- #define WIFI_PASSWORD "WIFI_PASSWORD"
- #define API_KEY "API_KEY"
- #define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
- #define USER_EMAIL "USER_EMAIL"
- #define USER_PASSWORD "USER_PASSWORD"
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- unsigned long sendDataPrevMillis = 0;
- unsigned long getDataPrevMillis = 0;
- String password_To_Open_the_door;
- int doorState;
- int packageState;
- float h;
- float t;
- int package_threshold = 2800;
- DHT dht(DHT11_SENSOR_PIN, DHT11);
- void setup() {
- Serial.begin(9600);
- Connect_To_WiFi();
- Setup_Sensors_and_Inputs_and_Outputs();
- Connect_To_Firebase();
- }
- void loop() {
- ReConnect_To_WiFi();
- Read_Password_from_Database(1000);
- //digitalWrite(LOCK_Relay_PIN, HIGH);
- //Read_DHT11_Sensor();
- //Read_Package_Sensor();
- //Read_Door_Sensor();
- }
- void Setup_Sensors_and_Inputs_and_Outputs() {
- dht.begin();
- pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
- pinMode(PACKAGE_SENSOR_PIN, INPUT);
- pinMode(LOCK_Relay_PIN, OUTPUT);
- digitalWrite(LOCK_Relay_PIN, LOW);
- }
- void ReConnect_To_WiFi() {
- while (WiFi.status() != WL_CONNECTED) {
- //delay(500);
- Serial.print(".");
- Connect_To_WiFi();
- }
- }
- void Connect_To_WiFi() {
- WiFiManager wm;
- bool res;
- res = wm.autoConnect("Smart P.O. Box [Setup WiFi]"); // password protected ap
- if (!res) {
- Serial.println("Failed to Connect!!");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("Connected...");
- }
- }
- void Read_Door_Sensor() {
- doorState = digitalRead(DOOR_SENSOR_PIN);
- if (doorState == HIGH) {
- Serial.println("The door is open");
- }
- else if (doorState == LOW) {
- Serial.println("The door is closed");
- }
- }
- void Read_Package_Sensor() {
- packageState = analogRead(PACKAGE_SENSOR_PIN);
- //Serial.println(packageState);
- if (packageState < package_threshold) {
- Serial.println("Package Inside");
- }
- else if (packageState > package_threshold) {
- Serial.println("P.O. Box is EMPTY");
- }
- }
- void Read_DHT11_Sensor() {
- h = dht.readHumidity();
- t = dht.readTemperature();
- Serial.print(F("Humidity: "));
- Serial.print(h);
- Serial.print(F("% Temperature: "));
- Serial.print(t);
- Serial.println(F("°C "));
- }
- void Read_Password_from_Database(unsigned long timeE) {
- if (Firebase.ready() && (millis() - getDataPrevMillis > timeE || getDataPrevMillis == 0))
- {
- getDataPrevMillis = millis();
- password_To_Open_the_door = Firebase.getString(fbdo, F("/password"));
- Serial.print("Password is Set to: ");
- Serial.println(password_To_Open_the_door);
- }
- }
- void Connect_To_Firebase() {
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- config.api_key = API_KEY;
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- config.database_url = DATABASE_URL;
- config.token_status_callback = tokenStatusCallback;
- Firebase.begin(&config, &auth);
- Firebase.reconnectWiFi(true);
- Firebase.setDoubleDigits(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement