LoginSignup
1
2

More than 5 years have passed since last update.

switch LED by nfc(suica)

Last updated at Posted at 2017-04-17

install nfcpy

忘れたけどたぶん
pip install nfcpy
でインストール

say hello

say_hello.py
import nfc

def say_hello (tag):
    print('hello')

clf =  nfc.ContactlessFrontend('usb')
rdwr = {'on-connect': say_hello}

print('start')
clf.connect(rdwr=rdwr)
print('end')

で, 最初のテスト.
suicaを載せると"hello"と喋る.
一回きり.

Lチカ

Lチカに移る.
+は7番.
-はGND.

switch_led.py
import nfc
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)

GPIO.output(7, False)

def turn_led (tag):
    is_turn = GPIO.input(7)
    print(is_turn)
    if(is_turn):
        GPIO.output(7, False)
    else:
        GPIO.output(7, True)

    time.sleep(1)

clf = nfc.ContactlessFrontend('usb')

rdwr = {'on-connect':turn_led}

while True:
    print('waiting...')
    clf.connect(rdwr=rdwr)
    print('scaned')

suicaをタッチするたびにLEDがON/OFFする.

timeの1秒はタッチの反応が連続して動作しないようにするため.

IMG_3560.jpg

1
2
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
1
2