Josh Garvey
Josh Garvey

Reputation: 55

My Python script quits after about 5 minutes from startup of RPI

I wrote a small python script that checks to see if something is connected to a specific IP address from my raspberry pi then changed the value of one of 3 GPIO pins to turn on LEDS for connected, not connected, or errors in connecting. The code runs on boot of the rpi and works perfectly for the first 4.5-5min then just gets stuck at whatever point in the code it was at.

#!/usr/bin/env python
# /etc/init.d/ping.py
### BEGIN INIT INFO
# Provides:     ping.py
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Start daemon at boot time
# Descripton:       Enable service provided by daemon.
### END INIT INFO

import subprocess
import RPi.GPIO as GPIO

# Set up pins, address, GPIO
address = '10.101.60.131'
grn = 8
red = 10
yel = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(grn, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(yel, GPIO.OUT)
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.LOW)

last = 5
# main loop, checks if network is there
while(1):
    ret = subprocess.call(['ping', '-c', '3', address])
    if (ret != last):
        # network responded correctly
        if ret == 0:
            print('ping to ' + address + ' OK')
            GPIO.output(grn, GPIO.HIGH)
            GPIO.output(red, GPIO.LOW)
            GPIO.output(yel, GPIO.LOW)
            last = 0
        # network was not located
        elif ret == 2:
            print('no response from ', address)
            GPIO.output(grn, GPIO.LOW)
            GPIO.output(red, GPIO.HIGH)
            GPIO.output(yel, GPIO.LOW)
            last = 2
        # other error in setup
        else:
            print('ping to ', address, ' failed')
            GPIO.output(grn, GPIO.LOW)
            GPIO.output(red, GPIO.LOW)
            GPIO.output(yel, GPIO.HIGH)
            last = 3

If i run the code outside of the boot (start it in the python IDLE) it runs fine. Let me know if theres more information needed.

Upvotes: 1

Views: 150

Answers (1)

Josh Garvey
Josh Garvey

Reputation: 55

The way I managed to fix this was to run the python code from /etc/profile. In /etc/profile at the end of the file I added sudo python /home/pi/Desktop/filenamehere.py & and then saved and rebooted.

Upvotes: 1

Related Questions