I'm using a Raspberry pi model B 512MB with 3 HC-SR04 sonarsensors. I want the program to run for a couple of weeks none stop. I am doing it in three separate python-scripts. Every python-script is generating a csv-file with the data in it. I have made a script in /etc/init.d/ so the scripts will be starting when the raspberry pi boots. My problem is that after a few hours the scripts stop working. I can see the processes are still alive through the command: "ps aux". Here the process have a "R" in the STAT column.
Any idea why the scripts stop working? I have tried to log all exceptions with a try/except-loop, and the use of the library logging but it doesn't show anything.
I suspect that there is some Raspberry Pi configuration that i need to do to keep it running "forever" but i don't know.
EDIT: I have added my code for one of the sonarsensors below.
#!/usr/bin/python import sys import logging import RPi.GPIO as GPIO #Import GPIO library import time #Import time library import shutil import os import fcntl, socket, struct from os import path, mkdir import subprocess GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering def getHwAddr(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) return ':'.join(['%02x' % ord(char) for char in info[18:24]]) def checkDirectory(thePath): if (not path.isdir(thePath)): mkdir(thePath) MACAddress = getHwAddr('eth0') TRIG = 23 #Associate pin 23 to TRIG ECHO = 24 #Associate pin 24 to ECHO x = 0 antalMaalingerPrFil = 300 newdest = '/home/pi/Result' print "Distance Measurement Initiated" GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in GPIO.setwarnings(False) oldDistance = 0 distance_pic = 0 time_start = time.time() logging.basicConfig(level=logging.DEBUG, filename='/home/pi/failX.log') while True: try: import csv timestamp = time.strftime("_%H-%M-%S-%d-%m-%Y") filenameRange = "".join([MACAddress, timestamp,"_RangeX", ".csv"]) RangeFile = open(filenameRange, "wb") csv_writerRange = csv.writer(RangeFile, delimiter=";") GPIO.output(TRIG, False) #Set TRIG as LOW print "Waitng For Sensor To Settle" time.sleep(1) #Delay of 1 seconds while x<antalMaalingerPrFil: GPIO.output(TRIG, True) #Set TRIG as HIGH time.sleep(0.00001) #Delay of 0.00001 seconds GPIO.output(TRIG, False) #Set TRIG as LOW pulse_start = time.time() while GPIO.input(ECHO) == 0: #Check whether the ECHO is LOW pulse_start = time.time() #Saves the last known time of LOW pulse pulse_end = pulse_start timeUp = time.time() while GPIO.input(ECHO) == 1: #Check whether the ECHO is HIGH pulse_end = time.time() #Saves the last known time of HIGH pulse timeDown = time.time() timeOut = timeDown - timeUp if timeOut > 0.050: break pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance distance = round(distance, 2) #Round to two decimal points distance = distance - 0.5 timestamp_range = time.strftime('%T %d-%m-%Y') tmpRange = [timestamp_range] tmpRange.extend([distance]) csv_writerRange.writerows([tmpRange]) time.sleep(2) x += 1 RangeFile.close() checkDirectory(newdest) x = 0 source = os.listdir("/") destination = newdest for files in source: if files.endswith("RangeX.csv"): shutil.move(files,destination) except: logging.exception("Fail:")
And here is my script in /etc/init.d to run the script above from boot.
#!/bin/bash ### BEGIN INIT INFO # Provides: afstandsmaalerX # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: afstandsmaalerX at boot time # Description: afstandsmaalerX ### END INIT INFO case "$1" in start) echo "Starting sonarsensorX" /home/pi/codes/Data_Acquisition_System/Maalere/AfstandsmaalerX.py & ;; stop) echo "Stopping sonarsensor X" killall AfstandsmaalerX.py ;; *) echo "Wrong Command" exit 1 ;; esac exit 0
I have made the script executeable and I have ran the command: "sudo update-rc.d afstandsmaalerX defaults"
This is only the code for one of the sonarsensors, but the code is similar for the two others.