Advertisement
Guest User

Untitled

a guest
Feb 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 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 = 2
  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 = 'digitechautomation.cywrzfld2wji.us-west-2.rds.amazonaws.com' #'r2ocontainers.cywrzfld2wji.us-west-2.rds.amazonaws.com'
  30. PORT = 3306 #1433
  31. DATABASE_NAME = 'CraigslistPosterTest' #'SmartPanel'
  32. LOGIN = 'admin' #'r2oadmin'
  33. PASSWORD = 'digitech123' #'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 sp_demo where demo = {0}".format(DEMO))
  48. cursor.execute("SELECT b.mode, a.humidity, b.humidity_threshold, b.fan1, b.fan2, a.timestamp FROM CraigslistPosterTest.sp_demo_graph a INNER JOIN CraigslistPosterTest.sp_demo b ON a.sp_demo_id = b.demo WHERE demo = {0} ORDER BY a.timestamp DESC LIMIT 1".format(DEMO))
  49. rows = cursor.fetchall()
  50. return rows[0]
  51.  
  52.  
  53. def main():
  54. while True:
  55. conn = Connect_MYSQL()
  56. #demo, mode, humidity, humidity_threshold, fan1, fan2 = Read_from_db_table(conn)
  57. mode, humidity, humidity_threshold, fan1, fan2, timestamp = Read_from_db_table(conn)
  58. #print(demo, mode, humidity, humidity_threshold, fan1, fan2)
  59. print('**************')
  60. print('demo: %s' % (DEMO))
  61. print('mode: %s' % (mode))
  62. print('humidity: %s' % (humidity))
  63. print('humidity threshold: %s' % (humidity_threshold))
  64. print('fan 1: %s' % (fan1))
  65. print('fan 2: %s' % (fan2))
  66. print('timestamp: %s' % (timestamp))
  67. print('**************')
  68. #print(DEMO, mode, humidity, humidity_threshold, fan1, fan2)
  69. # if logic, compare humidity to humidity_threshold
  70. if mode == 'logic':
  71. #print "logic"
  72. # if 'humidity' > 'humidity_threshold' -> turn on both fans
  73. if humidity > humidity_threshold:
  74. print "humidity > threshold - turn both fans on"
  75. GPIO.output(26, True)
  76. GPIO.output(13, True)
  77. # if 'humidity' < 'humidity_threshold' -> turn off both fans
  78. else:
  79. print "humidity < threshold - turn both fans off"
  80. GPIO.output(26, False)
  81. GPIO.output(13, False)
  82. # if manual, read values from 'fan1_control' and 'fan2_control'
  83. elif mode == 'manual':
  84. #print "manual"
  85. # if 'fan1_control' is high -> turn fan1 on
  86. if fan1 == '1':
  87. print "manual - fan 1 on"
  88. GPIO.output(26, True)
  89. else:
  90. print "manual - fan 1 off"
  91. GPIO.output(26, False)
  92. # if 'fan2_control' is high -> turn fan2 on
  93. if fan2 == '1':
  94. print "manual - fan 2 on"
  95. GPIO.output(13, True)
  96. else:
  97. print "manual - fan 2 off"
  98. GPIO.output(13, False)
  99. time.sleep(1)
  100.  
  101.  
  102. if __name__ == '__main__':
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement