RaspberryPi-Samples / py-my-key

Access control with RaspberryPi and NFC card reader
ISC License
1 stars 1 forks source link

WiringPi error #19

Closed lwalter86 closed 8 years ago

lwalter86 commented 8 years ago

An error occurs a few minutes after we launch the script :

" WiringPiSetup : unable to open /dev/mem : too many open files "
scls19fr commented 8 years ago

This could help https://www.raspberrypi.org/forums/viewtopic.php?t=56496&p=427033 http://stackoverflow.com/questions/27376914/python-best-way-to-create-infinitive-loop-without-a-crash

lwalter86 commented 8 years ago

NXP Explore-NFC reader doesn't use the Raspberry Pi pins we use to manage leds or buttons

scls19fr commented 8 years ago

A (quite dirty) workaround might be to run python script inside a Bash loop

until python cli_rpi_access.py; do
  echo Script failed, retrying in 2 seconds...
  sleep 2
done

http://serverfault.com/questions/80862/bash-script-repeat-command-if-it-returns-an-error

scls19fr commented 8 years ago

An other approach to consider might be to update WiringPi http://wiringpi.com/download-and-install/ and its Python wrapper https://github.com/WiringPi/WiringPi-Python

scls19fr commented 8 years ago

After updating WiringPi you should try

gpio -v
gpio readall

Reference: http://wiringpi.com/download-and-install/

scls19fr commented 8 years ago

Updating WiringPi Python wrapper may also be necessary

https://github.com/WiringPi/WiringPi-Python

lwalter86 commented 8 years ago

I did it. It works fine.

Le 07/04/2016 18:21, scls19fr a écrit :

After updating WiringPi you should try

|gpio -v gpio readall |

Reference: http://wiringpi.com/download-and-install/

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/RaspberryPi-Samples/py-my-key/issues/19#issuecomment-206978521

lwalter86 commented 8 years ago

I think i fund the error. The nxppy.Mifare() function must be called once. See the _read function of the NxppyReader class defined in readers module. I would like to execute at init but i don't know how

scls19fr commented 8 years ago

Great news!

One way to fix it could be to create an (empty) initialize method in BaseReader and to implement this initialize method in NxppyReader


class BaseReader(Base):
    def initialize(self):
        pass

class NxppyReader(BaseReader):
    def initialize(self):
        self.mifare = nxppy.Mifare()

    def _read(self):
        data = ''
        try:
            data = self.mifare.select()
        except nxppy.SelectError:
            pass
        if data != '':
            return data
        else:
            return None

We will need to explicitly call this initialize method in cli_rpi_access.py

    def create_reader(self):
        reader = self.Reader(comment='First reader')
        reader.initialize()

An other way could be to put nxppy.Mifare() in __init__ but also call inherited class __init__ method using super(...). I'm not sure about this second idea as Base is an SQLAlchemy object class... and there is probably some magical stuff around this ;-) so it should be heavily "tested" using some print calls.

lwalter86 commented 8 years ago

The initialise method fix this issue but not in the create_reader() function. I fix it by calling the method in the main function for now. I will consider this next week.

lwalter86 commented 8 years ago

create_reader() is only executed at init : sudo python py_my_key/cli_rpi_access.py --init

Or, we need to execute nxppy.Mifare() each time we start the application. So we have to put the initialize() call in the run() method of the app.

scls19fr commented 8 years ago

Yes you are right !

Thanks

scls19fr commented 8 years ago

You can probably now merge Led_management branch with master branch.