Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

MFRC522 - Problem reading Tag #459

Closed
UdoWeberJR opened this issue Apr 7, 2021 · 3 comments
Closed

MFRC522 - Problem reading Tag #459

UdoWeberJR opened this issue Apr 7, 2021 · 3 comments

Comments

@UdoWeberJR
Copy link

UdoWeberJR commented Apr 7, 2021

Describe the bug
I have two different typs of tags, a chip card and stickers. When I use the chip card, the behavior is as expected. When I try to use the stickers I get the error "mfrc522: wrong number of bits 4" with changing number of bits between 4 and 7.
I build a small example to show my settings. I tried "commands.PICC_AUTHENT1B" as well as "commands.PICC_AUTHENT1A" for authentication mode.

With my smartphone I'm able to read both tags, with the MIFARE Classic Tool from IKARUS Projects. So the default key (FFFFFFFFFFFF) seem to be correct. I'm also able to write data to both tags with this app.

I already tried a lower and a higher antenna gain without success.
I also tried to use a different reader on another raspberry with the same result.

I will be happy for any help, thanks.

To Reproduce
Steps to reproduce the behavior:

  1. Run program
package main

import (
	"fmt"
	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/conn/spi"
	"periph.io/x/periph/conn/spi/spireg"
	"periph.io/x/periph/experimental/devices/mfrc522"
	"periph.io/x/periph/experimental/devices/mfrc522/commands"
	"periph.io/x/periph/host"
	"time"
)

// mfrc522 rfid device
var rfid *mfrc522.Dev

// spi port
var port spi.PortCloser

// pins used for rest and irq
const (
	resetPin = "P1_22" // GPIO 25
	irqPin   = "P1_18" // GPIO 24
)

/*
Setup inits and starts hardware.
*/
func setup() {
	var err error

	// guarantees all drivers are loaded.
	if _, err = host.Init(); err != nil {
		fmt.Println(err)
	}

	// get the first available spi port (empty string).
	port, err = spireg.Open("")
	if err != nil {
		fmt.Println(err)
	}

	// get GPIO rest pin from its name
	var gpioResetPin gpio.PinOut = gpioreg.ByName(resetPin)
	if gpioResetPin == nil {
		fmt.Println("Failed to find %v", resetPin)
	}

	// get GPIO irq pin from its name
	var gpioIRQPin gpio.PinIn = gpioreg.ByName(irqPin)
	if gpioIRQPin == nil {
		fmt.Println("Failed to find %v", irqPin)
	}

	rfid, err = mfrc522.NewSPI(port, gpioResetPin, gpioIRQPin, mfrc522.WithSync())
	if err != nil {
		fmt.Println(err)
	}

	// setting the antenna signal strength, signal strength from 0 to 7
	rfid.SetAntennaGain(5)

	fmt.Println("Started rfid reader.")
}

func stringIntoByte16(str string) [16]byte {
	var data [16]byte
	copy(data[:], str) // copy already checks length of str
	return data
}

func main() {
	setup()

	// trying to read UID
	data, err := rfid.ReadUID(5 * time.Second)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(data)
	}

	// trying to write data
	err = rfid.WriteCard(5*time.Second, byte(commands.PICC_AUTHENT1B), 2, 0, stringIntoByte16("Hallo Welt"), mfrc522.DefaultKey)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Write OK")
	}

	// trying to read data
	data, err = rfid.ReadCard(5*time.Second, commands.PICC_AUTHENT1A, 2, 0, mfrc522.DefaultKey)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(data)
	}

}
  1. Run it.
  2. See error
Started rfid reader.
mfrc522: wrong number of bits 4
mfrc522: wrong number of bits 5
mfrc522: wrong number of bits 4

Expected behavior

Started rfid reader.
[18 100 14 52 76]
Write OK
[72 97 108 108 111 32 87 101 108 116 0 0 0 0 0 0]

Platform (please complete the following information):

  • OS: Raspbian GNU/Linux 10 (buster)
  • Board: Raspberry PI 3B+

Additional context
I read out the information of both tags, the only difference I see is the UID.

Screenshot_20210407-053133
Screenshot_20210407-053145
Screenshot_20210407-053155
Screenshot_20210407-053124

@UdoWeberJR
Copy link
Author

I changed the power supply and it worked, sorry for opening an issue.

@maruel
Copy link
Contributor

maruel commented Apr 7, 2021

Wow thanks for the detailed report.

Two requests:

@UdoWeberJR
Copy link
Author

Thanks for the hint, I changed to v3.

I also added documentation as far as my knowledge goes.
Tried to upload a picture as well, but get message "Uploads are disabled. File uploads require push access to this repository."

To be honest, I do not know if it really was the power supply, just read somewhere on the web, that some hardware can be sensitive. So it was just a guess, I changed it and it worked. So I have no way for diagnose.
But still, I added a hint in the documentation.

PXL_20210407_144044413 MP

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants