Last active
June 6, 2021 06:29
-
-
Save ogrew/c1205262bd3f3e4159ade9e1272b74ae to your computer and use it in GitHub Desktop.
Raspberry Piに繋いだNFCタグリーダーの結果をOSCで母機に送るスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nfc | |
import binascii | |
from pythonosc.udp_client import SimpleUDPClient | |
from pythonosc.osc_message_builder import OscMessageBuilder | |
# PC<->Raspberry Pi LAN (IP Address) | |
IP = "XXX.XXX.XXX.XXX" | |
PORT = 8400 | |
client = SimpleUDPClient(IP, PORT) | |
class MyNFCReader(object) : | |
def on_connect_nfc(self, tag): | |
print("【CONNECTED】") | |
print("TAG : " + tag) | |
# send rgb | |
msg = OscMessageBuilder(address = '/color') | |
id = binascii.hexlify(tag.identifier).upper() | |
id = id.decode() | |
print("ID : " + id) | |
if(id == "11111111AB"): | |
print("tag A attached!!") | |
msg.add_arg(255) | |
msg.add_arg(0) | |
msg.add_arg(0) | |
elif(id == "22222222CD"): | |
print("tag B attached!!") | |
msg.add_arg(0) | |
msg.add_arg(255) | |
msg.add_arg(0) | |
elif(id == "33333333ED"): | |
print("tag C attached!!") | |
msg.add_arg(0) | |
msg.add_arg(0) | |
msg.add_arg(255) | |
else: | |
print("【ERROR】 This is not our tag.") | |
msg.add_arg(0) | |
msg.add_arg(0) | |
msg.add_arg(0) | |
m = msg.build() | |
print("Sending... ", m.address, ":", m.params) | |
client.send(m) | |
return True | |
def read_id(self): | |
clf = nfc.ContactlessFrontend('usb') | |
try: | |
options = {'on-connect': self.on_connect_nfc} | |
clf.connect(rdwr=options) | |
finally: | |
clf.close() | |
if __name__ == '__main__': | |
cr = MyNFCReader() | |
while True: | |
print("Waiting for touch ...") | |
cr.read_id() | |
print("【RELEASED】") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment