mskutlu
Posts: 7
Joined: Mon Oct 26, 2015 11:43 am

python script stops after 3-4 hours run

Fri Dec 18, 2015 8:51 am

Hi,
I am trying to make a Tachometer with hc-sr04 proximity sensor. It is working normal for 3-4 hours and then it freezes. No error, no run, nothing, just standing still.

Here is my code,

Code: Select all

import RPi.GPIO as GPIO
import time
import pymssql

#GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)

TRIG = 18
ECHO = 16
relay = 22
sonzaman = time.time()
minute = 0
mesafe = 0
kontrol = 1
cnxn = pymssql.connect(server='**',user='**', password='**', database='**')
baslama = 1

def olcum(szaman):
        GPIO.setup(TRIG, GPIO.OUT)
        GPIO.setup(ECHO, GPIO.IN)

        GPIO.output(TRIG, False)

        time.sleep(1)

        GPIO.output(TRIG, True)
        time.sleep(0.00001)
        GPIO.output(TRIG, False)
#       pulse_start = time.time()
#       pulse_stop = time.time()
        while GPIO.input(ECHO) == 0:
                pulse_start = time.time()
           
        while GPIO.input(ECHO) == 1:
                pulse_stop = time.time()

        pulse_duration = pulse_stop - pulse_start

        distance = pulse_duration * 17000

        distance = round(distance, 2)

        devir = pulse_stop - szaman

        return (distance, pulse_stop)

try:
        while 1:
                if baslama == 1:
                        cursor3 = cnxn.cursor()
                        cursor3.execute("***")
                        baslama = 0
                tmpmesafe,zaman = olcum(sonzaman)
                if mesafe > 1.8 and mesafe < 5 and tmpmesafe > 5: # + 1.5 and tmpmesafe > mesafe - 1.5:
                        timediff = round(zaman - sonzaman,2)
                        minute = 60 / timediff
                        sonzaman = zaman
                        kontrol = 1
                        cursor = cnxn.cursor()
                        cursor.execute("******")
                        cnxn.commit()
                mesafe = tmpmesafe
#               GPIO.setup(relay, GPIO.OUT)
#               GPIO.output(relay, True)
                tmp = round(time.time() - sonzaman,2)
                if tmp > 50 and kontrol == 1:
#                       GPIO.output(relay,False)
                        time.sleep(1)
#                       GPIO.output(relay,True)
                        kontrol = 0
                        cursor2 = cnxn.cursor()
                        cursor2.execute("*****")
                        cnxn.commit()
                        baslama = 1
                        #print tmp      
                print "Mesafe:",mesafe,"cm, Devir:",minute,"tur/dk"  

except KeyboardInterrupt:
        GPIO.cleanup()

except Exception as e:
        logging.error('Hata :' + str(e))
        GPIO.cleanup()

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: python script stops after 3-4 hours run

Fri Dec 18, 2015 9:03 am

There are errors in the code you use to derive the distance from the HC-SR04. You probably picked the code up from the many sites which propagate the same error.

I've pointed out the nature of the error several times in posts on this and other forums. If you wait someone will tell you the sort of change you need to make.

mskutlu
Posts: 7
Joined: Mon Oct 26, 2015 11:43 am

Re: python script stops after 3-4 hours run

Fri Dec 18, 2015 9:31 am

Yes i picked up distance function from forums. I dont know how to work the sensor without this code. Could you just tell me errors in this code?

pete_l
Posts: 14
Joined: Thu May 17, 2012 7:08 am

Re: python script stops after 3-4 hours run

Mon Dec 21, 2015 11:13 am

I can't say if it's pertinent to your application hanging, but I wouldn't run code that waits indefinitely on an input state. All you need is to miss a (short) transition and your code will hang forever.
I'd suggest something like this:

Code: Select all

 
  measurement = 0
  GPIO.setup(PPin, GPIO.IN)
  while (GPIO.input(PPin) == GPIO.LOW):
    measurement = measurement +1
    if measurement > 100000:
      measurement = -1
      break


mskutlu
Posts: 7
Joined: Mon Oct 26, 2015 11:43 am

Re: python script stops after 3-4 hours run

Mon Dec 21, 2015 11:22 am

Thank you for your reply. I would definitely add something like you suggested. I also found that it happens when sensor returns more then 3.3v response and with a resistor between echo and pin seems solved my problem.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: python script stops after 3-4 hours run

Mon Dec 21, 2015 11:27 am

I'd second that. If the program is hanging then it's going to be either in a function you call -- but the gpio libraries are pretty robust -- or a while loop that doesn't terminate. There are two of those that are suspect, for example:

Code: Select all

        while GPIO.input(ECHO) == 0:
                pulse_start = time.time()
I would put something like:

Code: Select all

        started_looking = time.time()
       while GPIO.input(ECHO) == 0 and time.time() - started_looking < delay_at_max_range:
                pulse_start = time.time()

Return to “Python”