Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU load by daemon_rfid_reader.py and MFRC522 #275

Closed
kaoz3000 opened this issue Oct 20, 2018 · 17 comments
Closed

High CPU load by daemon_rfid_reader.py and MFRC522 #275

kaoz3000 opened this issue Oct 20, 2018 · 17 comments

Comments

@kaoz3000
Copy link
Contributor

Hi,
I'm using the MFRC522 Module with the scripts from @keinkurt: https://github.com/keinkurt/RPi-Jukebox-RFID/tree/MFRC255/scripts on a Raspberry Pi Zero W
I just recognize that the daemon generates a pretty high CPU load between 50–80%:
pi 486 **80.4** 1.7 11608 7624 ? Rs 20:47 17:29 /usr/bin/python2 /home/pi/RPi-Jukebox-RFID/scripts/daemon_rfid_reader.py
This is much more then mpd, which make about 7–10%

Maybe communicate with the reader less often?

@kaoz3000 kaoz3000 changed the title High CPU Load with daemon_rfid_reader.py an MFRC522 High CPU Load by daemon_rfid_reader.py and MFRC522 Oct 20, 2018
@kaoz3000 kaoz3000 changed the title High CPU Load by daemon_rfid_reader.py and MFRC522 High CPU load by daemon_rfid_reader.py and MFRC522 Oct 20, 2018
@varac
Copy link
Contributor

varac commented Nov 5, 2018

Lower the CPU load would be great indeed.
The problem is that the used MFRC522.py class comes from the MFRC522-python project. This project has anunresolved issue with high CPU load because it's not possible to use interrupts easily to listen for events, and the only way to read tags is a nasty while loop, which looks for tags over and over again.
Adding a time.sleep(0.3) in this loop will lower the CPU load, but it's still not ideal.

The solution would be to migrate to the pi-rc522 class, which solved this issue by adding support for listening on events and introduced a wait_for_tag() function which just waits until a tag is detected.
Moving from a simple while loop in a different project to wait_for_tag() in a different project decreased CPU load from 80% to 1-2% with this class!

@varac
Copy link
Contributor

varac commented Nov 5, 2018

@keinkurt thx for bringing RC522 support to this project - don't you experience this high CPU load issue yourself ?

@varac
Copy link
Contributor

varac commented Nov 5, 2018

Just for the numbers, adding a time.sleep(0.3) as described above decreased the CPU load at my system (rpi 2) from 100% to 40%.

@fenkman
Copy link
Contributor

fenkman commented Nov 24, 2018

Lower the CPU load would be great indeed.
The problem is that the used MFRC522.py class comes from the MFRC522-python project. This project has anunresolved issue with high CPU load because it's not possible to use interrupts easily to listen for events, and the only way to read tags is a nasty while loop, which looks for tags over and over again.
Adding a time.sleep(0.3) in this loop will lower the CPU load, but it's still not ideal.

The solution would be to migrate to the pi-rc522 class, which solved this issue by adding support for listening on events and introduced a wait_for_tag() function which just waits until a tag is detected.
Moving from a simple while loop in a different project to wait_for_tag() in a different project decreased CPU load from 80% to 1-2% with this class!

