DarkKaplah
Posts: 3
Joined: Thu Sep 22, 2016 6:38 pm

GPIO and Serial device help!

Fri Sep 14, 2018 8:25 am

So I've got a bit of a unique case. I have a raspberry pi based sensor manager that I'm building. I'm using 3 GPIO pins for a RGB LED, 4 GPIO pins for input (with essentially an optical switch on each), and then an RFID card reader from sparkfun.

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)

Brandon92
Posts: 400
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: GPIO and Serial device help!

Fri Sep 14, 2018 7:28 pm

How did you connect those button and use you a pull-up or pull-down resistor? If you don't that resistor, you input pin is floating. And can have any value. This can trigger constant you interrupt routine.

MarkDH102
Posts: 268
Joined: Fri Feb 13, 2015 3:18 pm

Re: GPIO and Serial device help!

Sat Sep 15, 2018 6:31 am

Code: Select all

response = ser.readline()

This statement will not return to your code until it reads something.

You can put a timeout parameter in the call to setup the serial port.

I don't know if the add_event_detect buttonPress() function will still run while the call to readline() is blocking.

DarkKaplah
Posts: 3
Joined: Thu Sep 22, 2016 6:38 pm

Re: GPIO and Serial device help!

Sat Sep 15, 2018 9:41 pm

yep. That's what I'm seeing. I've written the code in smaller files and I can access the GPIO ports reliably, and I can access the serial port reliably. The problem is I can't seem to do both.

Is there a way to read the serial port in a event based manner? That would be ideal, but I don't see a way to accomplish this.

As for the switches I've used a pull down resistor. Like I said the code works well enough by itself. I'm just having an issue getting these working together.

MarkDH102
Posts: 268
Joined: Fri Feb 13, 2015 3:18 pm

Re: GPIO and Serial device help!

Sun Sep 16, 2018 6:30 am

I use

Code: Select all

import serial
import threading
import time
A timeout in the setup of the serial port

Code: Select all

serialGPS = serial.Serial("/dev/serial0", 115200, timeout = 0.5, rtscts = 0)
And then a function to read the port

Code: Select all

def readGPS():

    global _strGPS
    while True :
        strX = serialGPS.readline().decode("utf-8")
        fields = strX.split(",")
        # There is more code that I have omitted
        
        time.sleep(0.001)
        
And then I start the function running in its own thread

Code: Select all

# Get the reading the GPS thread started so we can discard non-lock condition until we start logging
t3 = threading.Thread(target=readGPS)
t3.daemon = True
t3.start()

In my limited knowledge there ism't an event driven solution available...

Return to “Python”

Who is online

Users browsing this forum: No registered users and 33 guests