Return a value from a function - Raspberry Pi Forums
hi
have problem returning temperature value inside function. i'm reading ds18b20 sensor , setup done using tutorial: http://www.cl.cam.ac.uk/projects/raspbe ... mperature/
here code: read temperature value sensor first need load 2 modules. don't want load them automatically @ system boot, rather load them when needed. used try - except statement call modules when , error of accesing result file occurs - first time when program being run after boot. issue tho temperature reading correct (i added print statement confirm that), when run program first time return statement gives output none: when run code second time , there no need run modules (they loaded) expect: can explain me issue?
have problem returning temperature value inside function. i'm reading ds18b20 sensor , setup done using tutorial: http://www.cl.cam.ac.uk/projects/raspbe ... mperature/
here code:
code: select all
#!/usr/bin/python3 # import libraries import time import subprocess # define sensor serial number sensorsernum = '28-00000329361f' # helper functions run modules needed sensor communication def runmodules(): subprocess.call(['modprobe', 'w1-gpio']) subprocess.call(['modprobe', 'w1-therm']) # helper function 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 opened except ioerror: runmodules() # give time load modules time.sleep(0.1) # make attempt read temperature readtemp() print(readtemp())
code: select all
25.187 none
code: select all
25.187 25.187
that's not going work !
program flow on first call recursive, exception handler in readtemp calls readtemp again, , works modules loaded , prints temp , returns temperature, return goes first call of readtemp returns none printed print statement .
needed (i think) return value second call exception handler in first call.... see modified version below.... note i've not tested in way!
however, if runmodules fails load modules (for reason) program going enter recursive loop until runs out of memory... need add bit more error checking make sure modules have loaded properly. maybe run "lsmod" , check included in output.
petero
program flow on first call recursive, exception handler in readtemp calls readtemp again, , works modules loaded , prints temp , returns temperature, return goes first call of readtemp returns none printed print statement .
needed (i think) return value second call exception handler in first call.... see modified version below.... note i've not tested in way!
however, if runmodules fails load modules (for reason) program going enter recursive loop until runs out of memory... need add bit more error checking make sure modules have loaded properly. maybe run "lsmod" , check included in 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 opened except ioerror: runmodules() # give time load modules time.sleep(0.1) # make attempt read temperature t = readtemp() # changed return t # addition
raspberrypi
Comments
Post a Comment