#!/usr/bin/python # Example using an RGB character LCD connected to an MCP23017 GPIO extender. import math import time import commands import os import sys import MySQLdb import Adafruit_CharLCD as LCD import Adafruit_GPIO.MCP230xx as MCP 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() def find_ip(): ips = commands.getoutput("/sbin/ifconfig | grep -i \"inet\" | grep -iv \"inet6\" | " + "awk {'print $2'} | sed -ne 's/addr\:/ /p' | grep -v 127 | sed 's/ //g'") return ips def find_host(): # Select qSQL with id=4. hostname = "Sensiplicity-SS-L1" cursor.execute("SELECT value FROM sensors_system WHERE name = 'server_hostname'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: hostname = row[0] else: hostname = commands.getoutput("/bin/hostname") return hostname # Define MCP pins connected to the LCD. lcd_rs = 7 # RS is actually connected to the RPi GPIO lcd_en = 0 #Swapped with d7 lcd_d4 = 1 lcd_d5 = 2 lcd_d6 = 3 lcd_d7 = 4 lcd_red = 5 lcd_green = 6 lcd_blue = 7 # Define LCD column and row size for 16x2 LCD. lcd_columns = 16 lcd_rows = 2 # Alternatively specify a 20x4 LCD. # lcd_columns = 20 # lcd_rows = 4 # Initialize MCP23017 device using its default 0x20 I2C address. #gpio = MCP.MCP23017() #gpio = MCP.MCP23008(address=0x20, busnum=1) gpio = MCP.MCP23008() #Initialize the LCD using the pins lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue, gpio=gpio) if (sys.argv[1] == "start"): #lcd.set_color(0.0, 1.0, 0.0) #green #lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue lcd.set_color(1.0, 1.0, 1.0) #white lcd.clear() ipaddress = find_ip() hostname = find_host() lcd.message(str(hostname) + "\n" + str(ipaddress)) time.sleep(3.0) if (sys.argv[1] == "message"): ############################################ # EXAMPLE: tbp_lcd_12c.py message whatsup ############################################ lcd.set_color(0.0, 1.0, 0.0) #green #lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue #lcd.set_color(1.0, 1.0, 1.0) #white lcd.clear() main_temp = "C" if (server_temperature == 'Fahrenheit'): main_temp = "F" lcd.message(str(sys.argv[2]) + "\n Temp: " + str(sys.argv[3]) + chr(223) + main_temp ) if (sys.argv[1] == "main"): ############################################ # EXAMPLE: tbp_lcd_12c.py main hot ############################################ lcd.set_color(0.0, 1.0, 0.0) #green #lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue #lcd.set_color(1.0, 1.0, 1.0) #white if (sys.argv[2] == "hot" or sys.argv[2] == "red"): lcd.set_color(1.0, 0.0, 0.0) #red elif (sys.argv[2] == "cold" or sys.argv[2] == "blue"): lcd.set_color(0.0, 0.0, 1.0) #blue lcd.clear() ipaddress = find_ip() hostname = find_host() lcd.message(str(hostname) + "\n" + str(ipaddress)) time.sleep(3.0) if (sys.argv[1] == "error"): ############################################ # EXAMPLE: tbp_lcd_12c.py error 12345678 70 hot ############################################ #lcd.set_color(0.0, 1.0, 0.0) #green lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue #lcd.set_color(1.0, 1.0, 1.0) #white if (sys.argv[4] == "hot" or sys.argv[4] == "red"): lcd.set_color(1.0, 0.0, 0.0) #red elif (sys.argv[4] == "cold" or sys.argv[4] == "blue"): lcd.set_color(0.0, 0.0, 1.0) #blue lcd.clear() main_temp = "C" if (server_temperature == 'Fahrenheit'): main_temp = "F" lcd.message(str(sys.argv[2]) + "\n ERROR: " + str(sys.argv[3]) + chr(223) + main_temp) if (sys.argv[1] == "alarm"): ############################################ # EXAMPLE: tbp_lcd_12c.py error 12345678 70 hot ############################################ #lcd.set_color(0.0, 1.0, 0.0) #green lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue #lcd.set_color(1.0, 1.0, 1.0) #white lcd.clear() #lcd.autoscroll(False) #lcd.autoscroll(True) lcd.message("ERROR:" + str(sys.argv[2]) + "\nPlease Respond") #lcd.blink(True) if (sys.argv[1] == "stop"): #lcd.set_color(0.0, 1.0, 0.0) #green #lcd.set_color(1.0, 0.0, 0.0) #red #lcd.set_color(0.0, 0.0, 1.0) #blue lcd.set_color(1.0, 1.0, 1.0) #white lcd.clear() lcd.message(" You can now \n Poweroff ") # white #lcd.message(" (o) (o) \n =________= ") # white commands.getoutput("/bin/rm ./c")