Hardware Reference
In-Depth Information
Listing 13-2 continued
subprocess.call(['modprobe', 'w1-gpio'])
subprocess.call(['modprobe', 'w1-therm'])
#Open the file that you viewed earlier so that Python can
#see what is in it. Replace the serial number with
#your own number
filename = “/sys/bus/w1/devices/28-0000040be5b6/w1_slave”
if (fileexists(filename)):
tfile = open(filename)
else:
return 0
# Read the w1_slave file into memory
text = tfile.read()
# Close the file
tfile.close()
# You are interested in the second line so this code will
# put the second line into the secondline variable
secondline = text.split(“\n”)[1]
# You are interested in the 10th word on the second line
temperaturedata = secondline.split(“ “)[9]
# You are interested in the number of the 10 word so
# you discard the first two letters “t=” and convert
# the remaining number $
temperature = float(temperaturedata[2:])
# Divide the value by 1000 to get the decimal in the
# right place
temperature = temperature / 1000
temp = float(temperature)
# Do the Farenheit conversion if required
if Fahrenheit:
temp=temp*1.8+32
temp = round(temp,2)
return(temp)
def main():
# This is the main routine of the program
print “The temperature is “ + str(GetTemperature())
if __name__ == “__main__”:
main()
Lastly, let's create some code that will monitor the temperature and send an e-mail when the
temperature exceeds a particular value. You will use the SendEmail function from the
Search WWH ::




Custom Search