Advertisement
safwan092

Untitled

Nov 5th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <WiFi.h>
  3. #include <MFRC522.h>
  4. #include <FirebaseESP32.h>
  5. #include <addons/TokenHelper.h>
  6. #include <addons/RTDBHelper.h>
  7.  
  8. #define arduino_pin 22 // ESP32 pin GPIO22
  9. #define button_pin 34 // ESP32 pin GPIO34
  10. #define buzzer_pin 21 // ESP32 pin GPIO19
  11. #define SS_pin 5 // ESP32 pin GPIO5
  12. #define RST_pin 27 // ESP32 pin GPIO27
  13.  
  14. #define WIFI_SSID "network"
  15. #define WIFI_PASSWORD "123456789"
  16. #define API_KEY "AIzaSyAjIS98IBo4xU9DAqFbo6ZJLPqBwzrJvzk"
  17. #define USER_EMAIL "smartpostcar@gmail.com"
  18. #define USER_PASSWORD "123456789"
  19. #define DATABASE_URL "postcar-57fe1-default-rtdb.firebaseio.com"
  20. #define DATABASE_SECRET "7yJzXNiqlkQZUFb3Ul2KJACDHnztTMrDoFLgPOZM"
  21.  
  22. MFRC522 rfid(SS_pin, RST_pin);
  23.  
  24. FirebaseData fbdo;
  25. FirebaseAuth auth;
  26. FirebaseConfig config;
  27. unsigned long dataMillis = 0;
  28.  
  29. int button_pressed_flag = 0;
  30. int button_state = 0;
  31. int home1_value_from_DB = 0;
  32. int home2_value_from_DB = 0;
  33. int home3_value_from_DB = 0;
  34.  
  35. String home_1_RFID_Tag_UUID = "53 6F 36 D1 00 00 01";
  36. String home_2_RFID_Tag_UUID = "53 78 36 D1 00 00 01";
  37. String home_3_RFID_Tag_UUID = "53 79 36 D1 00 00 01";
  38. String Post_Office_RFID_Tag_UUID = "53 6E 36 D1 00 00 01";
  39.  
  40. int home_1_RFID_State = 0;
  41. int home_2_RFID_State = 0;
  42. int home_3_RFID_State = 0;
  43. int PO_RFID_State = 0;
  44.  
  45. void setup()
  46. {
  47. Serial.begin(115200);
  48. setup_rfid_reader();
  49. setup_inputs_and_outputs();
  50. connect_to_WiFi_and_Firebase_database();
  51. }
  52.  
  53. void loop()
  54. {
  55. //read_from_database();
  56. read_RFID_tag();
  57. }
  58.  
  59.  
  60. void read_button_and_send_data_to_database(int i) {
  61. button_state = digitalRead(button_pin);
  62. if (button_state == 1 && button_pressed_flag == 0) {
  63. digitalWrite(buzzer_pin, 1);
  64. button_pressed_flag = 1;
  65. write_to_database(i);
  66. button_pressed_flag = 0;
  67. digitalWrite(buzzer_pin, 0);
  68. digitalWrite(arduino_pin, LOW);
  69. }
  70. }
  71.  
  72. void read_RFID_tag() {
  73. // Look for new cards
  74. if ( ! rfid.PICC_IsNewCardPresent())
  75. {
  76. return;
  77. }
  78. // Select one of the cards
  79. if ( ! rfid.PICC_ReadCardSerial())
  80. {
  81. return;
  82. }
  83. //Show UID on serial monitor
  84. String content = "";
  85. // buzz();
  86. for (byte i = 0; i < rfid.uid.size; i++)
  87. {
  88. Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
  89. Serial.print(rfid.uid.uidByte[i], HEX);
  90. content.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
  91. content.concat(String(rfid.uid.uidByte[i], HEX));
  92. }
  93. Serial.println();
  94. content.toUpperCase();
  95. Serial.println(content.substring(1));
  96. rfid.PICC_HaltA(); // halt PICC
  97. rfid.PCD_StopCrypto1(); // stop encryption on PCD
  98. read_from_database();
  99. if (content.substring(1) == home_1_RFID_Tag_UUID) {
  100. Serial.println("[Home 1] Card authenticated");
  101. if (home1_value_from_DB == 1 && home_1_RFID_State == 0) {
  102. home_1_RFID_State = 1;
  103. //Stop Car Command untill button is pressed
  104. while (home_1_RFID_State) {
  105. //Stop Car Command Here
  106. digitalWrite(arduino_pin, HIGH); //High signal on pin 22 of the ESP32 to Stop CAR
  107. //buzzer ON
  108. digitalWrite(buzzer_pin, 1);
  109. read_button_and_send_data_to_database(1);// reset [Home 1] Database
  110. }
  111. }
  112. delay(5000);
  113. }
  114. else if (content.substring(1) == home_2_RFID_Tag_UUID) {
  115. Serial.println("[Home 2] Card authenticated");
  116. if (home2_value_from_DB == 1 && home_2_RFID_State == 0) {
  117. home_2_RFID_State = 1;
  118. //Stop Car Command untill button is pressed
  119. while (home_2_RFID_State) {
  120. //Stop Car Command Here
  121. digitalWrite(arduino_pin, HIGH); //High signal on pin 22 of the ESP32 to Stop CAR
  122. //buzzer ON
  123. digitalWrite(buzzer_pin, 1);
  124. read_button_and_send_data_to_database(2);// reset [Home 2] Database
  125. }
  126. }
  127. delay(5000);
  128. }
  129. else if (content.substring(1) == home_3_RFID_Tag_UUID) {
  130. Serial.println("[Home 3] Card authenticated");
  131. Serial.println(home3_value_from_DB);
  132. if (home3_value_from_DB == 1 && home_3_RFID_State == 0) {
  133. home_3_RFID_State = 1;
  134. }
  135. Serial.println(home_3_RFID_State);
  136. //Stop Car Command untill button is pressed
  137. while (home_3_RFID_State) {
  138. Serial.println("While loop [Home 3]");
  139. //Stop Car Command Here
  140. digitalWrite(arduino_pin, HIGH); //High signal on pin 22 of the ESP32 to Stop CAR
  141. //buzzer ON
  142. digitalWrite(buzzer_pin, 1);
  143. read_button_and_send_data_to_database(3);// reset [Home 3] Database
  144. }
  145.  
  146. delay(5000);
  147. }
  148.  
  149. else if (content.substring(1) == Post_Office_RFID_Tag_UUID) {
  150. Serial.println("[P.O.] Card authenticated");
  151. if (PO_RFID_State == 0) {
  152. PO_RFID_State = 1;
  153. //Stop Car Command untill someone from the database is requesting POST
  154. while (PO_RFID_State) {
  155. Serial.println("While Loop");
  156. //Stop Car Command Here
  157. digitalWrite(arduino_pin, HIGH); //High signal on pin 22 of the ESP32 to Stop CAR
  158. //read Data From Data Base
  159. read_from_database();
  160. //if someone has requested POST move the car from Post Office
  161. if (home1_value_from_DB == 1 || home2_value_from_DB == 1 || home3_value_from_DB == 1) {
  162. PO_RFID_State = 0;
  163. }
  164. }
  165. }
  166. delay(5000);
  167. }
  168. }
  169.  
  170.  
  171.  
  172. void setup_rfid_reader() {
  173. SPI.begin(); // init SPI bus
  174. rfid.PCD_Init(); // init MFRC522
  175. }
  176. void setup_inputs_and_outputs() {
  177. pinMode(button_pin, INPUT);
  178. pinMode(buzzer_pin, OUTPUT);
  179. pinMode(arduino_pin, OUTPUT);
  180. digitalWrite(buzzer_pin, LOW);
  181. digitalWrite(arduino_pin, LOW);
  182. }
  183.  
  184. void connect_to_WiFi_and_Firebase_database() {
  185. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  186. Serial.print("Connecting to Wi-Fi");
  187. while (WiFi.status() != WL_CONNECTED)
  188. {
  189. Serial.print(".");
  190. delay(300);
  191. }
  192. Serial.println();
  193. Serial.print("Connected with IP: ");
  194. Serial.println(WiFi.localIP());
  195. Serial.println();
  196. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  197. config.api_key = API_KEY;
  198. auth.user.email = USER_EMAIL;
  199. auth.user.password = USER_PASSWORD;
  200. config.database_url = DATABASE_URL;
  201. Firebase.reconnectWiFi(true);
  202. fbdo.setResponseSize(4096);
  203. String base_path = "/UsersData/";
  204. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  205. config.max_token_generation_retry = 5;
  206. Firebase.begin(&config, &auth);
  207. String var = "$userId";
  208. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  209. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  210. }
  211.  
  212. void read_from_database() {
  213.  
  214. if (millis() - dataMillis > 500 && Firebase.ready()) {
  215. dataMillis = millis();
  216. //////////////////////////////////////////////////////////
  217. //home_1
  218. if (Firebase.RTDB.getInt(&fbdo, "/home1")) {
  219. if (fbdo.dataType() == "int") {
  220. home1_value_from_DB = fbdo.intData();
  221. Serial.print("home1_value_from_DB = ");
  222. Serial.println(home1_value_from_DB);
  223. }
  224. }
  225. else {
  226. Serial.println(fbdo.errorReason());
  227. }
  228. //////////////////////////////////////////////////////////
  229. //////////////////////////////////////////////////////////
  230. //home_2
  231. if (Firebase.RTDB.getInt(&fbdo, "/home2")) {
  232. if (fbdo.dataType() == "int") {
  233. home2_value_from_DB = fbdo.intData();
  234. Serial.print("home2_value_from_DB = ");
  235. Serial.println(home2_value_from_DB);
  236. }
  237. }
  238. else {
  239. Serial.println(fbdo.errorReason());
  240. }
  241. //////////////////////////////////////////////////////////
  242. //////////////////////////////////////////////////////////
  243. //home_3
  244. if (Firebase.RTDB.getInt(&fbdo, "/home3")) {
  245. if (fbdo.dataType() == "int") {
  246. home3_value_from_DB = fbdo.intData();
  247. Serial.print("home3_value_from_DB = ");
  248. Serial.println(home3_value_from_DB);
  249. }
  250. }
  251. else {
  252. Serial.println(fbdo.errorReason());
  253. }
  254. //////////////////////////////////////////////////////////
  255. }
  256.  
  257.  
  258. }
  259.  
  260. void write_to_database(int j) {
  261. if (j == 1 && home_1_RFID_State == 1) {
  262. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home1", 0) ? "ok" : fbdo.errorReason().c_str());
  263. home_1_RFID_State = 0;
  264. }
  265. else if (j == 2 && home_2_RFID_State == 1) {
  266. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home2", 0) ? "ok" : fbdo.errorReason().c_str());
  267. home_2_RFID_State = 0;
  268. }
  269. else if (j == 3 && home_3_RFID_State == 1) {
  270. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home3", 0) ? "ok" : fbdo.errorReason().c_str());
  271. home_3_RFID_State = 0;
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement