-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfid-turntable-pi.py
More file actions
139 lines (108 loc) · 5.07 KB
/
Copy pathrfid-turntable-pi.py
File metadata and controls
139 lines (108 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# -*- coding: utf8 -*-
#This is the Raspberry Pi RFID turntable developed for the Johnson County Library.
#Complete tutorial at [COMING SOON]
#Original Arduino project at https://github.com/jocolibrarymakerspace/ListenLocal-turntable-arduino
#Links
#Johnson County Library - http://jocolibrary.org/
#Johnson County Library MakerSpace - https://www.jocolibrary.org/we-recommend/listen-local
#Listen Local - https://www.jocolibrary.org/we-recommend/listen-local
#This project requires:
#- SPI-Py installed from https://github.com/lthiery/SPI-Py
#- MFRC522-python from https://github.com/mxgxw/MFRC522-python
#- VLC-python bindings from https://github.com/oaubert/python-vlc
#This code is based off of:
#- the Read.py example included with MFRC522-python
#- the VLC Python binding examples at https://wiki.videolan.org/Python_bindings
#MFRC522 RFID reader wiring
#| Name | Pin # | Pin name |
#|------|-------|------------|
#| SDA | 24 | GPIO8 |
#| SCK | 23 | GPIO11 |
#| MOSI | 19 | GPIO10 |
#| MISO | 21 | GPIO9 |
#| IRQ | None | None |
#| GND | Any | Any Ground |
#| RST | 22 | GPIO25 |
#| 3.3V | 1 | 3V3 |
#Next up:
#-General cleanup for redundancies
#-RFID tag detection feature
#---Code begins below this line!---
#We import the GPIO module
import RPi.GPIO as GPIO
#We import the time module
import time
#We import the MFRC522 RFID reader module. Remember to copy MFRC522.py in the same folder as the turntable script!
import MFRC522
#We import the signal library
import signal
#We import the VLC python bindings library
import vlc
#We define the program state
continue_reading = True
# We capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print ("Ctrl+C captured, ending read.")
continue_reading = False
player.stop() #We stop VLC
GPIO.cleanup() #We clean up the GPIO data
# We hook the SIGINT data
signal.signal(signal.SIGINT, end_read)
# We create an object of the MFRC522 class
MIFAREReader = MFRC522.MFRC522()
#We create an object of the VLC class
player = vlc.MediaPlayer()
#We define the VLC playlist
#Add as many tracks as you need, just make sure to respect the formatting and put in the correct path!
#We use implied continuation of the list to make it easier to read.
playlist = ['/home/pi/01.mp3',
'/home/pi/02.mp3',
'/home/pi/03.mp3',
'/home/pi/04.mp3']
#Create lists to store and compare uids
uid = ()
justread = ()
# Welcome message
print "Welcome to the Listen Local RFID turntable"
print "Press Ctrl-C to stop."
# This loop keeps checking for RFID tags. If one is near it will get the UID.
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
#If there is no tag detected
if status == MIFAREReader.MI_NOTAG_ERR:
print("No tag detected")
#If we have a reader problem
elif status == MIFAREReader.MI_ERR:
print("Problem detected")
# If a tag is found
elif status == MIFAREReader.MI_OK:
print("Tag detected")
# We get the UID of the tag
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# We print the tag UID
#Alternatively you can display the tag UID with
#print ("Tag UID: " str(uid[0]) + str(uid[1] + str(uid[2] + str(uid[3])
print ("Tag UID: " +str(uid[0:4]))
#We compare the tag's 'UID with whatever we might have just read
if justread != uid:
#This is where the music playing begins!
if (uid[0]) == XXX and (uid[1]) == XXX: #Change the uid values to match your RFID tags
player.stop() #We stop playing any other track that might be playing
print("I'm playing track XX!") #We announce which track is about to be played
player = vlc.MediaPlayer(playlist[0]) #We request the track in the playlist list
player.play() #We start playing the track we just loaded
#Sample additional track playing block follows - uncomment and modify accordingly.
#Add as many blocks as you have tracks to play.
#elif (uid[0]) == XXX and (uid[1]) == XXX:
# player.stop() #We stop playing any other track that might be playing
# print("I'm playing track XX!") #We announce which track is about to be played
# player = vlc.MediaPlayer(playlist[X]) #We summon the corresponding track in the playlist list
# player.play() #We start playing the track we just loaded
#We store the card's uid into the justread variable for comparing later
justread = uid #We change the value of justread to store the latest tag UID we read
else:
print("You're already playing that track!") #We let the user know that the track is being played (And we don't try playing it from the top)
time.sleep(5) #If the same card is still on the reader, we take a 5 seconds break before polling for RFID again. This does not interrupt the track being played.