78Views0Replies

Author Options:

Unable to retrieve Rfid variable. Answered

I am unable to retrieve string (RFID) from Arduino connected to Android app through Bluetooth .

Arduino code

#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>

#define SS_PIN 10
#define RST_PIN 9

char command;
String string;

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

byte nuidPICC[3];// Init array that will store new NUID

String RFID;
void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
}

void loop() {
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;
   
   for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
      if(i==0)
      {
        RFID=String(nuidPICC[i],HEX);
        //Serial.println(RFID);
      }
      else
      {
       String temp =String(nuidPICC[i],HEX);
       RFID=String(RFID + temp); 
      }
   }
  
   //Serial.println("\n Rfid :-");
   //Serial.println(RFID);
  
  if (Serial.available() > 0)
    {string = "";}
   
    while(Serial.available() > 0)
    {
      command = ((byte)Serial.read());
     
      if(command == ':')
      {
        break;
      }
     
      else
      {
        string += command;
      }
     
      delay(1);
    }

    if(string == "TO")
    {
       // ledOn();
        //ledon = true;
        //Serial.println(RFID);
    }
 
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}


Android Code

rfid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                readrfid();
            }
        });

 --------------------------------------------------------------------------------------------
    // This will be the Readfrom Rfid !
    private void readrfid()
    {
        String tp;
        if (btSocket!=null)
        {
            try
            {

                btSocket.getOutputStream().write("TO".toString().getBytes());
                ins = btSocket.getInputStream();
                    while(ins.available() > 0)
                    {
                        //Reading from serial input
                        valor= Byte.toString((byte)ins.read());
                    }
                //Displaying readed value
                Toast.makeText(getApplicationContext(), valor , Toast.LENGTH_SHORT).show();
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


Thank You.

Discussions