guowenxue
2019-01-31 8d2c25c8b313426b723870ba684144ec2d086698
Add monitord.py source code
1 files added
129 ■■■■■ changed files
monitord/monitord.py 129 ●●●●● patch | view | raw | blame | history
monitord/monitord.py
New file
@@ -0,0 +1,129 @@
#!/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);