From 8d2c25c8b313426b723870ba684144ec2d086698 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 31 Jan 2019 20:52:22 +0800
Subject: [PATCH] Add monitord.py source code

---
 monitord/monitord.py |  129 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 129 insertions(+), 0 deletions(-)

diff --git a/monitord/monitord.py b/monitord/monitord.py
new file mode 100755
index 0000000..ee31791
--- /dev/null
+++ b/monitord/monitord.py
@@ -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);
+

--
Gitblit v1.9.1