Jukebox. Reading NFC cards. Connecting all together
Today We are connecting the RC522 NFC-card reader.
NFC reader RC-522 we are using needs to be connected to the SPI bus on the Raspberry Pi.
| RC522 pin | Raspberry Pi pin | |
|---|---|---|
| SDA |
24 (SPI0 CE0, GPIO 8) |
|
| SCK |
23 (SPI0 SCLK, GPIO 11) |
|
| MOSI |
19 (SPI0 MOSI, GPIO 10) |
|
| MISO |
21 (SPI0 MISO, GPIO 9) |
|
| IRQ |
Not connected |
|
| GND |
20 (GND) |
|
| RST |
Not Connected |
|
| 3.3V | 17 (3v3 Power) |
Software
Make sure, the SPI interface is enabled on the Raspberry Pi. Start the "raspi-config" tool
sudo raspi-config
and there navigate to "Interface Options"->"SPI" and enable it.
We will use MFRC522 library to communicate with the RF522 reader, it can be easily installed:
pi@raspberrypi:~/rfid $ python3 -m pip install MFRC522
And write a simple script:
1 2 3 4 5 6 7 | from mfrc522 import SimpleMFRC522 reader = SimpleMFRC522() while True: id, data = reader.read() print(f"Got id: {id}, data: {data}") |
And now start it:
Playing songs
You see that cards have ID and could have some data written to it. We will simply use the id to distinguish different cards. And store association between cards and songs.
To play the songs from python we will use pygame library, just install it:
python3 -m pip install pygame
Now connecting all together we get the first version of our Jukebox
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 | import time from mfrc522 import SimpleMFRC522 import pygame SONGS = { 39881389544: "/home/pi/Motorbike - TrackTribe.mp3", 365993536163: "/home/pi/Night Run Away - An Jone.mp3" } reader = SimpleMFRC522() pygame.mixer.init() while True: id, data = reader.read() if id not in SONGS: print(f"Got unknown id: {id}") else: print(f"Playing {SONGS[id]}") pygame.mixer.music.load(SONGS[id]) pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): time.sleep(0.1) |
In my case, the sound volume from pygame was significantly low comparing to what I had from "omxplayer" prevously.
And I had to adjust volume by the command:
sudo amixer Headphones 0
Now let's play with it!
Great!
Whats next?
This is the last chapter, I planned for Jukebox topic.
Now we have all the parts, and can build a housing for the Jukebox, implement more sofisticated logic of switching songs (for example stop playing when the card is removed). Move the library to database.
I'm adding some of these features to my repo https://github.com/golovasteek/jukebox .
I'm adding some of these features to my repo https://github.com/golovasteek/jukebox .
Links
Blogpost I've used to figureout how to use the card reader https://pimylifeup.com/raspberry-pi-rfid-rc522/
The library for reading NFC cards https://github.com/pimylifeup/MFRC522-python
My fork of MFRC522 library, where I've fixed couple of issues https://github.com/golovasteek/MFRC522-python
The initial post with demo of my implementaion of housing and song-switching logic: http://blog.epetrov.net/2021/05/jukebox-intro-choosing-componets.html?m=1



Comments
Post a Comment