Skip to content

Instantly share code, notes, and snippets.

@shevabam
Created April 19, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shevabam/ea28cf053f7407bc33c2dbae3906f407 to your computer and use it in GitHub Desktop.
Save shevabam/ea28cf053f7407bc33c2dbae3906f407 to your computer and use it in GitHub Desktop.
Get value from DS18B20 sensors with Raspberry Pi
#!/usr/bin/python
# -*- coding: utf-8 -*-
import glob, os, sys
from datetime import datetime
def get_temp(device_file):
temp_c = 0
if os.path.isfile(device_file):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
if lines[0].strip()[-3:] == 'YES':
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
base_dir = '/sys/bus/w1/devices/'
devices_folder = glob.glob(base_dir + '28*')
for device_dir in devices_folder:
device = os.path.basename(device_dir)
temp = round(get_temp(device_dir+'/w1_slave'), 1)
print device+' : '+str(temp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment