User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Return a value from a function

Sun Jul 13, 2014 9:51 am

Hi
I have a problem with returning temperature value from inside a function. I'm reading it from DS18B20 sensor and setup was done using this tutorial: http://www.cl.cam.ac.uk/projects/raspbe ... mperature/
Here is my code:

Code: Select all

#!/usr/bin/python3

# import libraries
import time
import subprocess

# define sensor serial number
sensorSerNum = '28-00000329361f'

# helper functions to run modules needed for sensor communication
def runModules():
    subprocess.call(['modprobe', 'w1-gpio'])
    subprocess.call(['modprobe', 'w1-therm'])

# helper function to read temperature
def readTemp():
    try:
        sensorFile = open('/sys/bus/w1/devices/' + sensorSerNum + '/w1_slave')
        fileData = sensorFile.read()
        sensorFile.close()
        secondLine = fileData.splitlines()[1]
        rawTemp = secondLine.split()[9]
        temp = float(rawTemp[2:]) / 1000
        print(temp)
        return temp
    # if file cannot be opened
    except IOError:
        runModules()
        # give some time to load modules
        time.sleep(0.1)
        # make another attempt to read temperature
        readTemp()

print(readTemp())
To read the temperature value from this sensor you first need to load two modules. I don't want to load them automatically at system boot, but rather load them just when they are needed. So I used try - except statement to call modules when and error of accesing result file occurs - first time when program is being run after boot. But my issue is that even tho temperature reading is correct (I added print statement to confirm that), when I run program for the first time return statement gives output None:

Code: Select all

25.187
None
When I run this code for second time and there is no need to run modules (they are already loaded) I get what I expect:

Code: Select all

25.187
25.187
Can someone explain me this issue?

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Return a value from a function

Sun Jul 13, 2014 10:46 am

That's not going to work !

The program flow on first call is recursive, the exception handler in readTemp calls readTemp again, and this works as the modules are now loaded and prints the temp and returns the temperature, but that return goes back into the first call of readTemp which then returns None which is printed by the print statement .

All that is needed (I think) is to return the value from the second call from the exception handler in the first call.... See modified version below.... Note I've not tested it in any way!

However, if runModules fails to load the modules (for any reason) you program is going to enter a recursive loop until something runs out of memory... You need to add a bit more error checking to make sure the modules have loaded properly. Maybe run "lsmod" and check they are included in the output.

Code: Select all

def readTemp():
    try:
        sensorFile = open('/sys/bus/w1/devices/' + sensorSerNum + '/w1_slave')
        fileData = sensorFile.read()
        sensorFile.close()
        secondLine = fileData.splitlines()[1]
        rawTemp = secondLine.split()[9]
        temp = float(rawTemp[2:]) / 1000
        print(temp)
        return temp
    # if file cannot be opened
    except IOError:
        runModules()
        # give some time to load modules
        time.sleep(0.1)
        # make another attempt to read temperature
        t = readTemp()        # Changed
        return t                    # Addition

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Return a value from a function

Sun Jul 13, 2014 3:01 pm

Thanks, that solved my issue. But still I will need some more time to rethink why it doesn't work my way, thanks for the clue tho.

Return to “Python”