Goal: build a simple mp3 player that a toddler can use, based on a raspberry pi.
A song is started by placing an NFC token on a sensor. Removing the token stops the music. That's all the user interface there is.
In my setup, the RasPi is hidden in a wooden box, that also contains a pair of tiny USB speakers. The NFC sensor is screwed to the inside of the lid. As tokens I use plastic poker chips. Each token plays one specific song. On one side I stick an NFC tag, on the other I draw a little icon symbolizing the song to be played by this token.
The music box is configured via a web interface, in which you can assign a song to a specific NFC tag.
Here's what I bought to build the player (no, I'm in no way affiliated with Amazon...):
- Neuftech Mifare RC522 RFC Reader
- Trust Leto 2.0 USB Speakers
- 20 NFC Tags Sticker NTAG213 Circus round 22mm 168Byte
- Some USB power supply, plus a USB extension cable
- Aukru 40x 20cm female-female jumper wire to wire the RFC reader to the RasPi
- Raspberry Board Pi 3 Model B
- Some SD Card (16 GB)
- Some RasPi Case
- A wooden box, about the size of a shoe box, from a DIY store
There are three ways documented below to provision your RPi:
- Fully-automatic build which generates a Rasbian-alike image with system packages installed and the RPi configured as a WIFI access point;
- Semi-automatic build, in case you already have a Pi running and just want to get the requirements installed properly on your Pi;
- Manual setup which gives you the bits necessary to configure your RPi.
What is left afterwards, though, is the setup of the nfcmusik service itself. See the section on the Software Setup for this purpose.
Recommended way of building nfcmusik is to use the build-image
make target.
make build-image
This target produces build/nfcmusik-rpi-arm.img
, ready to be flashed onto an SD card via
make flash
In order to make this work, you need to have the following tools installed:
- Vagrant in a recent version (tested with 2.2.5)
- Virtualbox in a recent version (tested with 6.0.12)
Suitable if you already have a Raspberry Pi running and accessible over ssh from within your network. It utilizes Ansible to configure your Pi as a Wifi Access Point, configures DHCP, and ensures the SPI (serial-parallel interface) is enabled:
ansible-playbook setup/ansible/nfcmusik.yml --inventory <rpi-ip>, --diff -e ansible_user=<rpi-user> --ask-pass
in case you authenticate via username/password (btw: yes, the comma is indeed intended and even required), or
ansible-playbook setup/ansible/nfcmusik.yml --inventory <rpi-ip>, --diff
in case of ssh-key-based authentication.
In order to make this work, you need to have the following tools installed:
- Python 3 (tested with Python 3.7)
- Ansible (tested with Ansible 2.8)
Configure the RPi to act as a WLAN access point on interface wlan0
.
See, for example, this site for instructions.
Note the static IP that you assign to the RasPi while configuring. This will be the address where
you can access the user interface (also see below).
The wlan0
interface is automatically shut down 3 minutes after startup, to avoid unneccessary 'radiation' and
to reduce interference in the speakers. Refreshing the home page of the user interface resets
the shutdown timer. After the access point has shut down, you would need to re-boot the RasPi to
reconnect (or connect to it via LAN).
The code was built and tested with Raspbian Jessie (2016-05-27).
Enable the SPI interface using sudo raspi-config
, then go to Advanced Options
and enable SPI.
Alternatively, you can also flash a hypriot image (Raspbian Buster including a working installation of docker) by use of the hypriot flash utility.
The configuration files used in this process are located under setup/image
.
To use it to flash an sd card, just run
make flash
and follow the prompts. No worries about enabling interfaces anymore. :)
Install these via sudo apt-get install <package>
, after doing sudo apt-get upgrade
- python-dev (required for building SPI driver)
Optional:
- vim
- ipython
After the build you still need to setup the nfcmusik service.
The easiest way to get all the packages you need is to use pipenv. If not already installed on your system, you can get it via
pip install pipenv
Once installed, you can get all the project's required packages nicely installed in a virtual env by executing the following command:
pipenv install --three
Finally, get into the virtual env via
pipenv shell
The RFID interface (rfid.py
) is based on pi-rc522, commit
6f5add08df29940bac15d3e9d98763fcc212ecc7
, with custom modifications.
This project was built and tested with NXP NTAG213 tags. Contrary to the examples
and default usage in rfid.py
, these do NOT require authentication
to be read or written. They also use 4-byte pages instead of 16-byte ones.
read
s return 4 pages (16 bytes) at a time, but writes write 4 bytes only.
rfid.py
can read 16 bytes fine, writes must happen with 16 bytes of data
of which only the first 4 are actually written.
ToDo: Fix rfid.py
to correctly handle 4-byte writes..
See the pi-rc522 page for instructions on how to connect the NFC reader to your RasPi. The RasPi pinout can be found here.
Copy mp3 files to the RasPi SD Card, adapt settings.py
to point to the correct MUSIC_ROOT
containing the mp3 files.
Copying can be done via scp
or by plugging the SD card into your PC/Mac.
Music files contained in MUSIC_ROOT
will be shown in the user interface and will
be playable by NFC tags.
Clone this git repo into a directory of your choice on the RasPi. Run python controller.py
to start.
See comment in controller.py
for how to autostart on reboot.
Open http://<RasPi IP or host name>:5000
to access NFC tag management.
Place tag on reader, click 'write to tag' besides one of the listed music files
to assign the file to the tag.