Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #!/usr/bin/python
  2. import MySQLdb
  3. from serial import Serial
  4. import time
  5. from datetime import datetime
  6.  
  7. serialPort = Serial("/dev/ttyAMA0", 9600, timeout=1)
  8. if (serialPort.isOpen() == False):
  9. serialPort.open()
  10.  
  11.  
  12. serialPort.flushInput()
  13. serialPort.flushOutput()
  14.  
  15.  
  16. db = MySQLdb.connect("localhost", "root", "jfkeauve", "logs")
  17. #"/var/lib/mysql/logs")
  18. if (db == False):
  19. print("DB lost conn!")
  20. cursor = db.cursor()
  21.  
  22. pir1 = []
  23. pir2 = []
  24. while True:
  25. #Variable for date/Input in database
  26. inputdate = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  27.  
  28. #data = serialPort.readline()
  29. minValue_temp = 5
  30. maxValue_temp = 40
  31. minValue_press = 99000
  32. maxValue_press = 101000
  33. if(serialPort.inWaiting() != 0):
  34. checkpoint = serialPort.read(2)
  35. #Temperature
  36. if(checkpoint == 'TE'):
  37. checkpoint = serialPort.read(1)
  38. temperature = serialPort.read(3)
  39. temperature = float(temperature) * 0.1
  40. temperature = round(temperature, 1)
  41. if(checkpoint == 'B'):
  42. cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Board')", (1, inputdate, temperature, minValue_temp, maxValue_temp$
  43.  
  44. db.commit()
  45. if(checkpoint == 'K'):
  46. cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Kuhinja')", (1, inputdate, temperature, minValue_temp, maxValue_te$
  47. db.commit()
  48. #Pressure
  49. if(checkpoint == 'PR'):
  50. checkpoint = serialPort.read(1)
  51. pressure = serialPort.read(5)
  52. if(checkpoint == 'B'):
  53. cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Board')", (2, inputdate, pressure, minValue_press, maxValue_press,$
  54. db.commit()
  55. if(checkpoint == 'K'):
  56. cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Kuhinja')", (2, inputdate, pressure, minValue_press, maxValue_pres$
  57. db.commit()
  58. #PIR -> Motion sensor
  59. if(checkpoint == 'PI'):
  60. #time.sleep(1)
  61. checkpoint = serialPort.read(1)
  62. if(checkpoint == '1'):
  63.  
  64. #pir1 += datetime.now().strftime("%H:%M:%S")
  65. #start = (pir1[-8],pir1[-7],pir1[-6],pir1[-5],pir1[-4],pir1[-3],pir1[-2],pir1[-1])
  66. #finish = (pir1[0],pir1[1],pir1[2],pir1[3],pir1[4],pir1[5],pir1[6],pir1[7])
  67. cursor.execute("INSERT INTO alarms VALUES (%s, %s, 'Hodnik')", (1, inputdate,))
  68. db.commit()
  69. if(checkpoint == '2'):
  70. cursor.execute("INSERT INTO alarms VALUES (%s, %s, 'Boravak')", (2, inputdate,))
  71. db.commit()
  72.  
  73.  
  74.  
  75.  
  76. serialPort.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement