Demo entry 5064245

Auto Control Script with Minute

   

Submitted by anonymous on Jun 02, 2016 at 14:23
Language: Python. Code size: 2.4 kB.

#!/usr/bin/python
import MySQLdb, time, RPi.GPIO as GPIO, math

# Open database connection
db = MySQLdb.connect("localhost","root","raspberry","homedb" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
sql = "SELECT hour_s, min_s, hour_e, min_e, day_s, day_e  FROM  time3"
 
try:
   # First Loop
   while True:
      print "Start New Loop , Local Time is : ",time.strftime("%A - %H:%M:%S")
      # Set Local Time variables
      pidate = time.strftime("%a")	
      pihour = time.strftime("%H")
      pimin = time.strftime("%M")	
      hstemp, hetemp, mstemp, metemp, diffhs, diffms, diffhe, diffme = 0, 0, 0, 0, 0, 0, 0, 0 			
      # Execute the SQL command
      cursor.execute(sql)      	
      # Fetch all the rows in a list of lists.
      results = cursor.fetchall()	
      # Second Loop & Fetch data each row
      for row in results:
         hs = row[0]
         ms = row[1]
         he = row[2]
         me = row[3]
         ds = row[4]
         de = row[5]
	 print "Time start = ",hs,":",ms, "Time end = ",he,":",me, "Date Start :",ds, "Date End :",de  
         if pidate == ds or pidate == de or ds == "All" or de == "All":            
            hstemp = hs
            hetemp = he
            mstemp = ms
            metemp = me              
            diffhs = math.fabs(int(pihour) - int(hstemp))
            diffhe = math.fabs(int(pihour) - int(hetemp))
            diffms = math.fabs(int(pimin) - int(mstemp))
            diffme = math.fabs(int(pimin) - int(metemp))            
	    print "diffhs:",diffhs,"diffms:",diffms, " diffhe:",diffhe,"diffme:",diffme 	
            if diffhs <= diffhe :
               if diffhs == 0 and diffms == 0:
                  #Turn On
                  GPIO.setmode(GPIO.BOARD)
                  GPIO.setup(26,GPIO.OUT)
                  print "Turn on!!"
                  GPIO.output(26,GPIO.LOW)
            if diffhe <= diffhs:
               if diffhe == 0 and diffme == 0:
                  #Turn Off
                  GPIO.setmode(GPIO.BOARD)
                  GPIO.setup(26,GPIO.OUT)
                  print "Turn off!!"
                  GPIO.output(26,GPIO.HIGH)
      # delay time & loop every 50 sec   
      time.sleep(50)   
except:
   print "Error: unable to fecth data"

# disconnect from server
db.close()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).