Help with Tempurature from ds18b20 on oled please?



  • I have a python app to take the temperature reading from a DS18B20 sensor and display it on a 0.96" Oled display. My only problem is I can't for the life of me figure out how to write the code to convert the temp from C. to F. I have the formula, just don't know where to use it. I have tried a few things and they don't work.

    I am including my code below. Thanks for any help...

    [code]
    #!/usr/bin/env python
    import os
    import glob
    import time
    from OmegaExpansion import oledExp
    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')[0]
    device_file = device_folder + '/w1_slave'

    def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    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

    	return temp_c
    

    status = oledExp.driverInit();
    status = oledExp.setDisplayMode(0);
    status = oledExp.scrollStop();

    status = oledExp.write(time.strftime("%Y-%m-%d %X")+ " Temp:"+ str(read_temp())+" C");
    [/code]



  • temp_f = (( temp_c * 9) / 5) + 32
    return temp_f



  • I see.... Use that instead of the return temp_c.?.?
    Didn't think of that one. I'll give it a shot.
    Thanks.



Looks like your connection to Community was lost, please wait while we try to reconnect.