Skip to content

Instantly share code, notes, and snippets.

@ajmontag
Created May 24, 2015 15:33
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 ajmontag/27cdf40da2e8dff8e748 to your computer and use it in GitHub Desktop.
Save ajmontag/27cdf40da2e8dff8e748 to your computer and use it in GitHub Desktop.
# read temperature from DS18B20
# https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/hardware
# TODO command line args for time to run/num iterations
# TODO command line args for output file
# TODO run this on startup so I can run it with no network connection
import re
import datetime
import os
import time
rootDir = '/sys/bus/w1/devices/'
sensorDir = ""
for dirName, subdirList, fileList in os.walk(rootDir):
for subdir in subdirList:
if re.search("28-[\da-f]+", str(subdir)):
sensorDir = subdir
sensorReadFile = rootDir + sensorDir + "/w1_slave"
outputFile = open('temp-' + str(datetime.datetime.now().replace(microsecond=0)),'a+')
while True:
# read the sensor data
sensorReadResult = open(sensorReadFile, "rb").read()
# parse the output from the sensor file read
m = re.search(".* : crc=.. (\w+)\n.* t=(\d+)", sensorReadResult)
if not m or "YES" != m.groups()[0]:
raise sensorReadResult
degC = int(m.groups()[1]) / 1000.0
degF = degC * 9.0 / 5.0 + 32
#print "%.2f" % degF
line = str(datetime.datetime.now().replace(microsecond=0)) + "\t%.2f" % degF
print line
outputFile.write(line + '\n')
outputFile.flush()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment