NFC Raspberry Pi Media Player Add New Movies Python Code
NFC Raspberry Pi Media Player
byLiz Clark andRuiz Brothers
published November 08, 2024, last edited February 28, 2025
Featured Productsview all
34
Intermediate
Project guide
Add New Movies Python Code
This project works by associating a movie file with an NFC card. Youcould manually type out the UID of each of your cards and movie titles, but a Python script helps to ease this process.
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries## SPDX-License-Identifier: MITimport csvimport osimport sysimport boardimport busiofrom digitalio import DigitalInOutfrom adafruit_pn532.spi import PN532_SPIspi = busio.SPI(board.SCK, board.MOSI, board.MISO)cs_pin = DigitalInOut(board.D24)pn532 = PN532_SPI(spi, cs_pin, debug=False)csv_file = "movies.csv"if not os.path.isfile(csv_file): with open(csv_file, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(['uid', 'movie'])def is_duplicate(uid, mov): with open(csv_file, mode='r') as f: reader = csv.DictReader(f) for row in reader: if row['uid'] == uid or row['movie'] == mov: return True return Falseic, ver, rev, support = pn532.firmware_versionprint("Found PN532 with firmware version: {0}.{1}".format(ver, rev))# Configure PN532 to communicate with MiFare cardspn532.SAM_configuration()print("Waiting for new RFID/NFC card...")while True: the_uid = pn532.read_passive_target(timeout=0.05) if the_uid is not None: uid_str = f"{[hex(i) for i in the_uid]}" movie = input("Enter the name of the new movie: ") movie = f"{movie}" if is_duplicate(uid_str, movie): print("UID {uid_str} or movie {movie} already exists, skipping.") else: with open(csv_file, mode='a', newline='') as file: writer = csv.writer(file) writer.writerow([uid_str, movie]) print(f"Added UID {uid_str} with movie title {movie} to movies.csv") sys.exit()
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries## SPDX-License-Identifier: MITimport csvimport osimport sysimport boardimport busiofrom digitalio import DigitalInOutfrom adafruit_pn532.spi import PN532_SPIspi = busio.SPI(board.SCK, board.MOSI, board.MISO)cs_pin = DigitalInOut(board.D24)pn532 = PN532_SPI(spi, cs_pin, debug=False)csv_file = "movies.csv"if not os.path.isfile(csv_file): with open(csv_file, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(['uid', 'movie'])def is_duplicate(uid, mov): with open(csv_file, mode='r') as f: reader = csv.DictReader(f) for row in reader: if row['uid'] == uid or row['movie'] == mov: return True return Falseic, ver, rev, support = pn532.firmware_versionprint("Found PN532 with firmware version: {0}.{1}".format(ver, rev))# Configure PN532 to communicate with MiFare cardspn532.SAM_configuration()print("Waiting for new RFID/NFC card...")while True: the_uid = pn532.read_passive_target(timeout=0.05) if the_uid is not None: uid_str = f"{[hex(i) for i in the_uid]}" movie = input("Enter the name of the new movie: ") movie = f"{movie}" if is_duplicate(uid_str, movie): print("UID {uid_str} or movie {movie} already exists, skipping.") else: with open(csv_file, mode='a', newline='') as file: writer = csv.writer(file) writer.writerow([uid_str, movie]) print(f"Added UID {uid_str} with movie title {movie} to movies.csv") sys.exit()
How the Script Works
You'll run the script in a terminal, separate from the main NFC player script. When the script starts, it will wait for an NFC card to be read. When a card is read, it will prompt you to enter a movie title. After you enter the title, the UID from the card and the title are added to themovies.csv file.
Page last edited January 21, 2025
Text editor powered bytinymce.