#!/usr/bin/python import sys import time import glob import os import commands import MySQLdb 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') 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/sensiplicity/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") #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/sensiplicity/rrds/' + str(row[1]) + '.rrd N:' + str(main_temp) os.system(rrdtool) front = '' if (row[7] == 'on|front'): sensor_plot = row[7] front = sensor_plot.split("|")[1] if (front == 'front'): rrdtool_data = rrdtool_data + ":" + str(main_temp) temp_symb = "C" if (server_temperature == 'Fahrenheit'): main_temp = ftemperature temp_symb = "F" timestamp = int(time.time()) cursor.execute("INSERT INTO `sensors`.`sensors_data` (`id`,`sid`,`timestamp`,`value`) VALUES (NULL,'"+str(row[0])+"','"+str(timestamp)+"','"+str(main_temp)+str(temp_symb)+"')") db.commit() 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") #row = cursor.fetchone() row = cursor.fetchall() for row in cursor: #print "row="+str(row) 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/sensiplicity/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]) #print "sensor_id="+str(sensor_id)+",sensor_bus="+str(sensor_bus)+",text="+str(text)+"" # 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,,, #Node_Typecode, Type_Description, Bus, Node_ID, Humidity_%, Ambient_Temp_C, Soil_Temp_Zone0_C, Soil_Temp_Zone1_C, Soil_Temp_Zone2_C, Soil_Temp_Zone3_C, Light_%, Idle_Voltage_V, Uptime_s, Measurement_Time_s, Number_of_Zones, Moisture_Zone0, Moisture_Zone1, Moisture_Zone2, Moisture_Zone3 #0X0004, "Soil Sensor, 4-zone", 3, 0x3D47FF756E6B6E6F77, 53.8, 21.81, 21.75, 21.62, 21.38, 21.38, 0.00, 4.72, 734, 733, 4, 3195, 2162, 1742, 2110 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[6]) temperature = float(csv_data[6]) atemp = float(csv_data[6]) stemp0 = float(csv_data[7]) stemp1 = float(csv_data[8]) stemp2 = float(csv_data[9]) stemp3 = float(csv_data[10]) light = float(csv_data[11]) if(csv_data[16]) != '': moist1 = float(csv_data[16]) else: moist1 = 0 if(csv_data[17]) != '': moist2 = float(csv_data[17]) else: moist2 = 0 if(csv_data[18]) != '': moist3 = float(csv_data[18]) else: moist3 = 0 if(csv_data[19]) != '': moist4 = float(csv_data[19]) else: moist4 = 0 #print "moist1="+str(moist1) #print "moist2="+str(moist2) #print "moist3="+str(moist3) #print "moist4="+str(moist4) #print "" # 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 ctemperature0 = stemp0 ftemperature0 = (stemp0 + 0) * 9/5 + 32 s_main_temp0 = ctemperature0 if (server_temperature == 'Fahrenheit'): s_main_temp0 = ftemperature0 ctemperature1 = stemp1 ftemperature1 = (stemp1 + 0) * 9/5 + 32 s_main_temp1 = ctemperature0 if (server_temperature == 'Fahrenheit'): s_main_temp1 = ftemperature1 ctemperature2 = stemp2 ftemperature2 = (stemp2 + 0) * 9/5 + 32 s_main_temp2 = ctemperature2 if (server_temperature == 'Fahrenheit'): s_main_temp2 = ftemperature2 ctemperature3 = stemp3 ftemperature3 = (stemp3 + 0) * 9/5 + 32 s_main_temp3 = ctemperature3 if (server_temperature == 'Fahrenheit'): s_main_temp3 = ftemperature3 # air temp, humidity, light sets rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_atemp.rrd N:' + str(a_main_temp) os.system(rrdtool) rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_humidity.rrd N:' + str(humidity) os.system(rrdtool) rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_light.rrd N:' + str(light) os.system(rrdtool) # The soil temp sets rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_stemp1.rrd N:' + str(s_main_temp0) os.system(rrdtool) if (s_main_temp1 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_stemp2.rrd N:' + str(s_main_temp1) os.system(rrdtool) if (s_main_temp2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_stemp3.rrd N:' + str(s_main_temp2) os.system(rrdtool) if (s_main_temp3 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_stemp4.rrd N:' + str(s_main_temp3) os.system(rrdtool) # The soil moist sets rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_moist1.rrd N:' + str(moist1) os.system(rrdtool) #print "rrdtool="+str(rrdtool) if (moist2 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_moist2.rrd N:' + str(moist2) os.system(rrdtool) #print "rrdtool="+str(rrdtool) if (moist3 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_moist3.rrd N:' + str(moist3) os.system(rrdtool) #print "rrdtool="+str(rrdtool) if (moist4 != ''): rrdtool = '/usr/bin/rrdtool update /opt/sensiplicity/rrds/' + str(row[1]) + '_moist4.rrd N:' + str(moist4) os.system(rrdtool) #print "rrdtool="+str(rrdtool) temp_symb = "C" if (server_temperature == 'Fahrenheit'): main_temp = ftemperature temp_symb = "F" timestamp = int(time.time()) soil_data = str(a_main_temp)+str(temp_symb)+':'+str(humidity)+':'+str(light)+':'+str(s_main_temp0)+str(temp_symb)+':'+str(s_main_temp1)+str(temp_symb)+':'+str(s_main_temp2)+str(temp_symb)+':'+str(s_main_temp3)+str(temp_symb)+':'+str(moist1)+':'+str(moist2)+':'+str(moist3)+':'+str(moist4) cursor.execute("INSERT INTO `sensors`.`sensors_data` (`id`,`sid`,`timestamp`,`value`) VALUES (NULL,'"+str(row[0])+"','"+str(timestamp)+"','"+str(soil_data)+"')") db.commit() front = '' if (row[7] == 'on|front'): sensor_plot = row[7] front = sensor_plot.split("|")[1] if (front == 'front'): rrdtool_data = rrdtool_data + ":" + str(a_main_temp) i = i + 1 continue cursor.close () db.close() rrdtool_output = commands.getoutput(rrdtool_data) #print "rrdtool_data="+str(rrdtool_data)+"" #print "rrdtool_output="+str(rrdtool_output)+"" commands.getoutput("/bin/rm ./c") sys.exit()