前提・実現したいこと
Raspberry PiとNFCリーダー、NFCタグを6面に貼ったキューブを使って、NFCのキューブの下にある面が変わった瞬間にGoogle Calendarに自分の行動(何時から何時まで勉強等)を記録するデバイスを作りたいと考えています。(IFTTTと連携)
一面だけの場合はできたのですが、6面になると前の方にあるコードが後ろ側のコードの変数を書き換えてしまうのでうまく作動しません。
該当のソースコード
python3
1 2#import 3import requests 4import datetime 5import pytz 6import binascii 7import nfc 8import os 9import time 10 11#set up 12before_idm = b'0000000' 13before = 0 14before_time = 0 15title = 0 16Status = 0 17time1 = 0 18time2 = 0 19 20#nfc-cubeが動いたときを感知してIFTTTに連携 21 22#問題箇所 23 24def cube(number,title_value): 25 global before 26 global Status 27 global before_time 28 global time1 29 global time2 30 global title 31 32 if before != number and Status == number: 33 print("【 Touched 】") 34 title = title_value 35 print(title) 36 print ("IFTTT連携開始") 37 before_time = time1 38 time1 = datetime.datetime.now(pytz.timezone('Asia/Tokyo')) 39#↑---この変数が他の関数cubeによって書き換えられてしまう 40 41 if before == number and Status != number: 42 time2 = datetime.datetime.now(pytz.timezone('Asia/Tokyo')) 43 payload = {"value1": time1, "value2": time2, "value3": title } 44 url = "〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇〇" 45 response = requests.post(url, data=payload) 46 print ("IFTTT連携終了") 47 48#nfcの読み取り 49 50class MyCardReader(object): 51 52 def on_connect(self, tag): 53 54 global Status 55 global before_idm 56 #IDmのみ取得して表示 57 self.idm = binascii.hexlify(tag._nfcid) 58 #print("IDm : " + str(self.idm)) 59 60 #特定のIDmだった場合のアクション 61 if before_idm != b'ec99c54f' and self.idm == b'ec99c54f': 62 Status = 1 63 64 if before_idm != b'9c2cc44f' and self.idm == b'9c2cc44f': 65 Status = 2 66 67 if before_idm != b'acf1c54f' and self.idm == b'acf1c54f': 68 Status = 3 69 70 if before_idm != b'6cf1c54f' and self.idm == b'6cf1c54f': 71 Status = 4 72 73 if before_idm != b'2c9ac54f' and self.idm == b'2c9ac54f': 74 Status = 5 75 76 if before_idm != b'dc2cc44f' and self.idm == b'dc2cc44f': 77 Status = 6 78 79 before_idm = self.idm 80 81 82 return True 83 84 def read_id(self): 85 clf = nfc.ContactlessFrontend('usb') 86 try: 87 clf.connect(rdwr={'on-connect': self.on_connect}) 88 finally: 89 clf.close() 90 91if __name__ == '__main__': 92 cr = MyCardReader() 93 while True: 94 #最初に表示 95 print("Please Touch") 96 97 #タッチ待ち 98 cr.read_id() 99 100 cube(1,'仕事') 101 102 cube(2,'休憩') 103 104 cube(3,'読書') 105 106 cube(4,'勉強') 107 108 cube(5,'料理') 109 110 cube(6,'余白') 111 112 113 before = Status
試したこと
time の部分を関数cubeの引数にしたりもしたのですがうまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
プログラミング自体が超初心者なのでコードが汚いですが、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。