Help us understand the problem. What is going on with this article?

Google HomeにSuicaの残高を喋らせる

More than 1 year has passed since last update.

Google HomeでSuicaの残高を読み上げてもらうよ!

nfcpyでSuicaの残高を出力するスクリプトを書いていたら思いついたのでやってみた。
と思ったら、すでに3年前にC言語での実装があった。
ラズベリーパイがICカードの残高をしゃべるよ

用意するもの

  • Google Home
  • Raspberry Pi
  • ICカードR/W(SonyのRC-S380推奨)
  • (あると便利)スピーカー

仕組み

Raspberry PiにICカードR/Wをつなげておいて、ICカードR/WにカードがタッチされたらGoogle Homeが残高を読み上げる。

プログラム

読み上げてもらうためにgoogle-home-notifierを使う。

導入方法は下記の通り。
GoogleHomeスピーカーに外部からプッシュして自発的に話してもらいます

残高の読み取り

必要なライブラリなど

sudo apt install python-usb
sudo pip install libusb1
sudo pip install pyserial
sudo pip install nfcpy
zandaka_yomiage.py
# -*- coding: UTF-8 -*-
import nfc
import requests
import struct
import subprocess

service_code = 0x090f

def on_connect(tag):
    sc = nfc.tag.tt3.ServiceCode(service_code >> 6, service_code & 0x3f)
    bc = nfc.tag.tt3.BlockCode(0, service=0)
    be = struct.unpack('>2B2H4BH4B', bytes(tag.read_without_encryption([sc],[bc])))
    le = struct.unpack('<2B2H4BH4B', bytes(tag.read_without_encryption([sc],[bc])))
    year = (be[3] >> 9) & 0x7f
    month = (be[3] >> 5) & 0x0f
    day = (be[3] >> 0) & 0x1f
    # カードが正常に認識されたらピッと音をRaspberry Piに繋いだスピーカーから鳴らす
    subprocess.call("aplay pi.wav", shell=True)
    lastdofuse = "20%02d年%02d月%02d日" % (year, month, day)
    balance = le[8]
    print("最終利用日 %s 残高%d円" % (lastdofuse, balance))

    data = [("text","残高は%d円です。最終利用日は%sです。" % (balance, lastdofuse)),]
    # google-home-notifierが同じRaspberry Pi内で動いているので、localhostでイケる。
    req = requests.post('http://localhost:3000/google-home-notifier',data=data)

def cardRead():
    clf = nfc.ContactlessFrontend('usb')
    try:
        clf.connect(rdwr={'on-connect': on_connect})
    finally:
        clf.close()

def main():
    try:
        while True:
            cardRead()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()

動作確認

a_halka
羽田空港第2ターミナル50番台スポットから手荷物受け取りカウンターまでウォーキングの会主宰
https://cf6.in/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした