Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFiManager.h>
- #include <Arduino.h>
- #include <FirebaseESP32.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- ///////////////firbase_setup//////////////////////////////////////////
- #define API_KEY "AIzaSyD-Vb49XEMVPtauYmnyJ7MRu39cQ5zFqL4"
- #define USER_EMAIL "tawaaf8@gmail.com"
- #define USER_PASSWORD "123456789"
- #define DATABASE_URL "tawaf-dc7b8-default-rtdb.firebaseio.com"
- #define DATABASE_SECRET "sgmO3Dwiwu9gkcdfkVOzbMjEIb3HBcj9GombYj4d"
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- unsigned long dataMillis = 0;
- /////////////////////////////////////////////////////////////////////
- unsigned char ReadMulti[10] = {0XAA, 0X00, 0X27, 0X00, 0X03, 0X22, 0XFF, 0XFF, 0X4A, 0XDD};
- unsigned int timeSec = 0;
- unsigned int timemin = 0;
- unsigned int dataAdd = 0;
- unsigned int incomedate = 0;
- unsigned int parState = 0;
- unsigned int codeState = 0;
- unsigned long currentTime = 0;
- unsigned long time_To_Wait_For_Removal = 15; // time in seconds
- String Last_EPC_Number = "";
- // Define the maximum number of EPC numbers that can be stored
- const int MAX_EPC_NUMBERS = 10;
- // Create a struct to store the EPC number and its counter
- struct EPCData {
- String epcNumber;
- int counter;
- unsigned long lastUpdateTime;
- };
- // Create an array to store the identified EPC numbers and their respective counters
- EPCData identifiedEPC[MAX_EPC_NUMBERS];
- int numIdentifiedEPC = 0; // Variable to keep track of the number of identified EPC numbers
- // Function to search for an EPC number in the identifiedEPC array
- bool searchEPC(const String& epcNumber) {
- for (int i = 0; i < numIdentifiedEPC; i++) {
- if (identifiedEPC[i].epcNumber == epcNumber) {
- return true; // Found the EPC number in the array
- }
- }
- return false; // EPC number not found in the array
- }
- void setup() {
- Serial.begin(115200);
- init_RFID_Reader();
- Connect_To_WiFi();
- firbase_setup();
- Serial.println("Hello world.");
- }
- void loop() {
- ReConnect_To_WiFi();
- read_RFID_Tags_R200_Long_Range();
- }//end of LOOP
- void Send_Data_To_Firebase(String EPC_Number_string, int identified_EPC_counter_int) {
- if (millis() - dataMillis > 1000 && Firebase.ready()) {
- dataMillis = millis();
- String PATH = "/tags/";
- PATH += EPC_Number_string;
- PATH += "/counter";
- Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, PATH, String(identified_EPC_counter_int)) ? "ok" : fbdo.errorReason().c_str());
- }
- }
- void init_RFID_Reader() {
- Serial2.begin(115200);
- Serial2.write(ReadMulti, 10);
- }
- void handel_EPC_Tag() {
- // Check if the new EPC number is already present in the identifiedEPC array
- if (searchEPC(Last_EPC_Number)) {
- // If the EPC number is found, check if 15 seconds have passed since the last update
- currentTime = millis();
- for (int i = 0; i < numIdentifiedEPC; i++) {
- if (identifiedEPC[i].epcNumber == Last_EPC_Number &&
- currentTime - identifiedEPC[i].lastUpdateTime >= (time_To_Wait_For_Removal * 1000)) {
- // Increment the counter and update the last update time
- identifiedEPC[i].counter = (identifiedEPC[i].counter + 1) % 8; // Increment and wrap around to 0 when reaching 8
- identifiedEPC[i].lastUpdateTime = currentTime;
- // Output a Serial message indicating the updated counter
- Serial.print("EPC number: ");
- Serial.print(Last_EPC_Number);
- Serial.print(", Counter: ");
- Serial.println(identifiedEPC[i].counter);
- Send_Data_To_Firebase(Last_EPC_Number, identifiedEPC[i].counter);
- break; // Exit the loop once the EPC number is found and updated
- }
- }
- } else {
- // If the EPC number is not found, add it to the identifiedEPC array with an initial counter value of 1
- if (numIdentifiedEPC < MAX_EPC_NUMBERS) {
- identifiedEPC[numIdentifiedEPC].epcNumber = Last_EPC_Number;
- identifiedEPC[numIdentifiedEPC].counter = 0;
- identifiedEPC[numIdentifiedEPC].lastUpdateTime = millis();
- numIdentifiedEPC++;
- // Output a Serial message indicating the new EPC number and counter
- Serial.print("New EPC number identified: ");
- Serial.print(Last_EPC_Number);
- Serial.print(", Counter: ");
- Serial.println(identifiedEPC[numIdentifiedEPC - 1].counter);
- Send_Data_To_Firebase(Last_EPC_Number, identifiedEPC[numIdentifiedEPC - 1].counter);
- } else {
- // Maximum number of EPC numbers reached, handle accordingly
- }
- }
- }
- void read_RFID_Tags_R200_Long_Range() {
- timeSec ++ ;
- if (timeSec >= 50000) {
- timemin ++;
- timeSec = 0;
- if (timemin >= 20) {
- timemin = 0;
- //digitalWrite(LED_BUILTIN, HIGH);
- Serial2.write(ReadMulti, 10);
- //digitalWrite(LED_BUILTIN, LOW);
- }
- }
- if (Serial2.available() > 0) {
- incomedate = Serial2.read();
- if ((incomedate == 0x02) & (parState == 0)) {
- parState = 1;
- }
- else if ((parState == 1) & (incomedate == 0x22) & (codeState == 0)) {
- codeState = 1;
- dataAdd = 3;
- }
- else if (codeState == 1) {
- dataAdd ++;
- if (dataAdd == 6) {
- //Serial.print("RSSI:");
- //Serial.println(incomedate, HEX);
- }
- else if ((dataAdd == 7) | (dataAdd == 8)) {
- if (dataAdd == 7) {
- //Serial.print("PC:");
- //Serial.print(incomedate, HEX);
- }
- else {
- //Serial.println(incomedate, HEX);
- }
- }
- else if ((dataAdd >= 9) & (dataAdd <= 20)) {
- if (dataAdd == 9) {
- //Serial.print("EPC:");
- }
- //Serial.print(incomedate, HEX);
- Last_EPC_Number += String(incomedate, HEX);
- Last_EPC_Number.toUpperCase();
- }
- else if (dataAdd >= 21) {
- //Serial.println(" ");
- //Serial.print("Saved:");;
- //Serial.println(Last_EPC_Number);
- handel_EPC_Tag();
- Last_EPC_Number = "";
- dataAdd = 0;
- parState = 0;
- codeState = 0;
- }
- }
- else {
- dataAdd = 0;
- parState = 0;
- codeState = 0;
- }
- }
- }
- void ReConnect_To_WiFi() {
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- Connect_To_WiFi();
- }
- }
- void Connect_To_WiFi() {
- WiFiManager wm;
- bool res;
- res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
- if (!res) {
- Serial.println("Failed to Connect!!");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("Connected...");
- }
- }
- void firbase_setup() {
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- config.api_key = API_KEY;
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- config.database_url = DATABASE_URL;
- Firebase.reconnectWiFi(true);
- fbdo.setResponseSize(4096);
- String base_path = "/UsersData/";
- config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
- config.max_token_generation_retry = 5;
- Firebase.begin(&config, &auth);
- String var = "$userId";
- String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
- Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement