#!/usr/bin/python import RPi.GPIO as GPIO import time import glob import os import MySQLdb import time path = '/opt/sensiplicity' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/SS-L1.conf') # MySQL Connection db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() def temp_status(): # Select qSQL with id=4. server_temperature = "Fahrenheit" cursor.execute("SELECT value FROM sensors_system WHERE name = 'server_temperature'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: server_temperature = row[0] return server_temperature server_temperature = temp_status() # find the path of a sensor directory that starts with 28 i = 1 textdata = '' sensadata = '' status = 'white' devicelist = glob.glob('/sys/bus/w1/devices/*/w1_slave') date = time.strftime("%H:%M:%S") xmldata = '\n\n\n' csvdata = '"Sensor Name","Temperature","Date"\n' for sensor in devicelist: sensor_file = sensor sensor_id = sensor_file.split('/') # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: if (row[4] == "on" and row[5] == "yes"): sensor_data = '' # 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 main_temp = ctemperature if (server_temperature == 'Fahrenheit'): main_temp = ftemperature main_temp = float("{0:.2f}".format(main_temp)) name = row[1] if (row[3] != ''): name = row[3] if csvdata == '': csvdata = '"' + str(name) + '",' + repr(main_temp) + ',"' + date + '"\n' else: csvdata = csvdata + '"' + str(name) + '",' + repr(main_temp) + ',"' + date + '"\n' if xmldata == '': xmldata = '\n' else: xmldata = xmldata + '\n' if textdata == '': textdata = repr(name) + ";C=" + repr(ctemperature) + ";F=" + repr(ftemperature) else: textdata = textdata + "|" + repr(name) + ";C=" + repr(ctemperature) + ";F=" + repr(ftemperature) if sensadata == '': sensadata = repr(name) + "|" + repr(main_temp) else: sensadata = sensadata + "|" + repr(name) + "|" + repr(main_temp) if ((row[10] != "" and row[11] != "") or (row[12] != "" and row[13] != "")): low = row[10] if (row[12] != ''): low = row[12] high = row[11] if (row[13] != ''): high = row[13] #print "sensor = "+ str(name) + ", temp = "+ str(ftemperature) + ", high = "+high+", low ="+low+"" if main_temp > float(high): print "==========================================================================" print "= " print "= The sensor is with ID: \""+ str(name) + "\"" print '= The temperature in C: ' + repr(ctemperature) + '.' print '= The temperature in F: ' + repr(ftemperature) + '.' print "= " print '= This sensor is OVER TEMP!!!' print "= " print "==========================================================================" print " " status = 'hot' cmd = path + '/bin/tbpi_lcd_i2c.py error ' + str(name) + ' ' + str(main_temp) + ' hot' os.system(cmd) elif main_temp < float(low): print "==========================================================================" print "= " print "= The sensor is with ID: \""+ str(name) + "\"" print '= The temperature in C: ' + repr(ctemperature) + '.' print '= The temperature in F: ' + repr(ftemperature) + '.' print "= " print '= This sensor is UNDER TEMP!!!' print "= " print "==========================================================================" print " " status = 'cold' cmd = path + '/bin/tbpi_lcd_i2c.py error ' + str(name) + ' ' + str(main_temp) + ' cold' os.system(cmd) time.sleep(5) i = i + 1 xmldata = xmldata + '\n' file = open(path + "/www/temperature.xml", "w") file.writelines(xmldata) file.write("\n") file.close() file = open(path + "/www/temperature.csv", "w") file.writelines(csvdata) file.write("\n") file.close() file = open(path + "/www/temperature.txt", "w") file.writelines(textdata) file.write("\n") file.close() file = open(path + "/www/temp", "w") file.writelines(sensadata) file.write("\n") file.close() # find the path of a sensor directory that starts with 28 i = 1 devicelist = glob.glob('/sys/bus/w1/devices/*/state') date = time.strftime("%H:%M:%S") for sensor in devicelist: sensor_file = sensor sensor_id = sensor_file.split('/') # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: if (row[4] == "on" and row[5] == "yes" and row[13] == "alarm"): name = row[1] if (row[3] != ''): name = row[3] print "==========================================================================" print "= " print "= The E-Button with ID: \""+ str(name) + "\" is in ERROR STATE!!!" print "= " print "==========================================================================" print " " status = 'alarm' cmd = path + '/bin/tbpi_lcd_i2c.py alarm ' + str(name) + '' os.system(cmd) if (status == 'white'): cmd = path + '/bin/tbpi_lcd_i2c.py main ' + status os.system(cmd) #os.system("/bin/rm ./c")