Skip to content

Instantly share code, notes, and snippets.

@aladagemre
Last active August 29, 2015 14:02
Show Gist options
  • Save aladagemre/1c86b15d6d05d591cc44 to your computer and use it in GitHub Desktop.
Save aladagemre/1c86b15d6d05d591cc44 to your computer and use it in GitHub Desktop.
Raspberrypy Temperature Logger
import time
import datetime
period = 1 # secs between each measurement
filename = "output.csv" # filename to write data.
def get_temperature():
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/10-000802824e58/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
return temperature
while True:
f = open(filename,"a")
temp = get_temperature()
d = datetime.datetime.now()
dt = "%s" % d.date()
tm = "%s" % d.time()
f.write("%s,%s,%s\n" % (dt, tm, temp))
time.sleep(period)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment