I need to continually monitor the 4 GPIO inputs as well as the RFID reader. The reader hooks in via USB and acts as a 9600 baud serial connection. I had that working well. I can also get the GPIO inputs working (not super reliably, but I'm thinking I forgot a resistor somewhere in my circuit). My problem is that I can't seem to get the python code reading it all in concert.
If I comment out all my code for the GPIO the serial works. If I leave the GPIO in place the serial doesn't work. Here's a code snippit around this section: (I am omitting some defs for brevity.)
Here's the thing . If the GPIO trips I need the code to do something, but when I have the RFID trip I need the same GPIO to be accessable to the serial code block. At one point I use a select statement to do a "hit this button in 60 seconds or fail" type of thing. Any help here would be greatly appreciated.
Code: Select all
import re, sys, signal, os, time, datetime
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import serial
import argparse
from gtts import gTTS
import requests
import json
import socket
from select import select
#Variables
BITRATE = 9600
# LED pin mapping.
red = 4
green = 5
blue = 6
#PhysicalSwitch
PhysicalSwitch = 19
#Glove dispensers
IRsensor1 = 18
IRsensor2 = 23
IRsensor3 = 20
IRsensor4 = 21
# GPIO Setup.
GPIO.setmode(GPIO.BCM)
#GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
# Clear all existing color values.
GPIO.output(red, 0)
GPIO.output(green, 0)
GPIO.output(blue, 0)
# Setup input pins
GPIO.setup(IRsensor1, GPIO.IN)
GPIO.setup(IRsensor2, GPIO.IN)
GPIO.setup(IRsensor3, GPIO.IN)
GPIO.setup(IRsensor4, GPIO.IN)
GPIO.setup(PhysicalSwitch, GPIO.IN)
# Set individual colors.
def setColor(color):
if color == 'red':
GPIO.output(red, 1)
elif color == 'green':
GPIO.output(green, 1)
elif color == 'blue':
GPIO.output(blue, 1)
elif color == 'white':
GPIO.output(blue, 1)
GPIO.output(green, 1)
GPIO.output(red, 1)
elif color == 'purple':
GPIO.output(blue, 1)
GPIO.output(red, 1)
elif color == 'yellow':
GPIO.output(green, 1)
GPIO.output(red, 1)
elif color == 'cyan':
GPIO.output(green, 1)
GPIO.output(blue, 1)
elif color == 'off':
GPIO.output(blue, 0)
GPIO.output(green, 0)
GPIO.output(red, 0)
def buttonPressed(channel):
if(channel==IRsensor1):
print "1"
elif(channel==IRsensor2):
print "2"
elif(channel==IRsensor3):
print "3"
elif(channel==IRsensor4):
print "4"
elif(channel==PhysicalSwitch):
print "phys"
GPIO.add_event_detect(IRsensor1, GPIO.FALLING, buttonPressed, bouncetime=2000)
GPIO.add_event_detect(IRsensor2, GPIO.FALLING, buttonPressed, bouncetime=2000)
GPIO.add_event_detect(IRsensor3, GPIO.FALLING, buttonPressed, bouncetime=2000)
GPIO.add_event_detect(IRsensor4, GPIO.FALLING, buttonPressed, bouncetime=2000)
GPIO.add_event_detect(PhysicalSwitch, GPIO.FALLING, buttonPressed, bouncetime=2000)
ser=serial.Serial('/dev/ttyUSB0', 9600)
ser.write("AT\r\n")
while True:
#wait for rfid card swipe
response = ser.readline()
trimmedResponse = re.sub('[^A-Za-z0-9]+', "", response)