koicho

Untitled

May 12th, 2017
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import smtplib
  4. from email.mime.image import MIMEImage
  5. from email.mime.multipart import MIMEMultipart
  6. from requests import get
  7. from email.mime.text import MIMEText
  8. import os
  9. import subprocess
  10.  
  11.  
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  14.  
  15. ip = get('https://api.ipify.org').text
  16.  
  17. def send_mail():
  18. subject="FIRE!!!!!!"
  19. fstation="[email protected]"
  20. fs_pass="firestation123"
  21. msg = MIMEMultipart()
  22. msg['Subject'] = subject
  23. msg['From'] = fstation
  24. msg['To'] = user
  25. msg.preamble = subject
  26. msg_txt = ("<html>"
  27. "<head></head>"
  28. "<body>"
  29. "<h1> theres a fire </h1>"
  30. "<p>%s</p>"
  31. "</body>"
  32. "</html>" % ip)
  33. msg.attach(MIMEText(msg_txt, 'html'))
  34. with open("test.jpg") as f:
  35. msg.attach(MIMEImage(f.read()))
  36. s = smtplib.SMTP('smtp.gmail.com',587)
  37. print ("connection stablished")
  38. s.starttls()
  39. s.ehlo_or_helo_if_needed()
  40. s.login(fstation,fs_pass)
  41. s.sendmail(fstation, user, msg.as_string())
  42. s.quit()
  43. def filewriting():
  44. with open('info.txt', 'a') as file:
  45. file.write(time.strftime("%H:%M:%S")+' on '+time.strftime("%d/%m/%Y")+' ;')
  46. def my_callback (pin):
  47. print('Theres a fire with ip: {}'.format(ip))
  48. os.system("fswebcam -r 640x480 --jpeg 85 -D 1 test.jpg")
  49. send_mail()
  50. filewriting()
  51. os.system("rm -f -r {damn-fire.jpg}")
  52.  
  53. GPIO.add_event_detect(27, GPIO.RISING, callback=my_callback, bouncetime = 500)
  54.  
  55. while True :
  56. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment