I have a script that I want to run while a certain pin is high and exit once it goes low and restart when the pin goes high.
The solution I have currently implemented has the exit code in the main script. I have an interrupt setup and when the pin goes low it launches a secondary script and exits. The secondary script then waits for the pin to go high and then launches the main script before exiting. This creates a kind of infinite loop between the two.
Now this works, for a while (may be 2-3 iterations) but then my Raspberry Pi becomes unresponsive. I am currently accessing it via SSH (Putty/JuiceSSH). The SSH session stops responding and then I am forced to unplug/plug back in the system.
My guess is that this method is far from ideal and is causing a memory leak of sorts which leads to the pi shutting down. Now I haven't been able to test it (can I?!). I tried looking at the system logs around the time the pi crashes but cant see anything unusual.
I am really not even sure where the problem is let alone have a solution in mind! Any suggestions will be helpful!
PS: For context, the GPIO pin has a signal from the main power supply coming to it. Basically I want the main script to run when it is getting power from a charger and just idle when on battery.
PPS: I am doing my programming in python. So, I am using
popen to launch the main and secondary scripts from each other.
So I spent a while trying to figure this out today. I hooked up the RPi to my TV to see whats going on. At the time my SSH session was crashing the whole system is going haywire. It starts displaying garbage on the screen. I think I managed to get some logs around the time it crashes and I think the problem is somewhere here
Oct 20 10:07:34 raspberrypi kernel: [ 678.920168] gpiomem-bcm2835 20200000.gpiomem: gpiomem device opened. Oct 20 10:08:02 raspberrypi kernel: [ 707.078654] gpiomem-bcm2835 20200000.gpiomem: gpiomem device opened. Oct 20 10:08:02 raspberrypi kernel: [ 707.515301] Bits 55-60 of /proc/PID/pagemap entries are about to stop being page-shift some time soon. See the linux/Documentation/vm/pagemap.txt for details. Oct 20 10:08:10 raspberrypi kernel: [ 715.085408] gpiomem-bcm2835 20200000.gpiomem: gpiomem device opened. Oct 20 10:09:26 raspberrypi kernel: [ 791.403799] gpiomem-bcm2835 20200000.gpiomem: gpiomem device opened. Oct 20 10:09:46 raspberrypi kernel: [ 811.613380] gpiomem-bcm2835 20200000.gpiomem: gpiomem device opened.
I can see the same thing in kern.log, syslog and messages.
Right now I am using the watchdog timer to reboot the RPi when it hangs suddenly. Here is the control script I am using
import RPi.GPIO as GPIO import subprocess import commands import time GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN) def start_stop_radio(channel): print 'here' power_mode=GPIO.input(22) print ('POWER = ',power_mode) if power_mode and ('radio' not in commands.getoutput('ps aux')): print 'Power on + no radio' print 'Starting radio' subprocess.Popen(["python","/home/pi/PiFm/radio.py"]) elif (not power_mode) and ('radio' in commands.getoutput('ps aux')): print 'Power off + radio on' print 'Stopping radio' subprocess.call(['pkill','-9','-f','radio']) subprocess.call(['pkill','-9','sox']) subprocess.call(['pkill','-9','pifm']) GPIO.add_event_detect(22,GPIO.BOTH,callback=start_stop_radio,bouncetime=1000) try: while True: pass except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit
The main script is kind of big but if people think it may be helpful I can post that too.