2

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.

EDIT:

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.

  • 1
    I would guess the pigpio daemon might have facilities for this kind of task, but I can't say for sure. What you are doing right now doesn't sound that bad in outline, but there is no way anyone can tell you what is wrong with the details unless you include the details. Think: If someone else described this problem to you, what would you want to look at in order to help them with it? – goldilocks Oct 19 '15 at 14:01
  • @goldilocks Strangely enough this is something I built into scripts. They have the wait and sys commands which would do what the questioner wants. Scripts are at the level of the programmable calculators of old. They can be very powereful, but to use that power needs some programming experience. – joan Oct 19 '15 at 18:09
  • @joan Wow, that is really nice -- I had no idea. All I've really done is I2C stuff, so I haven't made much use of any of the low level libs. But I have some piezos I've been meaning to fiddle with and I want to do some servo stuff soon, being able to experiment that way will be a pleasure :) – goldilocks Oct 19 '15 at 23:58
  • I edited the original post with some code and error logs. @goldilocks: I tried to update more details. I can include the rest of the code to if people think it might be helpful! @joan I have been trying to figure out how I can use what you mentioned! I was trying to use pigs proc <myscript.py> but couldnt get it to work properly. – cga007 Oct 20 '15 at 10:39
  • The first thing that leaps out to me is the busy loop at the end. That's crazy. It's going to completely hog 1 processor constantly for literally nothing (which is a very serious problem if this is not a Pi 2). You should replace pass with time.sleep(1). If it is a Pi 2, that's still not your problem, though. If you are monitoring this, you should watch htop (apt-get install htop first) to see if anything else weird is up, mem, cpu usage, or process spawning wise. The message about "Bits 55-60" in the pagemap is probably not a direct indication of a problem. – goldilocks Oct 20 '15 at 12:45
1

You need some kind of daemon for this: export the pin with interrupt, poll(2) on the pin and fork(2)/exec(3) the script when the pin go high and kill(2) it when the pin go low. Most of the time your daemon will be blocked by poll(2) so it will not hog CPU at all.

  • Its kind of what I am trying. Kindly take a look at my code that I posted. – cga007 Oct 20 '15 at 10:45

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.