0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

結局

Last updated at Posted at 2019-06-24

ICカードで施錠 https://qiita.com/carpenders/items/94d29098127faaffe1fc

source code

#!/usr/bin/python
# -*- coding: utf-8 -*-

import binascii
import nfc
import pigpio
import time

PIN_INP = 17
PIN_PWM = 18

push = 0

def cb_up(gpio, level, tick):
	global push
	#print gpio, level, tick

def cb_down(gpio, level, tick):
	global push
	if push == 0:
		push = 1
		print "button", gpio, level, tick

def on_startup(target):
	print "on_startup"
	return target

def on_connect(tag):
	print "on_connect"
	print tag
	# return False # break connect
	return True # keep connect

def on_release(tag):
	print "on_release"

def connect(clf):
	clf.connect(rdwr={
		'on-startup': on_startup,
		'on-connect': on_connect,
		'on-release': on_release
	})
	print "connect end"

idlist = { \
	'xxxxxxxxxxxxxxxx': 'n', \
	'xxxxxxxxxxxxxxxx': 'y', \
	}

def unlock_lock(pi):
	w1 = 1000
	w2 = 2000
	pi.set_servo_pulsewidth(PIN_PWM, w2)
	time.sleep(1)
	pi.set_servo_pulsewidth(PIN_PWM, w1)
	time.sleep(1)
	pi.set_servo_pulsewidth(PIN_PWM, 0)

def sense(pi, clf):
	global push
	while True:
		target_req = nfc.clf.RemoteTarget("212F")
		target_res = clf.sense(target_req, iterations=1, interval=0.1)
		if target_res != None:
			#print "res", binascii.hexlify(target_res.sensf_res)
			tag = nfc.tag.activate_tt3(clf, target_res)
			#print "tag", tag
			idm = binascii.hexlify(tag.idm)
			pmm = binascii.hexlify(tag.pmm)
			#print "idm", idm, "pmm", pmm
			hit = 0
			yes = 0
			for k, y in idlist.items():
				if idm == k:
					hit = 1
					if y == 'y':
						yes = 1
					break
			if hit != 0 and yes != 0:
				print "accept", idm
				unlock_lock(pi)
			else:
				print "ignore", idm
				time.sleep(1)
		if push == 1:
			unlock_lock(pi)
			push = 0

def main():
	pi = pigpio.pi()
	pi.set_mode(PIN_INP, pigpio.INPUT)
	pi.set_pull_up_down(PIN_INP, pigpio.PUD_UP)
	cbu = pi.callback(PIN_INP, pigpio.RISING_EDGE, cb_up)
	cbd = pi.callback(PIN_INP, pigpio.FALLING_EDGE, cb_down)
	clf = nfc.ContactlessFrontend('usb')
	if clf:
		print clf
		try:
			sense(pi, clf)
		except KeyboardInterrupt:
			pass
		clf.close()
	pi.set_mode(PIN_PWM, pigpio.INPUT)
	pi.stop

if __name__ == '__main__':
	main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?