#!/usr/bin/python import sys import time import glob import os import commands import MySQLdb 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') def getColorFromFile(filename): import imp f = open(filename) global color color = f.readlines() f.close() getColorFromFile(path + '/etc/color_hex.txt') from random import randint colors = [] for i in range(10): colors.append('%06X' % randint(0, 0xFFFFFF)) # find the path of a sensor directory that starts with 28 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() i = 1 textdata = '' sensadata = '' rrdtool_data = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/sensortemp.rrd N' #devicelist = glob.glob('/sys/bus/w1/devices/28*') # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_state = 'on' AND sensor_type = 'Temperature' ORDER BY sid ASC") #print "data = "+str(data[1]) #row = cursor.fetchone() row = cursor.fetchall() for row in cursor: if (row[4] == "on" and row[5] == "yes"): sensor_id = row[1] sensor_file = "/sys/bus/w1/devices/"+sensor_id+"/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 main_temp = ctemperature if (server_temperature == 'Fahrenheit'): main_temp = ftemperature name = row[1] if (row[3] != ''): name = row[3] rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '.rrd N:' + str(main_temp) os.system(rrdtool) rrdtool_data = rrdtool_data + ":" + str(main_temp) i = i + 1 continue # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_state = 'on' AND sensor_type = 'Soil Sensor' ORDER BY sid ASC") #print "data = "+str(data[1]) #row = cursor.fetchone() row = cursor.fetchall() for row in cursor: if (row[4] == "on" and row[5] == "yes"): sensor_id = row[1] sensor_bus = row[2] # Read all of the text in the file. text_cmd = '/opt/tempberrypi/bin/sn-util-rpi get '+ str(sensor_bus) +' '+ str(sensor_id) +' | grep '+ str(sensor_id) +' | grep -v Node ' text_array = commands.getstatusoutput(text_cmd) text = str(text_array[1]) # Split the text with new lines (\n) and select the second line. # 0X0001, "Soil Sensor, 1-zone", 1, 0x2726FF756E6B6E6F77, 47.3, 23.19, 22.81, 21, 4.69, 0.00, 7751, 7751, 1, 4384,,, csv_data = text.split(',') # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). #print "csv="+str(csv_data)+"" temperature = float(csv_data[6]) # Put the decimal point in the right place and display it. ctemperature = temperature ftemperature = (temperature + 0) * 9/5 + 32 main_temp = ctemperature if (server_temperature == 'Fahrenheit'): main_temp = ftemperature name = row[1] if (row[3] != ''): name = row[3] print "ID="+str(sensor_id)+",Temp="+str(main_temp)+"" rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '.rrd N:' + str(main_temp) os.system(rrdtool) rrdtool_data = rrdtool_data + ":" + str(main_temp) i = i + 1 continue cursor.close () db.close() rrdtool_output = commands.getoutput(rrdtool_data) commands.getoutput("/bin/rm ./c") sys.exit()