Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <WiFi.h>
- #include <MFRC522.h>
- #include <TinyGPSPlus.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- #define BOT_TOKEN "6452183043:AAEo00Jc_fjxAjDF80sVUnOjG8zahwuiFlo"
- String chat_id1 = "985890015";
- // 19 //ESP32 <--- MISO [RFID Reader]
- // 23 //ESP32 <--- MOSI [RFID Reader]
- // 18 //ESP32 <--- SCK [RFID Reader]
- #define SS_PIN 21 //ESP32 <--- SDA [RFID Reader]
- #define RST_PIN 22 //ESP32 <--- RST [RFID Reader]
- #define buzzerPin 4 //ESP32 <--- (+) [Buzzer]
- #define data_0 34 //ESP32 <--- 10 [Arduino Uno]
- #define data_1 35 //ESP32 <--- 11 [Arduino Uno]
- #define Door_Relay 15 //ESP32 <--- S [Relay Module]
- #define IR_Sensor 33 //ESP32 <--- S [IR Sensor]
- void setup() {
- Serial.begin(115200); // Communication Between ESP32 and PC
- Serial2.begin(9600); // Communication Between ESP32 and GPS
- connect_To_WiFi_And_Telegram_Server();
- initiate_Inputs_Outputs();
- }
- void loop() {
- checkSensor();
- checkGPS();
- checkTelegram();
- checkKeyPad();
- checkRFID();
- }
- //---------------------------------- Global Variables --------------------------------------
- //******************************************************************************************
- // Card #1 = A6 1B 2F 1A
- // Blue Tag = 15 6A B4 C3
- String user1ID = "A6 1B 2F 1A";
- String user2ID = "15 6A B4 C3";
- String stringMessage = "";
- String lonGPS = "";
- String latGPS = "";
- char locationURL[70];
- int data_0_State = 0;
- int data_1_State = 0;
- const unsigned long BOT_MTBS = 1000;
- unsigned long bot_lasttime;
- bool Start = false;
- int IR_Sensor_Reading = 0;
- int stolenFlag = 0;
- TinyGPSPlus gps;
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- //******************************************************************************************
- //----------------------------------- Functions --------------------------------------------
- //******************************************************************************************
- void buzz() {
- digitalWrite(buzzerPin, 1);
- delay(100);
- digitalWrite(buzzerPin, 0);
- delay(50);
- }
- //******************************************************************************************
- //******************************************************************************************
- void checkKeyPad() {
- data_0_State = analogRead(data_0);
- data_1_State = analogRead(data_1);
- /*
- Serial.print(data_0_State);
- Serial.print(", ");
- Serial.print(data_1_State);
- Serial.print("\n");
- */
- //00
- if (data_0_State == 0 && data_1_State == 0) {
- //Serial.println("no data received from keypad");
- }
- //11
- else if (data_0_State == 4095 && data_1_State == 4095) {
- Serial.println("Password Correct !");
- buzz();
- open_door(1);
- }
- //10
- else if (data_0_State == 4095 && data_1_State > 0 && data_1_State < 4095) {
- Serial.println("Password False !!!!!");
- bot.sendMessage(chat_id1, "Password authentication failed - •Door Locked•");
- buzz();
- buzz();
- buzz();
- }
- }
- //******************************************************************************************
- //******************************************************************************************
- void checkRFID() {
- // Look for new cards
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- // Select one of the cards
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- //Show UID on serial monitor
- String content = "";
- buzz();
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.print(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println();
- content.toUpperCase();
- Serial.println(content.substring(1));
- if (content.substring(1) == user1ID) {
- Serial.println("Card authenticated");
- open_door(2);
- }
- else {
- Serial.println("Card authentication failed");
- bot.sendMessage(chat_id1, "Card authentication failed - •Door Locked•");
- buzz();
- buzz();
- buzz();
- }
- /*
- if (content.substring(1) == user2ID) {
- Serial.println("Card authenticated");
- }
- */
- }
- //******************************************************************************************
- //******************************************************************************************
- void open_door(int how) {
- //open by Correct Password [how = 1]
- if (how == 1) {
- bot.sendMessage(chat_id1, "Correct Password - [Door Opened]");
- }
- //open by Correct RFID [how = 2]
- else if (how == 2) {
- bot.sendMessage(chat_id1, "Correct RFID - [Door Opened]");
- }
- //open by Telegram Command [how = 3]
- else if (how == 3) {
- bot.sendMessage(chat_id1, "Telegram Command - [Door Opened]");
- }
- digitalWrite(Door_Relay, HIGH);
- delay(5000);
- digitalWrite(Door_Relay, LOW);
- }
- //******************************************************************************************
- //******************************************************************************************
- void handleNewMessages(int numNewMessages)
- {
- for (int i = 0; i < numNewMessages; i++)
- {
- String chat_id = bot.messages[i].chat_id;
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "")
- from_name = "Guest";
- if (text == "/open")
- {
- buzz();
- open_door(3);
- }
- if (text == "/gps")
- {
- // Send GPS Location to Telegram
- // locationURL
- String GPSmessage = "Current Location is:\n";
- GPSmessage += String(locationURL);
- bot.sendMessage(chat_id1, GPSmessage);
- }
- if (text == "/reset")
- {
- stolenFlag = 0;
- bot.sendMessage(chat_id1, "Safe is [OK]");
- }
- }
- }
- //******************************************************************************************
- //******************************************************************************************
- void connect_To_WiFi_And_Telegram_Server() {
- // attempt to connect to Wifi network:
- Serial.print("Connecting to Wifi SSID ");
- Serial.print(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.print("\nWiFi connected. IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- }
- //******************************************************************************************
- //******************************************************************************************
- void checkTelegram() {
- if (millis() - bot_lasttime > BOT_MTBS)
- {
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while (numNewMessages)
- {
- Serial.println("got response");
- handleNewMessages(numNewMessages);
- numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- }
- bot_lasttime = millis();
- }
- }
- //******************************************************************************************
- //******************************************************************************************
- void initiate_Inputs_Outputs() {
- pinMode(IR_Sensor, INPUT);
- pinMode(data_0, INPUT);
- pinMode(data_1, INPUT);
- pinMode(Door_Relay, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- digitalWrite(buzzerPin, LOW);
- digitalWrite(Door_Relay, LOW);
- SPI.begin();
- mfrc522.PCD_Init();
- }
- //******************************************************************************************
- //******************************************************************************************
- void checkGPS() {
- while (Serial2.available() > 0)
- if (gps.encode(Serial2.read())) {
- Serial.print(F("Location: "));
- if (gps.location.isValid()) {
- lonGPS = String(gps.location.lng(), 6);
- latGPS = String(gps.location.lat(), 6);
- Serial.print("Lat: ");
- Serial.print(latGPS);
- Serial.print(F(","));
- Serial.print("Lng: ");
- Serial.print(lonGPS);
- Serial.println();
- stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
- stringMessage.toCharArray(locationURL, 70);
- }
- else {
- Serial.print(F("INVALID"));
- stringMessage = "http://www.google.com/maps/place/21.500462,39.245271";
- stringMessage.toCharArray(locationURL, 70);
- }
- }
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- }
- //******************************************************************************************
- //******************************************************************************************
- void checkSensor() {
- IR_Sensor_Reading = digitalRead(IR_Sensor);//0 = Safe is ON Desk || 1 = Safe is OFF the Desk
- Serial.println(IR_Sensor_Reading);
- if (IR_Sensor_Reading == 1 && stolenFlag == 0) {
- stolenFlag = 1;
- bot.sendMessage(chat_id1, "Safe Alert [Tampering Detected]");
- }
- if (stolenFlag) {
- //Activate Shock Relay
- for (int w = 0; w < 10; w++) {
- buzz();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement