I have the below script to read a serial port from an Arduino on a Raspberry Pi. The intent is to have the Pi monitor the Arduino rfid output and when a particular card number is identified, to activate two different relays on a relay board. What's happening is the script essentially runs twice when the particular card number is identified. I can't figure out why it's doing that.
#!/usr/bin/python # -*- coding: utf-8 -*-
import serial
import time
import RPi.GPIO as GPIO
ser = serial.Serial('/dev/ttyACM0', 9600)
GPIO.setmode(GPIO.BCM)
# init list with pin numbers
pin_assignments = {'Disarm Alarm': 18, 'Unlock Door': 23}
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.HIGH)
while True:
try:
data = ser.readline() .decode("utf-8*)
if "12 34 56 78" in data:
time.sleep(2)
GPIO.output(18, GPIO.LOW) # Disarm alarm
print('Alarm Disarmed')
time.sleep(1)
GPIO.output(23, GPIO.LOW) # Unlock door
print('Door Unlocked')
time.sleep(3)
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.HIGH)
print('Card Admitted')
time.sleep(1)
if data == 'no card select':continue
except ser.SerialTimeoutException:
print('Data could not be read')
time.sleep(1)
...On a valid card read, I'm getting:
Alarm Disarmed Door Unlocked Card Admitted Alarm Disarmed Door Unlocked Card Admitted
Why do you think it's running through twice?