guowenxue
2019-03-25 ed778b78c3ee7cb25aecb0d70efd66239662c44c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
#-*- coding: utf-8 -*-
 
import pygame
import pygame.camera
import time
import signal
import RPi.GPIO as GPIO
 
from pygame.locals import *
from ftplib import FTP
 
ON=1
OFF=0
 
# I/O Pin connected to PIN#16, BCM code pin number is 23 and wPi pin number is 4
infrared_pin=16
 
# I/O Pin connected to PIN#12, which can set be PWM mode
buzzer_pin=12
 
# I/O Pin connected to PIN#18, BCM code pin number is 24 and wPi pin number is 5
relay_pin=18
 
capture_times=2
local_image="rpi_capture.jpg"
 
def turn_relay(cmd):
  if OFF == cmd:
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +"  turn relay off");
    GPIO.output(relay_pin, GPIO.LOW)
  else:
    GPIO.output(relay_pin, GPIO.HIGH)
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +"  turn relay on");
 
def sig_proc(signum, frame):
  print("Catch stop signal and program exit now...")
  GPIO.cleanup()
  exit()
 
 
def alarm_handler(signum, frame):
  turn_relay(OFF)
 
signal.signal(signal.SIGALRM, alarm_handler)
signal.signal(signal.SIGINT, sig_proc)
signal.signal(signal.SIGTERM, sig_proc)
 
def module_int():
 
  #Raspberry Python GPIO Initialise
  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BOARD)
 
  # Initialise infrared GPIO port
  GPIO.setup(infrared_pin, GPIO.IN)
 
  # Initialise relay GPIO port
  GPIO.setup(relay_pin, GPIO.OUT)
 
  # Initialise camera library
  pygame.init()
  pygame.camera.init()
 
 
def turn_buzzer_on():
  GPIO.setup(buzzer_pin, GPIO.OUT)
  p = GPIO.PWM(buzzer_pin, 2800)
  #p.ChangeFrequency(2800)
  #p.ChangeDutyCycle(50);
 
  p.start(50)
  time.sleep(1.5)
  p.stop()
 
  GPIO.setup(buzzer_pin, GPIO.IN)
 
 
def ftp_connect(host, username, password):
  ftp = FTP()
  ftp.connect(host, 21)
  ftp.login(username, password)
  return ftp
 
def upload_file(ftp, remotepath, localpath):
  bufsize = 1024
  fp = open(localpath, 'rb')
  ftp.storbinary('STOR '+ remotepath , fp, bufsize)
  fp.close()
 
def detect_handler(channel):
  print( time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + "  Someone is closing on!")
  #turn_buzzer_on()
 
  turn_relay(ON)
  signal.alarm(30)
 
  camera = pygame.camera.Camera("/dev/video0",(1024,768))
  camera.start()
  time.sleep(3)
 
  for i in range(capture_times):
      #print("start camera capture image times [%d]" % (i+1));
      image = camera.get_image()
      pygame.image.save(image, local_image)
 
      #print("Start FTP upload image file");
      remote_image="StudioCapture_"+time.strftime('%Y%m%d_%H%M%S',time.localtime(time.time()))+".jpg"
      ftp = ftp_connect("master.iot-yun.com", "raspberry", "raspberry@pi")
      upload_file(ftp, remote_image, local_image)
      ftp.quit()
 
      print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +"  camere capture and ftp upload done");
      time.sleep(2)
 
  camera.stop()
 
 
 
if __name__ == "__main__":
  module_int()
  GPIO.add_event_detect(infrared_pin, GPIO.RISING, detect_handler)
 
  while True:
    if GPIO.input(infrared_pin) == False:
      print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"  No one nearby!"
 
    time.sleep(1);