Forum menu
Python program help...
 

[Closed] Python program help please

Posts: 843
Free Member
Topic starter
 

I'm trying to program a Pi to be a music player controlled by RFID cards but am stuck on getting the card input into the program.
I'm using Python 3 on a Pi B+

I can make it play a VLC playlist from files stored on the SD card (or USB stick), but I can't get the card No into the program to associate the number with the playlist.

The card reader is a cheapo USB one This and it reads the number OK, even in Python, but I can't use the number in a program?
I suspect that the Python code doesn't like the zeros at the beginning of the card number, but I could well be wrong so don't take this as gospel.

Any of you IT bods got any ideas?


 
Posted : 17/06/2019 7:41 pm
Posts: 7971
Full Member
 

Are you getting a specific error or is it just getting nulled.
Offhand would try int(input) in case its coming in as string.


 
Posted : 17/06/2019 7:44 pm
Posts: 31206
Full Member
 

Hard to say without your code but I'm guessing you should treat the card number as a string, not as an integer. That should avoid issues with leading zeroes.

e.g.

card_to_playlist_lookup = {
 "00000000001" : "Dads_80s_classics.lst",
 "00000000002" : "BestOfDisney.lst",
 "00000000003" : "DireStraits.lst"
}

# read number

if card_number in card_to_playlist_lookup:
    playlist = card_to_playlist_lookup[card_number]
    # do stuff with playlist
else:
    print "Card number not recognised!"

 
Posted : 17/06/2019 7:45 pm
Posts: 843
Free Member
Topic starter
 

Thanks chaps. See this is my trouble, two answers and both suggest different methods. LOL!

I'm learning in my normal haphazard fashion of looking up bits of code on the net, and then stitching them together eventually. Not the best way to learn but it works for me (sort of).

I've looked at so many examples in the last two days that I'm thoroughly confused now.

What I think I need is for someone to just point me in the right direction regarding the input so I can code this...

Grab this number - convert it to whatever - now use it to refer it to a url


 
Posted : 17/06/2019 7:53 pm
Posts: 31206
Full Member
 

It is too hard to say without seeing your code.

I'd suggest printing out whatever you get back from the card reader. If that is coming out with leading zeroes then it is a string. You can either keep it as a string as I showed, or keep it as a string but strip the leading zeros using card_number.lstrip("0") or just convert it to a number as @dissonance suggested.


 
Posted : 17/06/2019 7:58 pm
Posts: 843
Free Member
Topic starter
 

GrahamS, thanks. It's getting the card number into the program whole that I cant do, once it's in there I reckon I should be able to knock up some frankenstein code to make it run the playlist.

The IDE (Idle) reads it in the shell but I can't do anything with it?


 
Posted : 17/06/2019 7:59 pm
Posts: 31206
Full Member
 

Okay, so how are you reading it in Idle?
Can't you just do the same thing and assign it to a variable?


 
Posted : 17/06/2019 8:04 pm
Posts: 843
Free Member
Topic starter
 

This is one version that I tried...

import time
import sys

card = '0004370273'
def main():
with open('/dev/tty0', 'r') as tty:
while True:
RFID_input = tty.readline()
if raw_input == card:
print ("Access Granted")

print ("Read code from RFID reader:{0}").format(RFID_input)
else:
print ("Access Denied")

And this is the error message...

============== RESTART: /home/pi/python projects/cardreader2.py ==============
>>> 0004370273
SyntaxError: invalid token
>>>


 
Posted : 17/06/2019 8:50 pm
Posts: 843
Free Member
Topic starter
 

This was another one...

from time import sleep

reader = input

file = open("ID.txt", "a")

try:
while True:

id, text = reader.read()
print(id)
print(text)

file.Write(str(id)+"\n")

except KeyboardInterupt:
print("cleaning up")
GPIO.cleanup()
file.close()

And this is the error message for this one...

============== RESTART: /home/pi/python projects/cardreader3.py ==============
Traceback (most recent call last):
File "/home/pi/python projects/cardreader3.py", line 10, in <module>
id, text = reader.read()
AttributeError: 'builtin_function_or_method' object has no attribute 'read'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/python projects/cardreader3.py", line 16, in <module>
except KeyboardInterupt:
NameError: name 'KeyboardInterupt' is not defined
>>> 0004370273
SyntaxError: invalid token
>>>


 
Posted : 17/06/2019 8:51 pm
Posts: 31206
Full Member
 

Yikes! Okay now I’m very confused. Can you give us any details of the card reader or documentation that tells us how to read from it?


 
Posted : 17/06/2019 9:09 pm
Posts: 843
Free Member
Topic starter
 

It's just a generic USB card reader off Ebay, it reads the number of the card OK. 0004370273 in the error messages above.

https://www.ebay.com/p/125khz-USB-RFID-Contactless-Proximity-Sensor-Smart-ID-Card-Reader-Em4100/501755355?iid=163732946714


 
Posted : 17/06/2019 9:15 pm
Posts: 14287
Free Member
 

I'm crap at this (I create programs in the same way you do!) but....if you've got 'something' that will read the card can you output the number to a text file?
I'd work on that first and once you can do that you can read the data from the file and use it to play the music.
(From there you could look at loading the number into a variable rather than the text file).

The card reader acts like a keyboard (as would a barcode scanner) - I'm wondering if it's a driver issue?


 
Posted : 17/06/2019 9:34 pm
Posts: 843
Free Member
Topic starter
 

This is what I just tried myself, when I scan the card it shows up in the program after 'else'?

a = '0004370273'
b = '0004385158'

input()

#print()

while input == a:
print("play playlist")

else:
print("error")

I'm going to drink beer and see if that helps!


 
Posted : 17/06/2019 9:40 pm
Posts: 31206
Full Member
 

This bit:

when I scan the card it shows up in the program after ‘else’?

Sounds like you are still editing the program, not actually running it?

I’m away from my PC at the mo by if no one else answers I’ll have a quick tinker tomorrow.


 
Posted : 17/06/2019 10:45 pm
Posts: 843
Free Member
Topic starter
 

Right, got this to run once. just need to get it looping now.

card = ("0004370273", "0004385158")

number = input()

if number in card:
print("play playlist")
print (number)

And this is the result in shell..

============== RESTART: /home/pi/python projects/cardreader5.py ==============
0004370273
play playlist
0004370273
>>>


 
Posted : 17/06/2019 10:46 pm
Posts: 843
Free Member
Topic starter
 

This seems to be doing the job...

card = ("0004370273", "0004385158")

while True:
number = input()

if number in card:

print("play playlist")
print (number)

I can now call the variable 'number' to play a specific playlist in VLC (hopefully!), I Just need to put some simple exit program code (CTRL-C) at the bottom, anyone?


 
Posted : 17/06/2019 11:57 pm
Posts: 31206
Full Member
 

Right I just had a quick hack.
All I have here is Python 2.7 and no card reader or Pi, but I think this will probably do what you want:

from collections import namedtuple

# Just defining a named type to use for the playlist details
SpotifyDetails = namedtuple("SpotifyDetails", "playlist_id title")

# This maps your Card Number to a Spotify Playlist. Add more here.
# Ideally you might want to read this from a seperate file.
card_to_details = {
    "0004370273" : SpotifyDetails("3PN3UBO1ciPi1HTA2gWnUt", "The SingletrackMTB Chain"),
    "0004385158" : SpotifyDetails("1P2J21og3wkly78gUQHAf2", "Chipps"),
    "0004666666" : SpotifyDetails("6yFLZ7fzVNEG44UgK8ltrL", "Mark")
    }

try:
    while True:
        card_number = raw_input("\nPlease scan your playlist card...\n")

        # exit if card_number is empty
        if not card_number:
            break
        
        # this just goes back to the start of the loop if the card is not recongised.
        if card_number not in card_to_details:
            print("Sorry card number '" + card_number + "' is not recognised.\n")
            continue

        # Join the playlist number onto the full URL.
        playlist_url = " https://open.spotify.com/playlist/"  + card_to_details[card_number].playlist_id

        # Output message
        print("\nCard accepted.")
        print("  Title: '" + card_to_details[card_number].title + "'")
        print("  URL:   " + playlist_url + "\n")

        # Presumably you want to do something with the URL here?

except KeyboardInterrupt:
    pass

print ("\nGoodbye.")

 
Posted : 18/06/2019 12:05 pm
Posts: 843
Free Member
Topic starter
 

GrahamS, thanks very much!
I'll have a look at it later, hopefully it'll give me a base to work from. I'm not linking to Spotify I'm trying to access my locally stored mp3's so I'll see if I can adapt it. I appreciate the help that you (and others) have given me.


 
Posted : 18/06/2019 12:39 pm
Posts: 31206
Full Member
 

No worries. Shout if you need more help. (I'm no Python guru but I can program and Google)

I’m not linking to Spotify

My bad, no idea where I got the idea you were. But it is fairly easy to change the mapping to whatever you want. If you don't need the title field then you can just do a straight dictionary of card number to playlist name/id:

card_to_details = {
    "0004370273" : "Dads_Best_of_the_80s"),
    "0004385158" : "Dire_Straits_Greatest_Hits"),
    "0004666666" : "Huey_Lewis_and_The_News_sheen_of_consomethinge_professionalism_that_really_gives_the_songs_a_big_boost"),
    }

Make sense?


 
Posted : 18/06/2019 12:48 pm
Posts: 843
Free Member
Topic starter
 

Haha, I can Google as well, I think that's what got me so confused!😂


 
Posted : 18/06/2019 12:52 pm