0

I have a python system script with various functions. I have multiple buttons hooked up to the Pi and I am using this code:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(22, GPIO.RISING, callback=manual_fill_pool)

What I want to be able to do is trigger this like this (from the command line)

/usr/local/bin/gpio -g write 22 0

But it does not work, I have tried both 0 and 1 and neither seems to trigger the event detection. I have tried RISING, FALLING, BOTH, GPIO.PUD_UP, DOWN...nothing seems to work.

I am basically looking for a way to programmatically trigger the event detection instead of using a switch.

Am I thinking about this the wrong way? Is the only way to trigger an event detection event to use a physical switch?

I should add that this is a RPi3, and that there are no wires connected to the pin at all, and that I am not local to the Pi to connect wires for testing. This is running my pool control system so running back and forth to my pool equipment room is not an easy way to test so I was hoping to be able to do it in software!

0

It is certainly possible as I test from the command line regularly, although I use (my) pigs rather than gpio.

The simplest way to check is to connect a wire from a spare GPIO to the GPIO under test and then use the command line utility. That way means you aren't trying to overwrite any GPIO settings made by the program under test. E.g. if you directly write to a GPIO it will be set to OUTPUT mode even though the program under test had set it as an INPUT.

In your case I would connect a wire between GPIO 22 and GPIO 23 (pins 15/16) and use the command

pigs w 23 1 # set (indirectly) 22 high
pigs w 23 0 # set (indirectly) 22 low

EDITED TO ADD

The following also works for me

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

def mfp(channel):
   print("mfp: channel={} is {}".format(channel, GPIO.input(22)))

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(22, GPIO.RISING, callback=mfp)

time.sleep(60)

In one window

$ pigs w 22 1 mils 500 w 22 0
$ pigs w 22 1 mils 500 w 22 0
$ pigs w 22 1 mils 500 w 22 0
$ pigs w 22 1 mils 500 w 22 0

In another window

$ ./q.py  
mfp: channel=22 is 1
mfp: channel=22 is 1
mfp: channel=22 is 1
mfp: channel=22 is 1
  • I am not local to the RPi so I cannot put a wire on the pins which (I agree) would be the easiest way to do it, so I wanted to do it via software. However the GPIO.add_event_detect does not respond to me setting the pin high or low via software so I was wondering what I was doing wrong. – Richard Feb 16 '17 at 15:10
  • I added another method which does alter the environment but does allow the software to be tested. – joan Feb 16 '17 at 15:25
  • Thanks @joan, I tried your small program and I still get nothing when I issue the [ /usr/local/bin/gpio -g write 22 0 ]. Do I need pigs installed to make this work? I assumed that pigs is just another method of accessing the GPIOs. Thanks – Richard Feb 16 '17 at 16:09
  • @Richard The gpio utility must be accessing the GPIO via sysfs. Try sudo pigpiod to see if pigpio is installed. If the daemon starts the pigs command should work. Otherwise sudo apt-get install pigpio python-pigpio python3-pigpio – joan Feb 16 '17 at 16:20
  • Thanks @joan, will pigpiod interfere with anything else on the Rpi? – Richard Feb 16 '17 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.