Skip to content

Instantly share code, notes, and snippets.

@shortstheory
Created February 20, 2020 16:10
Show Gist options
  • Select an option

  • Save shortstheory/28c2bc4f39bf9f62c404394aa44a963e to your computer and use it in GitHub Desktop.

Select an option

Save shortstheory/28c2bc4f39bf9f62c404394aa44a963e to your computer and use it in GitHub Desktop.

Revisions

  1. shortstheory created this gist Feb 20, 2020.
    33 changes: 33 additions & 0 deletions temp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import os
    import glob
    import time

    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
    temp_f = temp_c * 9.0 / 5.0 + 32.0
    return temp_c, temp_f

    def main():
    while True:
    c,f = read_temp()
    print("Temperature - %3.3f C, %3.3f F" % (c,f))
    time.sleep(1)
    if __name__== "__main__":
    main()