Advertisement
safwan092

Untitled

Aug 3rd, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <WiFi.h>
  3. #include <MFRC522.h>
  4. #include <TinyGPSPlus.h>
  5. #include <WiFiClientSecure.h>
  6. #include <UniversalTelegramBot.h>
  7.  
  8. #define WIFI_SSID "MB18"
  9. #define WIFI_PASSWORD "123456789"
  10. #define BOT_TOKEN "6434355819:AAGSd0jHyHGDBKoXwK-vFFlL0Fq8arZN8yc"
  11. String chat_id1 = "-717869404";
  12.  
  13. // 19 //ESP32 <--- MISO [RFID Reader]
  14. // 23 //ESP32 <--- MOSI [RFID Reader]
  15. // 18 //ESP32 <--- SCK [RFID Reader]
  16. #define SS_PIN 21 //ESP32 <--- SDA [RFID Reader]
  17. #define RST_PIN 22 //ESP32 <--- RST [RFID Reader]
  18. #define buzzerPin 4 //ESP32 <--- (+) [Buzzer]
  19. #define data_0 34 //ESP32 <--- 10 [Arduino Uno]
  20. #define data_1 35 //ESP32 <--- 11 [Arduino Uno]
  21. #define Door_Relay 15 //ESP32 <--- S [Relay Module]
  22. #define IR_Sensor 33 //ESP32 <--- S [IR Sensor]
  23. #define Shock_Relay 32 //ESP32 <--- S [Relay Module]
  24.  
  25.  
  26. void setup() {
  27. Serial.begin(115200); // Communication Between ESP32 and PC
  28. Serial2.begin(9600); // Communication Between ESP32 and GPS
  29. connect_To_WiFi_And_Telegram_Server();
  30. initiate_Inputs_Outputs();
  31. }
  32.  
  33. void loop() {
  34. checkSensor();
  35. checkGPS();
  36. checkTelegram();
  37. checkKeyPad();
  38. checkRFID();
  39. }
  40.  
  41.  
  42.  
  43.  
  44. //---------------------------------- Global Variables --------------------------------------
  45.  
  46. //******************************************************************************************
  47. // Card #1 = A6 1B 2F 1A
  48. // Blue Tag = 15 6A B4 C3
  49.  
  50. String user1ID = "A6 1B 2F 1A";
  51. String user2ID = "15 6A B4 C3";
  52. String stringMessage = "";
  53. String lonGPS = "";
  54. String latGPS = "";
  55. char locationURL[70];
  56. int data_0_State = 0;
  57. int data_1_State = 0;
  58. const unsigned long BOT_MTBS = 1000;
  59. unsigned long bot_lasttime;
  60. bool Start = false;
  61. int IR_Sensor_Reading = 0;
  62. int stolenFlag = 0;
  63.  
  64. TinyGPSPlus gps;
  65. MFRC522 mfrc522(SS_PIN, RST_PIN);
  66. WiFiClientSecure secured_client;
  67. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  68.  
  69. //******************************************************************************************
  70.  
  71.  
  72. //----------------------------------- Functions --------------------------------------------
  73.  
  74. //******************************************************************************************
  75. void buzz() {
  76. digitalWrite(buzzerPin, 1);
  77. delay(100);
  78. digitalWrite(buzzerPin, 0);
  79. delay(50);
  80. }
  81. //******************************************************************************************
  82.  
  83.  
  84. //******************************************************************************************
  85. void checkKeyPad() {
  86. data_0_State = analogRead(data_0);
  87. data_1_State = analogRead(data_1);
  88. /*
  89. Serial.print(data_0_State);
  90. Serial.print(", ");
  91. Serial.print(data_1_State);
  92. Serial.print("\n");
  93. */
  94. //00
  95. if (data_0_State == 0 && data_1_State == 0) {
  96. //Serial.println("no data received from keypad");
  97. }
  98. //11
  99. else if (data_0_State == 4095 && data_1_State == 4095) {
  100. Serial.println("Password Correct !");
  101. buzz();
  102. open_door(1);
  103. }
  104. //10
  105. else if (data_0_State == 4095 && data_1_State > 0 && data_1_State < 4095) {
  106. Serial.println("Password False !!!!!");
  107. bot.sendMessage(chat_id1, "Password authentication failed - •Door Locked•");
  108. buzz();
  109. buzz();
  110. buzz();
  111. }
  112. }
  113. //******************************************************************************************
  114.  
  115.  
  116. //******************************************************************************************
  117. void checkRFID() {
  118. // Look for new cards
  119. if ( ! mfrc522.PICC_IsNewCardPresent())
  120. {
  121. return;
  122. }
  123. // Select one of the cards
  124. if ( ! mfrc522.PICC_ReadCardSerial())
  125. {
  126. return;
  127. }
  128. //Show UID on serial monitor
  129. String content = "";
  130. buzz();
  131. for (byte i = 0; i < mfrc522.uid.size; i++)
  132. {
  133. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  134. Serial.print(mfrc522.uid.uidByte[i], HEX);
  135. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  136. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  137. }
  138. Serial.println();
  139. content.toUpperCase();
  140. Serial.println(content.substring(1));
  141. if (content.substring(1) == user1ID) {
  142. Serial.println("Card authenticated");
  143. open_door(2);
  144. }
  145. else {
  146. Serial.println("Card authentication failed");
  147. bot.sendMessage(chat_id1, "Card authentication failed - •Door Locked•");
  148. buzz();
  149. buzz();
  150. buzz();
  151. }
  152. /*
  153. if (content.substring(1) == user2ID) {
  154. Serial.println("Card authenticated");
  155. }
  156. */
  157. }
  158. //******************************************************************************************
  159.  
  160.  
  161. //******************************************************************************************
  162. void open_door(int how) {
  163. //open by Correct Password [how = 1]
  164. if (how == 1) {
  165. bot.sendMessage(chat_id1, "Correct Password - [Door Opened]");
  166. }
  167. //open by Correct RFID [how = 2]
  168. else if (how == 2) {
  169. bot.sendMessage(chat_id1, "Correct RFID - [Door Opened]");
  170. }
  171. //open by Telegram Command [how = 3]
  172. else if (how == 3) {
  173. bot.sendMessage(chat_id1, "Telegram Command - [Door Opened]");
  174. }
  175. digitalWrite(Door_Relay, HIGH);
  176. delay(5000);
  177. digitalWrite(Door_Relay, LOW);
  178. }
  179. //******************************************************************************************
  180.  
  181.  
  182. //******************************************************************************************
  183. void handleNewMessages(int numNewMessages)
  184. {
  185. for (int i = 0; i < numNewMessages; i++)
  186. {
  187. String chat_id = bot.messages[i].chat_id;
  188. String text = bot.messages[i].text;
  189. String from_name = bot.messages[i].from_name;
  190. if (from_name == "")
  191. from_name = "Guest";
  192. if (text == "/open")
  193. {
  194. buzz();
  195. open_door(3);
  196. }
  197. if (text == "/gps")
  198. {
  199. // Send GPS Location to Telegram
  200. // locationURL
  201. String GPSmessage = "Current Location is:\n";
  202. GPSmessage += String(locationURL);
  203. bot.sendMessage(chat_id1, GPSmessage);
  204. }
  205. if (text == "/reset")
  206. {
  207. stolenFlag = 0;
  208. digitalWrite(Shock_Relay, LOW);
  209. bot.sendMessage(chat_id1, "Safe is [OK]");
  210. }
  211. }
  212. }
  213. //******************************************************************************************
  214.  
  215.  
  216. //******************************************************************************************
  217. void connect_To_WiFi_And_Telegram_Server() {
  218. // attempt to connect to Wifi network:
  219. Serial.print("Connecting to Wifi SSID ");
  220. Serial.print(WIFI_SSID);
  221. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  222. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  223. while (WiFi.status() != WL_CONNECTED)
  224. {
  225. Serial.print(".");
  226. delay(500);
  227. }
  228. Serial.print("\nWiFi connected. IP address: ");
  229. Serial.println(WiFi.localIP());
  230.  
  231. Serial.print("Retrieving time: ");
  232. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  233. time_t now = time(nullptr);
  234. while (now < 24 * 3600)
  235. {
  236. Serial.print(".");
  237. delay(100);
  238. now = time(nullptr);
  239. }
  240. Serial.println(now);
  241. }
  242. //******************************************************************************************
  243.  
  244.  
  245. //******************************************************************************************
  246. void checkTelegram() {
  247. if (millis() - bot_lasttime > BOT_MTBS)
  248. {
  249. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  250.  
  251. while (numNewMessages)
  252. {
  253. Serial.println("got response");
  254. handleNewMessages(numNewMessages);
  255. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  256. }
  257.  
  258. bot_lasttime = millis();
  259. }
  260. }
  261. //******************************************************************************************
  262.  
  263.  
  264. //******************************************************************************************
  265. void initiate_Inputs_Outputs() {
  266. pinMode(IR_Sensor, INPUT);
  267. pinMode(data_0, INPUT);
  268. pinMode(data_1, INPUT);
  269. pinMode(Door_Relay, OUTPUT);
  270. pinMode(Shock_Relay,OUTPUT);
  271. pinMode(buzzerPin, OUTPUT);
  272. digitalWrite(buzzerPin, LOW);
  273. digitalWrite(Door_Relay, LOW);
  274. digitalWrite(Shock_Relay, LOW);
  275. SPI.begin();
  276. mfrc522.PCD_Init();
  277. bot.sendMessage(chat_id1, "Vault status - [ONLINE]");
  278. }
  279. //******************************************************************************************
  280.  
  281.  
  282. //******************************************************************************************
  283. void checkGPS() {
  284. while (Serial2.available() > 0)
  285. if (gps.encode(Serial2.read())) {
  286. Serial.print(F("Location: "));
  287. if (gps.location.isValid()) {
  288. lonGPS = String(gps.location.lng(), 6);
  289. latGPS = String(gps.location.lat(), 6);
  290. Serial.print("Lat: ");
  291. Serial.print(latGPS);
  292. Serial.print(F(","));
  293. Serial.print("Lng: ");
  294. Serial.print(lonGPS);
  295. Serial.println();
  296. stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
  297. stringMessage.toCharArray(locationURL, 70);
  298. }
  299. else {
  300. Serial.print(F("INVALID"));
  301. stringMessage = "http://www.google.com/maps/place/21.913985,39.262653";
  302. stringMessage.toCharArray(locationURL, 70);
  303. }
  304. }
  305. if (millis() > 5000 && gps.charsProcessed() < 10) {
  306. Serial.println(F("No GPS detected: check wiring."));
  307. while (true);
  308. }
  309. }
  310. //******************************************************************************************
  311.  
  312.  
  313. //******************************************************************************************
  314. void checkSensor() {
  315. IR_Sensor_Reading = digitalRead(IR_Sensor);//0 = Safe is ON Desk || 1 = Safe is OFF the Desk
  316. Serial.println(IR_Sensor_Reading);
  317. if (IR_Sensor_Reading == 1 && stolenFlag == 0) {
  318. stolenFlag = 1;
  319. digitalWrite(Shock_Relay, HIGH);
  320. bot.sendMessage(chat_id1, "Safe Alert [Tampering Detected]");
  321. }
  322. if (stolenFlag) {
  323. //Activate Shock Relay
  324. for (int w = 0; w < 10; w++) {
  325. buzz();
  326. }
  327. }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement