Dear Friends,
I am trying an experiment with RFID reader MFRC522 library with WEMos mini as follows:
1.I have a Mifare 1K card with some amount of credit money stored on it say Rs.10000 at sector 12
2. I am writing a code to detect the card, read the credit and send it to serial terminal
3. The serial terminal ( here B4J serial connector ) sends back the amount to be deducted from credit back to the wemos
4. The received amount has to be deducted from credit and the new balance has to be written back to sector 12
The reading of credit is OK, the deduct amount reaches the Wemos and the new balance is correctly computed. The problem starts when this new amount has to be written back to sector 12. As required I use authenticate again before write, but it returns authentication failure and does not complete the write.
Since I an very new to RFID cards , I am unable to debug it. Can any of you guys point out whats going on?
My code is as follows:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
regards,
			
			I am trying an experiment with RFID reader MFRC522 library with WEMos mini as follows:
1.I have a Mifare 1K card with some amount of credit money stored on it say Rs.10000 at sector 12
2. I am writing a code to detect the card, read the credit and send it to serial terminal
3. The serial terminal ( here B4J serial connector ) sends back the amount to be deducted from credit back to the wemos
4. The received amount has to be deducted from credit and the new balance has to be written back to sector 12
The reading of credit is OK, the deduct amount reaches the Wemos and the new balance is correctly computed. The problem starts when this new amount has to be written back to sector 12. As required I use authenticate again before write, but it returns authentication failure and does not complete the write.
Since I an very new to RFID cards , I am unable to debug it. Can any of you guys point out whats going on?
My code is as follows:
			
				B4X:
			
		
		
		Sub Process_Globals
    Public Serial1 As Serial
    Private rfid As MFRC522
    Private bc As ByteConverter
    Dim d1 As D1Pins
    Dim credit,deduct As Double
    Dim authenticated As Boolean
  
    Dim astream As AsyncStreams
    'RST->D4
    'SDA(SS)->D3
    'MOSI->D7
    'MISO->D6
    'SCK->D5
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    rfid.Initialize(d1.D3, d1.D4, "rfid_CardPresent")
    astream.Initialize(Serial1.Stream, "astream_newdata", Null)
    rfid.LogVersion
  
End Sub
Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
    Log("UID: ", bc.HexFromBytes(UID))
    Log("Type: ", CardType, ", Is it Mifare: ", rfid.IsMifare)
    Dim txt As String
    If rfid.IsMifare Then
         txt=card_read(12)
         Log(txt)
            'astream.Write(bc.StringToBytes(txt))
         credit=txt
        Else
            Log("Failed to read")
    End If
End Sub
Sub card_read(sector As Int) As String
    Dim msg As String
    Dim buffer(18) As Byte
    authenticated=card_authenticate(sector)
     If rfid.MifareRead(sector, buffer) > 0 Then
    Dim read_buffer(7) As Byte
    For i=0 To 6
        read_buffer(i)=buffer(i)
    Next
    msg=bc.stringFromBytes(read_buffer)
    Else
        msg=""
    End If
    Return msg
End Sub
Sub card_write(sector As Int,msg As String)
    Dim buffer(18) As Byte
    msg=JoinStrings(Array As String(msg,"            "))
    Log(msg)
    buffer=bc.stringToBytes(msg)
    authenticated=card_authenticate(sector)
    Log("Write:", rfid.MifareWrite(sector, buffer))
End Sub
Sub card_authenticate(sector As Int) As Boolean
    If rfid.MifareAuthenticate(sector) = False Then
        Log("Failed to authenticate")
        authenticated=False
    Else
        authenticated=True
    End If
    Return authenticated
  
End Sub
Sub astream_newdata (Buffer() As Byte)
    Dim msg As String
    msg=bc.stringFromBytes(Buffer)
    amt_deduct(msg)
End Sub
Sub amt_deduct(msg As String)
    deduct=msg
    Dim new_balance As Double=credit-deduct
    Dim new_balance_txt As String=new_balance
    card_write(12,new_balance_txt)
    Log("New balance:",card_read(12))
End Subregards,
 
				 
 
		 
 
		 
 
		 
 
		 
 
		