Advertisement
safwan092

Untitled

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