1

I'm learning Python whilst tinkering with various 'kits' I got for my Raspberry Pi. Currently messing around with the RFID-RC522, a simple to use RFID read/write board.

I have independent Write.py and Read.py which work fine, but wanted to combine the two into one program and have a button which determines which function to use - either read or write. I've added a GPIO event listener for the button, which functions as expected, until I want that button to call a function which contains the input() function.

If I call the writeFunction() without user input, just by referencing it at the bottom of my program, it works fine. The user is prompted to input some text, they press Enter, then is prompted to place their RFID tag on the reader and voila, the text gets written to that tag. Great.

If I try calling that same writeFunction from a button press, however, the input() doesn't act the same, the input text isn't formatted blue as it is with the working method (I'm using the console in Thonny, on the Pi), and I get an error stating 'Wrong type of command waiting for input'. I'm using Python 3 so I think I'm using input() correctly, as I understand there's a difference using this or raw_input() if you're on Python 2.

This works, as the button isn't being used.


import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set GPIO21 to be an input pin with initial value OFF
reader = SimpleMFRC522() # Define reader use

def buttonWasPressed(channel):
    writeFunction()

def writeFunction():
        text = input('New data to write: ')
        print('Place tag for writing...')
        reader.write(text)
        print("Written")

GPIO.add_event_detect(21, GPIO.RISING, callback=buttonWasPressed) # Add event listener for the GPIO pin (button)

writeFunction()

This does not work, the button is used:


import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set GPIO21 to be an input pin with initial value OFF
reader = SimpleMFRC522() # Define reader use

def buttonWasPressed(channel):
    writeFunction()

def writeFunction():
        text = input('New data to write: ')
        print('Place tag for writing...')
        reader.write(text)
        print("Written")

GPIO.add_event_detect(21, GPIO.RISING, callback=buttonWasPressed) # Add event listener for the GPIO pin (button)

I've tried having the button call the writeFunction() itself, rather than go through another function, but it doesn't make a difference. (I'd thought maybe the argument of 'channel' might've been confusing it but alas, doesn't seem to affect the outcome).

Here's my error when I press Enter after being prompted to input some text (sorry it's a screenshot, for whatever reason it won't copy from TeamViewer to my OS:

error message from Thonny

5
  • 1
    This is an error in the IDE (Thonny). Try using another one like Mu or IDLE or run the code from the command line. Jan 15 at 13:30
  • I knew there was a reason I ditched Thonny on my last Pi install a couple of years ago, I've had issues with it before but never put two and two together here. Works as expected in Mu. Thanks so much!
    – Joe L
    Jan 15 at 14:42
  • 2
    If you have an issue with Thonny, why not report it? github.com/thonny/thonny/issues
    – CoderMike
    Jan 15 at 20:32
  • @JoeL, Thonny's developer here. I created an issue for this: github.com/thonny/thonny/issues/1557
    – Aivar
    Jan 20 at 10:45
  • @JoeL, do you remember or can you check, whether Thonny gave you >>> prompt for submitting the input?
    – Aivar
    Jan 20 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.