Skip to content

Instantly share code, notes, and snippets.

Created April 19, 2016 16:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Get value from DS18B20 sensors with Raspberry Pi
# -*- 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()
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