Advertisement
Guest User

Untitled

a guest
Feb 27th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import sys
  3. import Adafruit_DHT
  4. import time
  5. import pyodbc
  6. from mysql.connector import connect
  7.  
  8. DEMO = 1
  9.  
  10. GPIO.setmode(GPIO.BCM)
  11. pinList = [26, 13]
  12. manualPinList = [5, 6]
  13.  
  14. # loop through pins and set mode and state to 'off'
  15.  
  16. for i in pinList:
  17. GPIO.setup(i, GPIO.OUT)
  18. GPIO.output(i, GPIO.LOW)
  19.  
  20.  
  21. for j in manualPinList:
  22. GPIO.setup(j, GPIO.OUT)
  23. GPIO.output(j, GPIO.HIGH)
  24.  
  25. SERVER_CONNECTOR = ['']
  26.  
  27. def Connect_MYSQL():
  28.  
  29. SERVER = 'smartpanel.cywrzfld2wji.us-west-2.rds.amazonaws.com' #'r2ocontainers.cywrzfld2wji.us-west-2.rds.amazonaws.com'
  30. PORT = 3306 #1433
  31. DATABASE_NAME = 'smartpanel' #'SmartPanel'
  32. LOGIN = 'admin' #'r2oadmin'
  33. PASSWORD = 'smartpaneltest' #'pop90909'
  34.  
  35.  
  36. conn = connect(user=LOGIN, password=PASSWORD, database=DATABASE_NAME, port=PORT, host=SERVER)
  37. #conn = pyodbc.connect( 'DRIVER=FreeTDS;SERVER={};PORT={};DATABASE={};UID={};PWD={};'.format(SERVER, PORT, DATABASE_NAME, LOGIN, PASSWORD))
  38.  
  39. # conn = pyodbc.connect(
  40. # 'DRIVER={ODBC Driver 13 for SQL Server};SERVER=' + SERVER + ';DATABASE=' + DATABASE_NAME + ';UID=' + LOGIN + ';PWD=' + PASSWORD)
  41.  
  42. return conn
  43.  
  44.  
  45. def Read_from_db_table(conn):
  46. cursor = conn.cursor(buffered=True)
  47. cursor.execute("select mode, humidity, humidity_threshold, fan1, fan2 from demo where demo = {0}".format(DEMO))
  48. rows = cursor.fetchall()
  49. return rows[0]
  50.  
  51.  
  52. def main():
  53. conn = Connect_MYSQL()
  54. while True:
  55. #demo, mode, humidity, humidity_threshold, fan1, fan2 = Read_from_db_table(conn)
  56. mode, humidity, humidity_threshold, fan1, fan2 = Read_from_db_table(conn)
  57. #print(demo, mode, humidity, humidity_threshold, fan1, fan2)
  58. print(mode, humidity, humidity_threshold, fan1, fan2)
  59. # if logic, compare humidity to humidity_threshold
  60. if mode == 'logic':
  61. print "logic"
  62. # if 'humidity' > 'humidity_threshold' -> turn on both fans
  63. if humidity > humidity_threshold:
  64. print "humidity > threshold - turn both fans on"
  65. GPIO.output(26, True)
  66. GPIO.output(13, True)
  67. # if 'humidity' < 'humidity_threshold' -> turn off both fans
  68. else:
  69. print "humidity < threshold - turn both fans off"
  70. GPIO.output(26, False)
  71. GPIO.output(13, False)
  72. # if manual, read values from 'fan1_control' and 'fan2_control'
  73. elif mode == 'manual':
  74. print "manual"
  75. # if 'fan1_control' is high -> turn fan1 on
  76. if fan1 == '1':
  77. print "manual - fan 1 on"
  78. GPIO.output(26, True)
  79. else:
  80. print "manual - fan 1 off"
  81. GPIO.output(26, False)
  82. # if 'fan2_control' is high -> turn fan2 on
  83. if fan2 == '1':
  84. print "manual - fan 2 on"
  85. GPIO.output(13, True)
  86. else:
  87. print "manual - fan 2 off"
  88. GPIO.output(13, False)
  89. time.sleep(1)
  90.  
  91.  
  92. if __name__ == '__main__':
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement