0

i have a function to read temperature sensors files and write it into db. However, sensors will not be connected all the time as they are portable. What may happen is that in the middle of script, a sensor is disconnected which currently causes that function stops and waits for sensor to be connected back. I want the function to run even with no sensors.

As the function is looping checking different sensors, i was thinking about skipping the loop or a function in it.

I have found something similar from here: Timeout function if it takes too long to finish This works perfectly except for one think, it breaks the function and stops the script.... I need it to run again instead. Thank you for your help.

Here is the code:

def read_all(): 

    base_dir = '/sys/bus/w1/devices/'
    sensors=['28-000006dcc43f', '28-000006de2bd7', '28-000006dc7ea9', '28-000006dd9d1f','28-000006de2bd8']

    for sensor in sensors:
        os.system('modprobe w1-gpio')
        os.system('modprobe w1-therm')

        device_folders = glob.glob(base_dir + sensor)
        if len (device_folders) == 0:
                continue
        device_folder = device_folders[0]
        if not os.path.isdir(base_dir):
                continue
        device_file = device_folder + '/w1_slave'
        if not os.path.isfile(device_file):
                continue
        @timeout(5):    
        def read_temp_raw():
            catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out,err = catdata.communicate()
            out_decode = out.decode('utf-8')
            lines = out_decode.split('\n')
            return lines
        def read_temp():
            lines = read_temp_raw()
            while lines[0].strip()[-3:] != 'YES':
                time.sleep(0.2)
                lines = read_temp_raw()
            equals_pos = lines[1].find('t=')
            if equals_pos != -1:
                temp_string = lines[1][equals_pos+2:]
                temp_c = float(temp_string) / 1000.0
                temp_f = temp_c * 9.0 / 5.0 + 32.0  
                return temp_c

        temp = read_temp()

        db=MySQLdb.connect("localhost","root","password","temps")
        curs=db.cursor()
        loggit= "insert into log1 values (current_date(), now(), %s, %s)"
        curs.execute (loggit, (temp, sensor))
        db.commit()        

        print temp, sensor  
        #print(read_temp()) 

while True:
    read_all()
    time.sleep(1)
Community
  • 1
  • 1
Miroslav
  • 33
  • 3
  • This is a follow-on question to http://stackoverflow.com/questions/32269775/python-raspberry-pi-if-path-doesnt-exist-skip-the-loop – PM 2Ring Aug 29 '15 at 08:23

1 Answers1

0

you are sending a signal to your process, which is not caught by your script.

try to catch and process the singal.

def alarm_received(signo, stack):
   print "signal caught {}".format(signo)

signal.signal(signal.SIGALRM, alarm_received)
arash javanmard
  • 1,362
  • 2
  • 17
  • 37