Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import sys
- import Adafruit_DHT
- import time
- import pyodbc
- from mysql.connector import connect
- DEMO = 2
- GPIO.setmode(GPIO.BCM)
- pinList = [26, 13]
- manualPinList = [5, 6]
- # loop through pins and set mode and state to 'off'
- for i in pinList:
- GPIO.setup(i, GPIO.OUT)
- GPIO.output(i, GPIO.LOW)
- for j in manualPinList:
- GPIO.setup(j, GPIO.OUT)
- GPIO.output(j, GPIO.HIGH)
- SERVER_CONNECTOR = ['']
- def Connect_MYSQL():
- SERVER = 'digitechautomation.cywrzfld2wji.us-west-2.rds.amazonaws.com' #'r2ocontainers.cywrzfld2wji.us-west-2.rds.amazonaws.com'
- PORT = 3306 #1433
- DATABASE_NAME = 'CraigslistPosterTest' #'SmartPanel'
- LOGIN = 'admin' #'r2oadmin'
- PASSWORD = 'digitech123' #'pop90909'
- conn = connect(user=LOGIN, password=PASSWORD, database=DATABASE_NAME, port=PORT, host=SERVER)
- #conn = pyodbc.connect( 'DRIVER=FreeTDS;SERVER={};PORT={};DATABASE={};UID={};PWD={};'.format(SERVER, PORT, DATABASE_NAME, LOGIN, PASSWORD))
- # conn = pyodbc.connect(
- # 'DRIVER={ODBC Driver 13 for SQL Server};SERVER=' + SERVER + ';DATABASE=' + DATABASE_NAME + ';UID=' + LOGIN + ';PWD=' + PASSWORD)
- return conn
- def Read_from_db_table(conn):
- cursor = conn.cursor(buffered=True)
- #cursor.execute("select mode, humidity, humidity_threshold, fan1, fan2 from sp_demo where demo = {0}".format(DEMO))
- 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))
- rows = cursor.fetchall()
- return rows[0]
- def main():
- while True:
- conn = Connect_MYSQL()
- #demo, mode, humidity, humidity_threshold, fan1, fan2 = Read_from_db_table(conn)
- mode, humidity, humidity_threshold, fan1, fan2, timestamp = Read_from_db_table(conn)
- #print(demo, mode, humidity, humidity_threshold, fan1, fan2)
- print('**************')
- print('demo: %s' % (DEMO))
- print('mode: %s' % (mode))
- print('humidity: %s' % (humidity))
- print('humidity threshold: %s' % (humidity_threshold))
- print('fan 1: %s' % (fan1))
- print('fan 2: %s' % (fan2))
- print('timestamp: %s' % (timestamp))
- print('**************')
- #print(DEMO, mode, humidity, humidity_threshold, fan1, fan2)
- # if logic, compare humidity to humidity_threshold
- if mode == 'logic':
- #print "logic"
- # if 'humidity' > 'humidity_threshold' -> turn on both fans
- if humidity > humidity_threshold:
- print "humidity > threshold - turn both fans on"
- GPIO.output(26, True)
- GPIO.output(13, True)
- # if 'humidity' < 'humidity_threshold' -> turn off both fans
- else:
- print "humidity < threshold - turn both fans off"
- GPIO.output(26, False)
- GPIO.output(13, False)
- # if manual, read values from 'fan1_control' and 'fan2_control'
- elif mode == 'manual':
- #print "manual"
- # if 'fan1_control' is high -> turn fan1 on
- if fan1 == '1':
- print "manual - fan 1 on"
- GPIO.output(26, True)
- else:
- print "manual - fan 1 off"
- GPIO.output(26, False)
- # if 'fan2_control' is high -> turn fan2 on
- if fan2 == '1':
- print "manual - fan 2 on"
- GPIO.output(13, True)
- else:
- print "manual - fan 2 off"
- GPIO.output(13, False)
- time.sleep(1)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement