Kenny Ho
Published

Camera Alert Application with Raspberry Pi 3, iOS/Android

How to build a simple application that detects motion and sends notifications to the your smart phone using Raspberry Pi and camera module.

EasyFull instructions provided1 hour7,495
Camera Alert Application with Raspberry Pi 3, iOS/Android

Things used in this project

Story

Read more

Code

Initialize two major objects at first

Python
 
camera = picamera.PiCamera()
notificationHandler = NotificationHandler(PUSHBULLET_KEY,didReceiveCommand)
 
def main():
	global isMotionDetected
	global notificationHandler
	logging.info("### Initialize Camera")
	cameraInitialize()
	pushData = {'type': 'TEXT_MESSAGE', 'text': 'PiCameraNotifier app starts !'}
	notificationHandler.pushToMobile(pushData)
 

Camera Initialize

Python
 
def cameraInitialize():
	logging.info("cameraInitialize: for (1) motion detection, and (2) circularIO recording")
	global camera
	# for motion detection 
	motionAnalysisPort=2
	camera.start_recording(
				'/dev/null', 
				splitter_port=motionAnalysisPort,
				resize=(640,480),
				format='h264',
				motion_output=DetectMotion(camera, size=(640,480))
				)
	# for circularIO recording
	HDVideoRecordPort=1
	global stream
	camera.start_recording(
				stream,
				format="h264", 
				resize=(640,480),
				splitter_port=HDVideoRecordPort)
 

Detect Motion

Python
 
class DetectMotion(picamera.array.PiMotionAnalysis):
	def analyse(self,a):
		global isMotionDetected
		a = np.sqrt(np.square(a['x'].astype(np.float)) + np.square(a['y'].astype(np.float))).clip(0, 255).astype(np.uint8)
		if(a > 60).sum() > 10:
			logging.info("motion just detected")
			isMotionDetected = True
			didDetectMotion() 
		else: 
			isMotionDetected = False
 
def didDetectMotion():
		global isRecordingMotion
		if isRecordingMotion:
			print("is Recording Motion ...")
		else: 
			isRecordingMotion = True
			print("start Recording Motion ...")
			global notificationHandler
			global camera
			pushData = {'type': 'TEXT_MESSAGE', 'text': 'Hey! someone sneak into your room. Check it out!'}
			notificationHandler.pushToMobile(pushData)
			fileName=time.strftime("%Y%m%d_%I:%M:%S%p")  # '20170424_12:53:15AM'
			logging.info("push image...")
			captureImage(fileName)
			camera.wait_recording(7)
			writeVideo(fileName)
			isRecordingMotion = False
 

At command received

Python
 
def didReceiveCommand(command):
	global notificationHandler
	if command == "@check":
		logging.info("get system info")
		process = subprocess.Popen([ WORKING_DIR + 'systemInfo.sh'], stdout=subprocess.PIPE)
		out, err = process.communicate()
		pushData = {'type': 'TEXT_MESSAGE', 'text': out}
		notificationHandler.pushToMobile(pushData)
	if command == "@snap"	:
		fileName=time.strftime("%Y%m%d_%I:%M:%S%p")  # '20170424_12:53:15AM'
		captureImage(fileName)
	else: 
		logging.info("Command not supported: " + command)
		logging.info("send notification to response")
 

init push manager

Python
 
	def __init__(self, pushBulletAPIKey,didReceiveCommand):
		# Setup pushBullet manager	
		self.pushBulletAPIKey = pushBulletAPIKey
		self.didReceiveCommand = didReceiveCommand
		self.pushBulletManager = Pushbullet(self.pushBulletAPIKey)
		thread = Thread(target = self.__createListener)
		thread.start()
		# Setup Notification Queue 
		self.__setupNotificationQueue()
 

At command received

Plain text
 
[Unit]
Description=My Sample Service
After=multi-user.target
 
[Service]
Type=simple
ExecStart=/home/pi/Desktop/PiCameraNotifier/main.py
 
[Install]
WantedBy=multi-user.target
 

Github

https://github.com/iotbreaks/PiCameraNotifier

Credits

Kenny Ho

Kenny Ho

0 projects • 9 followers
IoT fullstack engineer
Contact

Comments