Python වලින් rfid card එකක data කියවන්න

RadioShack

Junior member
  • Feb 13, 2024
    16
    19
    13
    මම raspberry PI එකක SPI bus එකට rfid reader එකක් සම්බන්ද කරලා Python rfid cards වල data කියවන්න code එකක් ලිව්වා.. මම While loop එකක් දාල තමයි program close එක වෙන්නේ නැතුව දිගටම card කියවන්න පුළුවන් විදියට හැදුවේ.. while loop එක ඇතුලේ තත්පර 5 ක thread sleep එකක් දැම්මා එක card එකක් reader ඇල්ලුවම ගොඩ පාරක් එකේම data කියවන එක නවත්වන්න.. එත් card 3 ක 4 ක සැරයක් error එකක් එනවා.. sleep එක අයින් කරාම නම් මේ error එක එන්නේ නෑ.. එත් මම තාම කියවන data වල history එකක් තියාගන්නේ නැති නිසා backend එකට එක යනවා..

    මේක error එක නැති කරගන්න ක්‍රමයක් තියනවද යාලුවනේ? මිට වඩා මේක කරන්න හොද ක්‍රම තියනවද?



    Python:
    #!/usr/bin/env python
    
    import time
    import RPi.GPIO as GPIO
    from mfrc522 import SimpleMFRC522
    import requests
    
    reader = SimpleMFRC522()
    
    try:
        while True:
            id, text = reader.read()
            print(id)
            print(text)
            # call the webservice and post data
            #......
            #
            print('Waiting for next card to read...')
            print('--')
            time.sleep(5)
    finally:
            GPIO.cleanup()


    error

    Bash:
    706556429817
    Hello World from card1                                 
    Waiting for next card to read...
    --
    706556429818
    Hello World from card2
    Waiting for next card to read...                                 
    --
    706556429817
    Hello World from card1
    Waiting for next card to read...                               
    --
    706556429818
    Hello World from card2
    Waiting for next card to read...                               
    --
    706556429817
    Hello World from card1
    Waiting for next card to read...                                 
    --
    Error while reading!
    706556429818
    
    Waiting for next card to read...
    --
     
    • Like
    Reactions: kinkon

    RadioShack

    Junior member
  • Feb 13, 2024
    16
    19
    13
    reading ehema delay dala karannepa,kauda danne a welwae oka read karaida kiyala,eeta wada hoda nadda card ekak available kiyana eka balana eka
    card එක තියනවද කියල බලන්න වෙන ක්‍රමයක් නැනේ.. reader.read() එක card එකක් ලං කරනකම් block වෙලා තියනවා
    repeatly එන same data filter කරන්න.
    sleep එක අයින් කරල
    කියවපු data වල history එක memory එකේ තියාගත්තොත් එක ටිකකින් clear කරන්න ඕන නේද අයෙත් පස්සේ වෙලාවක card එක කියවන එක අදුනගන්න ? එක තමයි මට කරන විදිය තේරෙන්නේ නැත්තේ මචං
     
    • Like
    Reactions: Sitting Bull

    Amith0424

    Well-known member
  • Oct 27, 2010
    14,243
    24,393
    113
    ℒ𝓸𝓬𝓪𝓽𝓲𝓸𝓷
    card එක තියනවද කියල බලන්න වෙන ක්‍රමයක් නැනේ.. reader.read() එක card එකක් ලං කරනකම් block වෙලා තියනවා

    කියවපු data වල history එක memory එකේ තියාගත්තොත් එක ටිකකින් clear කරන්න ඕන නේද අයෙත් පස්සේ වෙලාවක card එක කියවන එක අදුනගන්න ? එක තමයි මට කරන විදිය තේරෙන්නේ නැත්තේ මචං
    variable දෙකක් හදන්න එකක් ID අනෙක text එකට. read උනාට පස්සෙ ඔය data දෙක ඒ variable දෙකට assign කරන්න. if දාල අලුත් id & text compare කරල different නම් data pass කරන්න. different නම් අලුත් data අර variable දෙකටත් assign කරන්න.
    ඕන්නම් පොඩි time period එකක් ගියාම ඒ data clear කරන්න හදන්න. timestamp record එකක් අරන්.
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    අන්තිමට කියවපු අයිඩී එක වෙනම වේරියබල් එකක (last_id) තියා ගන්න. ඊටපස්සේ අයිඩී එක රීඩ් කරනකොට ලාස්ට් අයිඩී එකට කම්පෙයා කරලා අසමානනම් විතරක් ඩේටා පෝස්ට් කරන්න. ස්ලීප් එක අයින් කරන්න. පහලින් තියෙන්නේ වෙනස් කරපු කෝඩ් එක.

    Python:
    #!/usr/bin/env python
    
    import time
    import RPi.GPIO as GPIO
    from mfrc522 import SimpleMFRC522
    import requests
    
    reader = SimpleMFRC522()
    
    try:
        last_id = -1
        while True:
            id, text = reader.read()
            if id != last_id:
                    print(id)
                    print(text)
                    # call the webservice and post data
                    #......
                    #
                    print('Waiting for next card to read...')
                    print('--')
                    last_id = id
    finally:
            GPIO.cleanup()

    මම පයිතන් කරලා නෑ ඔයා දීපු විස්තරයට අනුව ලොජික් එක අනුමාන කරලා කිව්වේ.
     
    • Love
    Reactions: RadioShack

    RadioShack

    Junior member
  • Feb 13, 2024
    16
    19
    13
    variable දෙකක් හදන්න එකක් ID අනෙක text එකට. read උනාට පස්සෙ ඔය data දෙක ඒ variable දෙකට assign කරන්න. if දාල අලුත් id & text compare කරල different නම් data pass කරන්න. different නම් අලුත් data අර variable දෙකටත් assign කරන්න.
    ඕන්නම් පොඩි time period එකක් ගියාම ඒ data clear කරන්න හදන්න. timestamp record එකක් අරන්.
    අන්තිමට කියවපු අයිඩී එක වෙනම වේරියබල් එකක (last_id) තියා ගන්න. ඊටපස්සේ අයිඩී එක රීඩ් කරනකොට ලාස්ට් අයිඩී එකට කම්පෙයා කරලා අසමානනම් විතරක් ඩේටා පෝස්ට් කරන්න. ස්ලීප් එක අයින් කරන්න. පහලින් තියෙන්නේ වෙනස් කරපු කෝඩ් එක.

    Python:
    #!/usr/bin/env python
    
    import time
    import RPi.GPIO as GPIO
    from mfrc522 import SimpleMFRC522
    import requests
    
    reader = SimpleMFRC522()
    
    try:
        last_id = -1
        while True:
            id, text = reader.read()
            if id != last_id:
                    print(id)
                    print(text)
                    # call the webservice and post data
                    #......
                    #
                    print('Waiting for next card to read...')
                    print('--')
                    last_id = id
    finally:
            GPIO.cleanup()

    මම පයිතන් කරලා නෑ ඔයා දීපු විස්තරයට අනුව ලොජික් එක අනුමාන කරලා කිව්වේ.

    මේ විදියට කලාම හරිගියා.. එකම අවුල එකම card එක අතරමැද වෙන card එකක් කියවන්නේ නැතුව අයෙත් කියවන්න බැරි එක.. එත් එකදැනට අවුලක් නෙමෙයි මම POC එකක් විදියට කරන නිසා... ඔයාලට තැන්ක්ස්..
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    මේ විදියට කලාම හරිගියා.. එකම අවුල එකම card එක අතරමැද වෙන card එකක් කියවන්නේ නැතුව අයෙත් කියවන්න බැරි එක.. එත් එකදැනට අවුලක් නෙමෙයි මම POC එකක් විදියට කරන නිසා... ඔයාලට තැන්ක්ස්..

    ඒකට කරන්න තියෙන්නේ එකම කාඩ් එක ආයෙත් කියවන්න ඕනේ වෙලාවට මොකක් හරි කී එකක් ප්‍රෙස් කලාම last_id එක රීසෙට් කරන්න. පහලින් දාපු කෝඩ් එකේ R අකුර press කලාම last_id එක රීසෙට් වෙනවා. එතකොට එකම කාඩ් එක ආයේ කියවන්න පුලුවන්. පයිතන් keyboard module එක install කරලා ඉන්න.

    Python:
    #!/usr/bin/env python
    
    import time
    import RPi.GPIO as GPIO
    from mfrc522 import SimpleMFRC522
    import requests
    import keyboard
    
    reader = SimpleMFRC522()
    
    try:
        pressed_key_r = False
        last_id = -1
        while True:
            id, text = reader.read()
            if id != last_id:
                print(id)
                print(text)
                # call the webservice and post data
                #......
                #
                print('Waiting for next card to read...')
                print('--')
                last_id = id
            if keyboard.is_pressed('r'):
                if not pressed_key_r:
                    last_id = -1
                    pressed_key_r = True
            else:
                pressed_key_r = False
    finally:
            GPIO.cleanup()
     

    Amith0424

    Well-known member
  • Oct 27, 2010
    14,243
    24,393
    113
    ℒ𝓸𝓬𝓪𝓽𝓲𝓸𝓷
    මේ විදියට කලාම හරිගියා.. එකම අවුල එකම card එක අතරමැද වෙන card එකක් කියවන්නේ නැතුව අයෙත් කියවන්න බැරි එක.. එත් එකදැනට අවුලක් නෙමෙයි මම POC එකක් විදියට කරන නිසා... ඔයාලට තැන්ක්ස්..
    timestamp(tmp_TS) එකක් asign කරන්න තව variable එකක.
    එ timestamp එකට time period එකක් එකතු කරන්න. (5sec) ඒtimestamp එකට current time අඩුනම් && same value නම් reject. වැඩිනම් data pass කරන්න. මුල් (tmp_TS) timestamp එකට current time assign කරන්න.
     
    • Love
    Reactions: RadioShack

    RadioShack

    Junior member
  • Feb 13, 2024
    16
    19
    13
    ඒකට කරන්න තියෙන්නේ එකම කාඩ් එක ආයෙත් කියවන්න ඕනේ වෙලාවට මොකක් හරි කී එකක් ප්‍රෙස් කලාම last_id එක රීසෙට් කරන්න. පහලින් දාපු කෝඩ් එකේ R අකුර press කලාම last_id එක රීසෙට් වෙනවා. එතකොට එකම කාඩ් එක ආයේ කියවන්න පුලුවන්. පයිතන් keyboard module එක install කරලා ඉන්න.

    Python:
    #!/usr/bin/env python
    
    import time
    import RPi.GPIO as GPIO
    from mfrc522 import SimpleMFRC522
    import requests
    import keyboard
    
    reader = SimpleMFRC522()
    
    try:
        pressed_key_r = False
        last_id = -1
        while True:
            id, text = reader.read()
            if id != last_id:
                print(id)
                print(text)
                # call the webservice and post data
                #......
                #
                print('Waiting for next card to read...')
                print('--')
                last_id = id
            if keyboard.is_pressed('r'):
                if not pressed_key_r:
                    last_id = -1
                    pressed_key_r = True
            else:
                pressed_key_r = False
    finally:
            GPIO.cleanup()

    timestamp(tmp_TS) එකක් asign කරන්න තව variable එකක.
    එ timestamp එකට time period එකක් එකතු කරන්න. (5sec) ඒtimestamp එකට current time අඩුනම් && same value නම් reject. වැඩිනම් data pass කරන්න. මුල් (tmp_TS) timestamp එකට current time assign කරන්න.

    තැන්ක්ස් මචන්ලා idea වලට.. reader එක වැඩ නොකර ගිය එක පාරටම.. අලුත් එකක් ගත්නම try කරලා බලන්නම්
     
    • Like
    Reactions: Amith0424