I just submitted a pull request ( #345) doing what you suggested. Thanks for the tip. Saved my Pi-zero-based project.

MiczFlor added a commit that referenced this issue Nov 25, 2018
CPU issue #275: Replace MFRC522 with pi-rc522 library (ondryaso/pi-rc522)
@MiczFlor
Copy link
Owner

MiczFlor commented Nov 25, 2018

Hi, I merged this with the develop branch
@fenkman
#345
and it will go to the master branch soonish. Please test develop, if this works for you. My fear, that happened before: the merge will break all USB RFID readers - and that caused me sleepless nights and unnecessary work. that happened before. See:
#231

@varac
Copy link
Contributor

varac commented Nov 27, 2018

I am successfully using develop together with my MFC522 reader.
I needed to do 2 adjustments:

So @MiczFlor please judge yourself my commits, I'm not sure how to do a proper PR since I don't know if Reader.py.experimental will replace the current Reader.py.

Btw, the CPU usage improvements are great due to the new use of pi-rc522 and wait_for_tag() !

@cubertt
Copy link

cubertt commented Nov 27, 2018

i used the master classic Version with the deamon_rfid_reader.py from the Version of Kein Kurt successfully.

The orginal deamon_rfid_reader.py gives the following error:

pi@raspberrypi:/RPi-Jukebox-RFID/scripts $ python daemon_rfid_reader.py
/home/pi/RPi-Jukebox-RFID/scripts
Traceback (most recent call last):
File "daemon_rfid_reader.py", line 14, in
cardid = reader.readCard()
AttributeError: 'Reader' object has no attribute 'readCard'
pi@raspberrypi:
/RPi-Jukebox-RFID/scripts $

(sorry for the wrong formated text)

Btw, the CPU usage improvements are great due to the new use of pi-rc522 and wait_for_tag()

Yeah this is great!

@varac
Copy link
Contributor

varac commented Nov 27, 2018

@cubertt See how to solve the Reader' object has no attribute 'readCard' error in this commit, l. 14.

@fenkman
Copy link
Contributor

fenkman commented Nov 27, 2018

Thank you for merging this so quickly and thank you for testing. Unfortunately I cannot verify USB readers - not having one and going for the seemingly cheaper RC-522 module was the reason for me to work on this anyway :-)

Btw, the CPU usage improvements are great due to the new use of pi-rc522 and wait_for_tag() !

All the credit goes to you @varac for pointing us in the right direction.

One caveat to be mentioned here: The connection layout for the RC-522 that you find all over the internet is not sufficient to make this work. You have to connect the RC-522 IRQ pin with GPIO25 to make this work as described here: pi-rc522

@Ardios
Copy link

Ardios commented Dec 3, 2018

It doesn't work for me. After a fresh installation don't get my reader correctly activated anymore he deactivates himself after I try to read a card

Has anyone tried this with the current version 1.1.8-rc3 classic?

@MiczFlor
Copy link
Owner

MiczFlor commented Dec 4, 2018

Please read this
https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/RFID-Reader-Special
I hope that helps.

@Ardios
Copy link

Ardios commented Dec 4, 2018

Thanks @MiczFlor

I have already tried this manual but as soon as I try to read a card it's over.

@alex-voigt
Copy link

I can't get it to work as well.

I have a RC522 module that did not work with the latest version of the install script. After reading here #11 and switching to the Version-1.1.5-dynamicRFIDreader branch I could register the device and use it. But CPU load was so high that my pi couldn't play audio from Spotify without interruption.

So I checked out the develop branch and now the reader doesn't work again.
Running RegisterDevice.py results in an empty reader list.

The additional pin is connected.

@varac Could you please help on how to set up? The changes suggested didn't work for me. But they don't seem to be the problem right now because the reader is not found at all. Setting the device in deviceName.txt manually also didn't work.

@alex-voigt
Copy link

Ok, I got it working now. Using the get_devices method from the Reader did the trick...

alex-voigt@aca4519

@varac
Copy link
Contributor

varac commented Dec 8, 2018

@alex-voigt Have a look at my varac branch.
This is how I got it working.

MiczFlor added a commit that referenced this issue Dec 16, 2018
Fix experimental reader to be compatible with stable reader #275
@MiczFlor
Copy link
Owner

Hi @Ardios @alex-voigt @fenkman
I merged alex-voight's pull request to develop branch. Please check if this works for you now, before I merge this to master.
Thanks for the input, support and good ideas!

@fenkman
Copy link
Contributor

fenkman commented Dec 18, 2018

I checked out the most recent develop branch and it (still) works for me. Thanks for all the good work! 👍

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

No branches or pull requests

7 participants