Advertisement
safwan092

Untitled

Mar 4th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. #include <WiFiManager.h>
  2. #include <Arduino.h>
  3. #include <FirebaseESP32.h>
  4. #include <addons/TokenHelper.h>
  5. #include <addons/RTDBHelper.h>
  6.  
  7. ///////////////firbase_setup//////////////////////////////////////////
  8.  
  9. #define API_KEY "AIzaSyD-Vb49XEMVPtauYmnyJ7MRu39cQ5zFqL4"
  10. #define USER_EMAIL "tawaaf8@gmail.com"
  11. #define USER_PASSWORD "123456789"
  12. #define DATABASE_URL "tawaf-dc7b8-default-rtdb.firebaseio.com"
  13. #define DATABASE_SECRET "sgmO3Dwiwu9gkcdfkVOzbMjEIb3HBcj9GombYj4d"
  14. FirebaseData fbdo;
  15. FirebaseAuth auth;
  16. FirebaseConfig config;
  17. unsigned long dataMillis = 0;
  18. /////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. unsigned char ReadMulti[10] = {0XAA, 0X00, 0X27, 0X00, 0X03, 0X22, 0XFF, 0XFF, 0X4A, 0XDD};
  22. unsigned int timeSec = 0;
  23. unsigned int timemin = 0;
  24. unsigned int dataAdd = 0;
  25. unsigned int incomedate = 0;
  26. unsigned int parState = 0;
  27. unsigned int codeState = 0;
  28.  
  29. unsigned long currentTime = 0;
  30. unsigned long time_To_Wait_For_Removal = 15; // time in seconds
  31. String Last_EPC_Number = "";
  32.  
  33.  
  34.  
  35.  
  36. // Define the maximum number of EPC numbers that can be stored
  37. const int MAX_EPC_NUMBERS = 10;
  38.  
  39. // Create a struct to store the EPC number and its counter
  40. struct EPCData {
  41. String epcNumber;
  42. int counter;
  43. unsigned long lastUpdateTime;
  44. };
  45.  
  46. // Create an array to store the identified EPC numbers and their respective counters
  47. EPCData identifiedEPC[MAX_EPC_NUMBERS];
  48. int numIdentifiedEPC = 0; // Variable to keep track of the number of identified EPC numbers
  49.  
  50. // Function to search for an EPC number in the identifiedEPC array
  51. bool searchEPC(const String& epcNumber) {
  52. for (int i = 0; i < numIdentifiedEPC; i++) {
  53. if (identifiedEPC[i].epcNumber == epcNumber) {
  54. return true; // Found the EPC number in the array
  55. }
  56. }
  57. return false; // EPC number not found in the array
  58. }
  59.  
  60.  
  61.  
  62.  
  63. void setup() {
  64. Serial.begin(115200);
  65. init_RFID_Reader();
  66. Connect_To_WiFi();
  67. firbase_setup();
  68. Serial.println("Hello world.");
  69. }
  70.  
  71. void loop() {
  72. ReConnect_To_WiFi();
  73. read_RFID_Tags_R200_Long_Range();
  74. }//end of LOOP
  75.  
  76.  
  77. void Send_Data_To_Firebase(String EPC_Number_string, int identified_EPC_counter_int) {
  78. if (millis() - dataMillis > 1000 && Firebase.ready()) {
  79. dataMillis = millis();
  80. String PATH = "/tags/";
  81. PATH += EPC_Number_string;
  82. PATH += "/counter";
  83. Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, PATH, String(identified_EPC_counter_int)) ? "ok" : fbdo.errorReason().c_str());
  84. }
  85. }
  86.  
  87.  
  88. void init_RFID_Reader() {
  89. Serial2.begin(115200);
  90. Serial2.write(ReadMulti, 10);
  91. }
  92. void handel_EPC_Tag() {
  93. // Check if the new EPC number is already present in the identifiedEPC array
  94. if (searchEPC(Last_EPC_Number)) {
  95. // If the EPC number is found, check if 15 seconds have passed since the last update
  96. currentTime = millis();
  97. for (int i = 0; i < numIdentifiedEPC; i++) {
  98. if (identifiedEPC[i].epcNumber == Last_EPC_Number &&
  99. currentTime - identifiedEPC[i].lastUpdateTime >= (time_To_Wait_For_Removal * 1000)) {
  100. // Increment the counter and update the last update time
  101. identifiedEPC[i].counter = (identifiedEPC[i].counter + 1) % 8; // Increment and wrap around to 0 when reaching 8
  102. identifiedEPC[i].lastUpdateTime = currentTime;
  103.  
  104. // Output a Serial message indicating the updated counter
  105. Serial.print("EPC number: ");
  106. Serial.print(Last_EPC_Number);
  107. Serial.print(", Counter: ");
  108. Serial.println(identifiedEPC[i].counter);
  109. Send_Data_To_Firebase(Last_EPC_Number, identifiedEPC[i].counter);
  110. break; // Exit the loop once the EPC number is found and updated
  111. }
  112. }
  113. } else {
  114. // If the EPC number is not found, add it to the identifiedEPC array with an initial counter value of 1
  115. if (numIdentifiedEPC < MAX_EPC_NUMBERS) {
  116. identifiedEPC[numIdentifiedEPC].epcNumber = Last_EPC_Number;
  117. identifiedEPC[numIdentifiedEPC].counter = 0;
  118. identifiedEPC[numIdentifiedEPC].lastUpdateTime = millis();
  119. numIdentifiedEPC++;
  120.  
  121. // Output a Serial message indicating the new EPC number and counter
  122. Serial.print("New EPC number identified: ");
  123. Serial.print(Last_EPC_Number);
  124. Serial.print(", Counter: ");
  125. Serial.println(identifiedEPC[numIdentifiedEPC - 1].counter);
  126. Send_Data_To_Firebase(Last_EPC_Number, identifiedEPC[numIdentifiedEPC - 1].counter);
  127. } else {
  128. // Maximum number of EPC numbers reached, handle accordingly
  129. }
  130. }
  131. }
  132.  
  133.  
  134.  
  135.  
  136. void read_RFID_Tags_R200_Long_Range() {
  137. timeSec ++ ;
  138. if (timeSec >= 50000) {
  139. timemin ++;
  140. timeSec = 0;
  141. if (timemin >= 20) {
  142. timemin = 0;
  143. //digitalWrite(LED_BUILTIN, HIGH);
  144. Serial2.write(ReadMulti, 10);
  145. //digitalWrite(LED_BUILTIN, LOW);
  146. }
  147. }
  148. if (Serial2.available() > 0) {
  149. incomedate = Serial2.read();
  150. if ((incomedate == 0x02) & (parState == 0)) {
  151. parState = 1;
  152. }
  153. else if ((parState == 1) & (incomedate == 0x22) & (codeState == 0)) {
  154. codeState = 1;
  155. dataAdd = 3;
  156. }
  157. else if (codeState == 1) {
  158. dataAdd ++;
  159. if (dataAdd == 6) {
  160. //Serial.print("RSSI:");
  161. //Serial.println(incomedate, HEX);
  162. }
  163. else if ((dataAdd == 7) | (dataAdd == 8)) {
  164. if (dataAdd == 7) {
  165. //Serial.print("PC:");
  166. //Serial.print(incomedate, HEX);
  167. }
  168. else {
  169. //Serial.println(incomedate, HEX);
  170. }
  171. }
  172. else if ((dataAdd >= 9) & (dataAdd <= 20)) {
  173. if (dataAdd == 9) {
  174. //Serial.print("EPC:");
  175. }
  176. //Serial.print(incomedate, HEX);
  177. Last_EPC_Number += String(incomedate, HEX);
  178. Last_EPC_Number.toUpperCase();
  179.  
  180. }
  181.  
  182. else if (dataAdd >= 21) {
  183. //Serial.println(" ");
  184. //Serial.print("Saved:");;
  185. //Serial.println(Last_EPC_Number);
  186. handel_EPC_Tag();
  187. Last_EPC_Number = "";
  188. dataAdd = 0;
  189. parState = 0;
  190. codeState = 0;
  191. }
  192. }
  193. else {
  194. dataAdd = 0;
  195. parState = 0;
  196. codeState = 0;
  197. }
  198. }
  199. }
  200.  
  201.  
  202. void ReConnect_To_WiFi() {
  203. while (WiFi.status() != WL_CONNECTED) {
  204. Serial.print(".");
  205. Connect_To_WiFi();
  206. }
  207. }
  208. void Connect_To_WiFi() {
  209. WiFiManager wm;
  210. bool res;
  211. res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
  212. if (!res) {
  213. Serial.println("Failed to Connect!!");
  214. // ESP.restart();
  215. }
  216. else {
  217. //if you get here you have connected to the WiFi
  218. Serial.println("Connected...");
  219. }
  220. }
  221.  
  222.  
  223. void firbase_setup() {
  224. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  225. config.api_key = API_KEY;
  226. auth.user.email = USER_EMAIL;
  227. auth.user.password = USER_PASSWORD;
  228. config.database_url = DATABASE_URL;
  229. Firebase.reconnectWiFi(true);
  230. fbdo.setResponseSize(4096);
  231. String base_path = "/UsersData/";
  232. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  233. config.max_token_generation_retry = 5;
  234. Firebase.begin(&config, &auth);
  235. String var = "$userId";
  236. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  237. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement