Advertisement
alexarcan

project.py

Apr 19th, 2016
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. from time import sleep
  2. import os
  3. import httplib, urllib
  4. import RPi.GPIO as GPIO
  5. import subprocess
  6. import datetime
  7. import MySQLdb
  8. import socket
  9. import fcntl
  10. import struct
  11.  
  12.  
  13.  
  14. GPIO.setmode(GPIO.BOARD)
  15. GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  16. #GPIO.setup(23, GPIO.IN)
  17.  
  18. # setup variables
  19. count = 0
  20. up = False
  21. down = False
  22. command = ""
  23. filename = ""
  24. index = 0
  25. camera_pause = "500"
  26. command2=""
  27. db=MySQLdb.connect(host="localhost", user="root", passwd="raspberry", db="admin_pidoorbell")
  28. cur=db.cursor()
  29.  
  30.  
  31. hostname = socket.gethostname()
  32. address= socket.gethostbyname("%s.local" % hostname)
  33.  
  34. #def restart():
  35.         #print("Restarting... ")
  36.         #command2="sudo reboot"
  37.         #print(command2)
  38.         #os.system(command2)
  39.  
  40. def takepic(imageName):
  41.         print("click")
  42.         command = "sudo raspistill -o " + imageName
  43.         print(command)
  44.         os.system(command)
  45.  
  46. def PushOver(title,message,url):
  47.    application_token = "a9XubHhzgngExwtdAqvea3gm2ZhRUh"
  48.    user_token = "udSYHEqdgJ2kFz3UpdZLBkjAeXWuj6"
  49.    # Start your connection with the Pushover API server
  50.    conn = httplib.HTTPSConnection("api.pushover.net:443")
  51.  
  52.    # Send a POST request in urlencoded json
  53.    conn.request("POST", "/1/messages.json",
  54.    urllib.urlencode({
  55.    "token": application_token,
  56.    "user": user_token,
  57.    "title": title,
  58.    "message": message,
  59.    "url": url,
  60.    }), { "Content-type": "application/x-www-form-urlencoded" })
  61.  
  62.    # Listen for any error messages or other responses
  63.    conn.getresponse()
  64.  
  65. # Application specific variables
  66.  
  67. # PushOver('Doorbell','Started','')
  68. print 'Doorbell Server Started\r'
  69.  
  70. while True:
  71.         if (GPIO.input(18) == False):
  72.                 print 'Button Pushed!\r'
  73.  
  74.  
  75.                 #os.system('mpg321 -g 100 /home/pi/Ringtones/doorbell1.mp3 &')
  76.  
  77.                 now = datetime.datetime.now()
  78.                 timeString = now.strftime("%Y-%m-%d_%H_%M_%S")
  79.                 print("request received" + timeString)
  80.                 filename = timeString + '.jpg'
  81.  
  82.                 print "BUTTON DOWN PRESSED"
  83.                 takepic(filename)
  84.  
  85.                 #query="INSERT INTO images (id,url,date) VALUES (NULL," + filename  + ",NULL)"
  86.                 cur.execute("INSERT INTO images (id,url,date) VALUES (NULL,'" + filename  + "',NULL)")
  87.                 print("s-a inserat")
  88.                 cur.execute("SELECT * FROM images")
  89.                 #for reading in cur.fetchall():
  90.                 #       print str(reading[0])+" "+str(reading[1])+"     "+\
  91.                 #               str(reading[2])
  92.                 PushOver('PiDoorBell','Ding Dong! Someone is ringing at your door!','http://' + address + '/' + filename)
  93.                 print("pushed over!And filename= "+filename)
  94.                 #print query
  95.                 up = GPIO.input(18)
  96.                 count = count +1
  97.  
  98.  
  99.                 sleep(.1)
  100.         #if (GPIO.input(23)==False):
  101.                 #restart()
  102.  
  103. # this is never hit, but should be here to indicate if you plan on leaving the main loop
  104.  
  105. print "done"
  106. sleep(0.2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement