#!/usr/bin/python import RPi.GPIO as GPIO import time import glob import os path = '/opt/tempberrypi' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/TBp1.conf') # blinking function def blink(pin): GPIO.output(pin,GPIO.HIGH) time.sleep(1) GPIO.output(pin,GPIO.LOW) time.sleep(1) return # find the path of a sensor directory that starts with 28 i = 1 textdata = '' sensadata = '' devicelist = glob.glob('/sys/bus/w1/devices/28*') for sensor in devicelist: print "==========================================================================" print "= " print "= The sensor is located at %s" % sensor + "/w1_slave" sensor_file = sensor + "/w1_slave" # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. tfile = open(sensor_file) # 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. ctemperature = temperature / 1000 ftemperature = (temperature/1000 + 0) * 9/5 + 32 print '= The temperature in C: ' + repr(ctemperature) + '.' print '= The temperature in F: ' + repr(ftemperature) + '.' if textdata == '': textdata = repr(i) + ";C=" + repr(ctemperature) + ";F=" + repr(ftemperature) else: textdata = textdata + "|" + repr(i) + ";C=" + repr(ctemperature) + ";F=" + repr(ftemperature) if sensadata == '': sensadata = repr(i) + "|" + repr(ctemperature) else: sensadata = sensadata + "|" + repr(i) + "|" + repr(ctemperature) # to use Raspberry Pi board pin numbers GPIO.setmode(GPIO.BOARD) # blink GPIO17 50 times if ftemperature > data.high: print "= " print '= This sensor is OVER TEMP!!!' # set up GPIO output channel GPIO.setup(11, GPIO.OUT) for j in range(0,i): blink(11) GPIO.cleanup() #GPIO.setmode(GPIO.BOARD) if ftemperature < data.low: print "= " print '= This sensor is UNDER TEMP!!!' GPIO.setup(13, GPIO.OUT) for j in range(0,i): blink(13) GPIO.cleanup() print "= " print "==========================================================================" print " " time.sleep(2) i = i + 1 file = open("/var/www/temperature.txt", "w") file.writelines(textdata) file.write("\n") file.close() file = open("/var/www/temp", "w") file.writelines(sensadata) file.write("\n") file.close() commands.getoutput("/bin/rm ./c")