Advertisement
safwan092

Untitled

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