Advertisement
Guest User

Untitled

a guest
May 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. import pymysql
  2. import serial
  3. import time
  4.  
  5. #Serial Constants
  6. port = "COM3"
  7. baud = 9600
  8.  
  9. #MySQL DB Constants
  10. hostname = '104.196.69.254'
  11. username = 'aquaponics'
  12. password = 'duck'
  13. database = 'Controller_1'
  14.  
  15. #Connects to MySQL DB Using Constants
  16. def connectToSQL():
  17.     print("Connecting to SQL...")
  18.     try:
  19.         c = pymysql.connect(host=hostname, user=username, password=password, db=database, charset='utf8',
  20.                         cursorclass=pymysql.cursors.DictCursor)
  21.         print("Connected to: " + str(c))
  22.         return c
  23.     except:
  24.         print("Error connecting to MySQL.")
  25.         return False
  26.  
  27. #Writes Temperature to MySQL DB Table 'temp"
  28. def writeTemp(t, c):
  29.     print("Attempting to write temp " + str(t) + " to " + str(c) + ".")
  30.     try:
  31.         with c.cursor() as cursor:
  32.             sql = "INSERT INTO `temp` (`celsius`) VALUES (%s)"
  33.             cursor.execute(sql, (t))
  34.         c.commit()
  35.         print("Temp wrote.")
  36.     except:
  37.         print("Failed to write to MySQL Database.")
  38.         return False
  39.  
  40. #Connects to Arduino from Serial
  41. def connectToArduino():
  42.     print("Connecting to arduino...")
  43.     try:
  44.         connection = serial.Serial(port, baud)
  45.         print("Connected to: " + str(connection))
  46.         return connection
  47.     except:
  48.         print("Failed to connect to arduino.")
  49.         return False
  50.  
  51. #Reads Arduino line from Serial
  52. def readArduino(a):
  53.     print("Reading arduino...")
  54.     try:
  55.         d = float(a.readline())
  56.         print("Read data: " + str(d))
  57.         return d
  58.     except:
  59.         print("Failed to read data from Arduino.")
  60.         return False
  61.  
  62. print("Init.")
  63. arduino = connectToArduino()
  64. SQL = connectToSQL()
  65. time.sleep(5)
  66.  
  67.  
  68. print("Loop.")
  69. while True:
  70.     temp = readArduino(arduino)
  71.     print(temp)
  72.     print(arduino)
  73.     print(SQL)
  74.     if readArduino(arduino) == False:
  75.         print("Failed to connect arduino, attempting reconnection in 5 seconds...")
  76.         time.sleep(5)
  77.         arduino = connectToArduino()
  78.     elif writeTemp(temp, SQL) == False:
  79.         print("Failed to connect to SQL, attempting reconnection in 5 seconds...")   #Possibly change this into a Try function. Right now this line also will write temp to MySQL and we dont want redundant data.
  80.         time.sleep(5)
  81.         SQL = connectToSQL()
  82.     else:
  83.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement