Guest User

Untitled

a guest
Feb 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import os
  3. import datetime
  4. import pymysql.cursors
  5. from time import sleep
  6.  
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setup(18, GPIO.IN)
  9.  
  10. dbHost = "localhost"
  11. dbUser = "root"
  12. dbPassword = "XXXX"
  13. dbName = "weather"
  14.  
  15. def getFlag():
  16. connection = pymysql.connect(host=dbHost, user=dbUser, password=dbPassword, db=dbName)
  17.  
  18. try:
  19. with connection.cursor() as cursor:
  20. sql = "SELECT flag FROM notice LIMIT 1"
  21. cursor.execute(sql)
  22. result = cursor.fetchall()
  23. return result[0][0]
  24. except:
  25. print("Access error")
  26. finally:
  27. connection.close()
  28.  
  29. return None
  30.  
  31. def updateFlag():
  32. connection = pymysql.connect(host=dbHost, user=dbUser, password=dbPassword, db=dbName)
  33.  
  34. try:
  35. with connection.cursor() as cursor:
  36. sql = "UPDATE notice SET flag = 0"
  37. cursor.execute(sql)
  38. connection.commit()
  39. except:
  40. print("Access error")
  41. finally:
  42. connection.close()
  43.  
  44. try:
  45. while True:
  46. result = GPIO.input(18)
  47. if result == 1 and getFlag() is 1:
  48. responce = os.system('php weather.php')
  49. updateFlag()
  50.  
  51. print result
  52. sleep(0.5)
  53.  
  54. except KeyboardInterrupt:
  55. pass
  56.  
  57. GPIO.cleanup()
Add Comment
Please, Sign In to add comment