#!/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 #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 #print "rrdtool_data="+str(rrdtool_data)+"" rrdtool_output = commands.getoutput(rrdtool_data) #print "rrdtool_output="+str(rrdtool_output)+"" # 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. # Node_Typecode, Type_Description, Bus, Node_ID, Humidity_%, Ambient_Temp_C, Soil_Temp_C, Light_%, Idle_Voltage_V, Sending_Voltage_V, Uptime_s, Measurement_Time_s, Number_of_Zones, Moisture_Zone0, Moisture_Zone1, Moisture_Zone2, Moisture_Zone3 #0X0001, "Soil Sensor, 1-zone", 3, 0x2625FF756E6B6E6F77, 58.0, 21.19, 21.31, 0, 4.81, 0.00, 940, 936, 1, 4392,,, 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)+"" humidity = float(csv_data[5]) temperature = float(csv_data[6]) atemp = float(csv_data[6]) stemp = float(csv_data[7]) light = float(csv_data[8]) if(csv_data[14]) != '': moist1 = float(csv_data[14]) else: moist1 = 0 if(csv_data[15]) != '': moist2 = float(csv_data[15]) else: moist2 = 0 if(csv_data[16]) != '': moist3 = float(csv_data[16]) else: moist3 = 0 if(csv_data[17]) != '': moist4 = float(csv_data[17]) else: moist4 = 0 # Put the decimal point in the right place and display it. ctemperature = atemp ftemperature = (atemp + 0) * 9/5 + 32 a_main_temp = ctemperature if (server_temperature == 'Fahrenheit'): a_main_temp = ftemperature ctemperature = stemp ftemperature = (stemp + 0) * 9/5 + 32 s_main_temp = ctemperature if (server_temperature == 'Fahrenheit'): s_main_temp = ftemperature #print "ID="+str(sensor_id)+",Temp="+str(main_temp)+"" rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_atemp.rrd N:' + str(a_main_temp) os.system(rrdtool) rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_stemp.rrd N:' + str(s_main_temp) os.system(rrdtool) rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_humidity.rrd N:' + str(humidity) os.system(rrdtool) rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_moist1.rrd N:' + str(moist1) os.system(rrdtool) if (moist2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_moist2.rrd N:' + str(moist2) os.system(rrdtool) if (moist2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_moist3.rrd N:' + str(moist3) os.system(rrdtool) if (moist2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_moist4.rrd N:' + str(moist4) os.system(rrdtool) if (moist2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/tempberrypi/rrds/' + str(row[1]) + '_light.rrd N:' + str(light) os.system(rrdtool) #rrdtool_data = rrdtool_data + ":" + str(main_temp) i = i + 1 continue cursor.close () db.close() commands.getoutput("/bin/rm ./c") sys.exit()