Skip to content

Instantly share code, notes, and snippets.

@blacklight
Created December 27, 2018 10:54
Show Gist options
  • Save blacklight/08353a688bcd91c938d840dd70e866ee to your computer and use it in GitHub Desktop.
Save blacklight/08353a688bcd91c938d840dd70e866ee to your computer and use it in GitHub Desktop.
Read content from NFC tags abd dispatch it as requests to Platypush
import json
import logging
import nfc
import ndef
import sys
import time
from redis import Redis
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def on_connect(tag):
has_msg_records = False
tag_id = ':'.join([('%02x' % ord(c)) for c in tag.identifier])
logging.info('Tag ID {} connected'.format(tag_id))
if not tag or not tag.ndef:
return
for record in tag.ndef.records:
if isinstance(record, ndef.TextRecord):
try:
msg = json.loads(record.text)
has_msg_records = True
except:
continue
logging.info('Executing: {}'.format(record.text))
redis = Redis()
redis.rpush('platypush_bus_mq', msg)
return has_msg_records
while True:
with nfc.ContactlessFrontend('usb') as clf:
tag = clf.connect(
rdwr={'on-connect': on_connect}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment