Sitemap / Advertise

Introduction

Get notified via WhatsApp with a video(mp4) and a captured first-look thumbnail image as intrusion alert when the proximity sensor detects motion.


Tags

Share

WhatsApp Surveillance Video Camera with Infrared Proximity Sensor

Advertisement:


read_later

Read Later



read_later

Read Later

Introduction

Get notified via WhatsApp with a video(mp4) and a captured first-look thumbnail image as intrusion alert when the proximity sensor detects motion.

Tags

Share





Advertisement

Advertisement




    Components :
  • [1]Raspberry Pi 3 Model B+
  • [1]Raspberry Pi Camera Module V2
  • [1]Raspberry Pi 3 Board Case 3B
  • [1]Arduino Nano
  • [1]GP2Y0A02YK0F Infrared Proximity Sensor
  • [1]Photoresistor
  • [2]LED
  • [1]Breadboard
  • [4]220 ohm resistor
  • [1]1K resistor
  • [1]Female/Male Jumper W.
  • [1]Male/Male Jumper W.

Description

As you can see in my previous electronics projects, I am fond of developing web applications showing good congruence with Twilio's API, giving unique features to electronics projects via WhatsApp.

In this project, I wanted to create a surveillance video camera for my library, especially for one particular bookcase, to get notified when someone takes a book or organizes books differently. In that regard, I programmed a Raspberry Pi 3 Model B+ to capture the first-look thumbnail image and record a video with timestamp according to the data collated by Arduino Nano - motion and brightness. After collecting files and information from sensors, Raspberry Pi transfers them to the file_pathway webhook to be sent to the verified phone number as WhatsApp notification messages. In PHP, I provided the source code of the file_pathway webhook: save the file_pathway webhook on your server by entering your Twilio account settings - works stupendously even on the localhost.

Aside from this project, if you need a host server for the file_pathway webhook or a user interface on your server to manage the uploaded files easily, inspect this web application on TheAmplituhedron.

Some of the mentioned products and components in this project are sponsored by digitspace:

- GP2Y0A02YK0F Infrared Proximity Sensor | Inspect

- Raspberry Pi Camera Module V2 3B/3B+ Compatible | Inspect

- Raspberry Pi Camera Module V2 + Holder for Model 3B | Inspect

- Raspberry Pi 3 Board Case 3B | Inspect

- Raspberry Pi 4B/3B+ SD Card 32GB | Inspect

- Photoresistor | Inspect

On testing:

- Raspberry Pi 3B Micro-HDMI VGA Converter Cables | Inspect

- Raspberry Pi 5V3A Power Adapter USB Cable with Switch | Inspect

project-image
Figure - 36.1

Step 1: Twilio for WhatsApp

To send WhatsApp messages through Twilio's API, the only thing you need to do is to sign up for a Twilio Trial Account.

After signing up, with your SID and Auth Token, you can use Twilio's API for WhatsApp without any charge. As explained on Twilio, you need to join a shared phone number with your phone in order to verify your phone number and initiate WhatsApp template messages.

Important: In this project, we use Twilio's API to send WhatsApp notification messages in PHP and thus you do not need to change the default endpoint URL of your Twilio application in the Sandbox. In other words, you can use the file_pathway webhook even on the localhost.

For more information and learn how to activate your account, click the link below.

Go to Twilio.

You can sign up to Twilio with my referral code here.

project-image
Figure - 36.2


project-image
Figure - 36.3


project-image
Figure - 36.3.1

Step 2: Developing the file_pathway webhook

Elicit files and information from Raspberry Pi to save them to the server - video, captured thumbnail image, and brightness.

Send the currently uploaded files to the verified phone number as intrusion alert via WhatsApp.

To manage that, you have to download Twilio PHP Helper Library in your server from here.

- If the uploaded file names are not empty, get the brightness value and file properties for each uploaded file - name, temporary name, size, and extension.

- Check whether the uploaded file extensions are in allowed formats and whether the uploaded file sizes exceed the data limit - 5MB.

- Define media URLs.

- Upload files to the server.

- Send the information of the uploaded files, including temporary names, to Raspberry Pi as the response message.

- Define the required properties by Twilio's API to send files and information to the verified phone number via WhatsApp - SID, AUTH_TOKEN, Verified Number, and Shared Number.

- Note: Refrain from adding '+' at the beginning of the phone numbers when entering them as send_data_to_WhatsApp() function parameters.

project-image
Figure - 36.4


project-image
Figure - 36.5


project-image
Figure - 36.6

Step 3: Setting Up Raspberry Pi

If you are a novice in programming with Raspberry Pi, you can find well-explained instructions and tutorials on official Raspberry Pi website.

To go to the official Raspberry Pi setting up tutorial, click here.

project-image
Figure - 36.7

- Update Raspberry Pi before running the terminal.

project-image
Figure - 36.8


project-image
Figure - 36.9

Step 4: Setting Up the Camera Module

To learn how to connect the Raspberry Pi Camera Module to your Raspberry Pi and take pictures, record video, and apply image effects, go to the official getting started with the camera module tutorial.

project-image
Figure - 36.10

Step 5: Programming Raspberry Pi to send files and information to the file_pathway webhook in Python

- Create a file named whatsapp_surveillance.py on your IDE.

project-image
Figure - 36.11

- Add the required libraries.

- Set up BCM GPIO numbering.

- Define input pins to get signals from Arduino Nano.

- Initiate the camera module with pre-defined settings.

- Annotate the date on the generated files as the timestamp.

- Capture an image as the thumbnail and record a 15 seconds video.

Note: Raspberry Pi records videos in the h264 format, but unfortunately, this format is not supported by WhatsApp. To convert video formats, download MP4Box as shown here.

- Convert the h264 format to the mp4 format using MP4Box as terminal command to open videos on WhatsApp.

- Define the file_pathway location to send the currently recorded video and data to the server. I used TheAmplituhedron to host the file_pathway webhook for my surveillance camera, change the URL as your file_pathway directory location on your server - e.g., https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/file_pathway.php

- Define the files and the data parameters - video, captured image, and brightness.

Note: Download the requests library to make HTTP requests in Python as shown here.

- Make an HTTP Post Request to the file_pathway webhook to send information.

- Print the response sent by the file_pathway webhook.

- Initiate the loop.

- Get values from the input pins.

- If the proximity sensor detects motion, activate record_trespassing() and send_video_to_server() functions.

project-image
Figure - 36.12


project-image
Figure - 36.13


project-image
Figure - 36.14


project-image
Figure - 36.15

Code testing on the shell:

project-image
Figure - 36.16


project-image
Figure - 36.17


project-image
Figure - 36.18

Display the generated files(.h264, .jpg, .mp4) on Raspberry Pi:

project-image
Figure - 36.19


project-image
Figure - 36.20

Step 6: Reading Analog Sensors with Raspberry Pi using Arduino Nano

Unfortunately, Raspberry Pi is not capable of reading analog signals from analog sensors since it does not include a built-in ADC (Analog to Digital Converter) circuit.

You can either add an external ADC such as the MCP3008 to read analog signals with Raspberry Pi or use capacitors to read resistance values.

Nevertheless, in this project, I used an Arduino Nano to read analog signals from the analog sensors because I have lots of spare Arduino Nano boards at my disposal. Arduino Nano generates digital output values according to the thresholds for each sensor - GP2Y0A02YK0F Infrared Proximity Sensor and Photoresistor. If the predefined condition is met, it produces 1 (HIGH) as the digital output detected as the input value by Raspberry Pi.

Then, Raspberry Pi can read analog signals from sensors as the digital output values generated by Arduino Nano with the input pins to activate record_trespassing() and send_video_to_server() functions.

Do not forget to add voltage dividers for each input pin due to the voltage discrepancy between Raspberry Pi and Arduino Nano.

- Get brightness as percentage values from the photoresistor using the map() function.

- Get distance in centimeters from the GP2Y0A02YK0F Infrared Proximity Sensor using the sensor value in the datasheet - 0.0048828125 (5 / 1024).

- Define conditions to generate digital output values as signals detectable by Raspberry Pi.

project-image
Figure - 36.21


project-image
Figure - 36.22

Features

1-) Check whether the brightness is optimum or low for the camera module using the photoresistor.

project-image
Figure - 36.23

2-) Detect intruders using the GP2Y0A02YK0F Infrared Proximity Sensor.

project-image
Figure - 36.24

3-) If the proximity sensor detects motion, capture a first-look thumbnail image and record a 15 seconds video.

project-image
Figure - 36.25

4-) Send the captured image(jpg), the video(mp4), and the brightness value as intrusion alert to the verified phone number via WhatsApp.

project-image
Figure - 36.26


project-image
Figure - 36.27


project-image
Figure - 36.28


project-image
Figure - 36.29

Connections

Set up Raspberry Pi and its peripherals.

project-image-slide project-image-slide
Slide


Assembly the camera module holder.

project-image-slide project-image-slide
Slide


Attach Arduino Nano, sensors, and components on the breadboard.

project-image-slide project-image-slide
Slide


Affix the camera module and the breadboard to the Raspberry Pi board case. And, connect the Raspberry Pi GPIO input pins to the Arduino Nano digital output pins as shown on the schematic.

project-image-slide project-image-slide
Slide


Videos

Testing Code on Shell

Intrusion Alert on WhatsApp

Video Recorded by Raspberry Pi | intrusion_01-17-2020_17.25.01


Code

file_pathway.php (webhook)

Download



<?php
// Implement Twilio's API to send data to WhatsApp.
require_once $_SERVER['DOCUMENT_ROOT'].'/path/to/vendor/autoload.php'; 
 
use Twilio\Rest\Client; 

if(!empty($_FILES["rasp_video"]["name"]) && !empty($_FILES["rasp_capture"]["name"])){
	// Get the brightness value from Raspberry Pi.
	$brightness = (isset($_POST['brightness'])) ? $_POST['brightness'] : "Not Detected!";
	
	// Get properties of the uploaded files.
	// Video File:
	$video_properties = array(
	    "name" => $_FILES["rasp_video"]["name"],
	    "tmp_name" => $_FILES["rasp_video"]["tmp_name"],
		"size" => $_FILES["rasp_video"]["size"],
		"extension" => pathinfo($_FILES["rasp_video"]["name"], PATHINFO_EXTENSION)
	);
	// Image File:
	$capture_properties = array(
	    "name" => $_FILES["rasp_capture"]["name"],
	    "tmp_name" => $_FILES["rasp_capture"]["tmp_name"],
		"size" => $_FILES["rasp_capture"]["size"],
		"extension" => pathinfo($_FILES["rasp_capture"]["name"], PATHINFO_EXTENSION)
	);
	// Check whether the uploaded file extensions are in allowed formats.
	$allowed_formats = array('jpg', 'png', 'mp4');
	if(!in_array($video_properties["extension"], $allowed_formats) || !in_array($capture_properties["extension"], $allowed_formats)){
		echo 'SERVER RESPONSE:\r\nFILE => File Format Not Allowed!';
	}else{
		// Check whether the uploaded file sizes exceed the data limit - 5MB.
		if($video_properties["size"] > 5000000 || $capture_properties["size"] > 5000000){
			echo 'SERVER RESPONSE:\r\nFILE => File size cannot exceed 5MB.';
		}else{
		    $video_root = $video_properties["name"];
		    $capture_root = $capture_properties["name"];
			$media = "[_URL_]".$capture_properties["name"]; // e.g., https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/
			$video = "[_URL_]".$video_properties["name"];   // e.g., https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/
			
			// Upload files - video and capture image.
		    move_uploaded_file($video_properties["tmp_name"], $video_root);
		    move_uploaded_file($capture_properties["tmp_name"], $capture_root);
			
		    // Send the information of the uploaded files, including temp names, to Raspberry Pi as the response message.
		    echo "SERVER RESPONSE:\r\nFILE => Files Uploaded Successfully!\r\nTMP_VIDEO => ".$video_properties["tmp_name"]."\r\nVIDEO_SIZE => ".$video_properties["size"]."\r\nTMP_CAPTURE => ".$capture_properties["tmp_name"]."\r\nCAPTURE_SIZE => ".$capture_properties["size"]."\r\nDATA => Received: ".$brightness;
			
			// Send the recently uploaded files to WhatsApp - video and capture image.
			// Define the required properties by Twilio's API:
            // SID, AUTH_TOKEN, FROM_PHONE_NUMBER, TO_PHONE_NUMBER, MESSAGE BODY, and MEDIA FILE PATH(CAPTURE).
			// Note: Refrain from adding '+' at the beginning of the phone numbers when entering them as send_data_to_WhatsApp() function parameters.
			send_data_to_WhatsApp("[_SID_]", "[_AUTH_TOKEN_]", "[_FROM_PHONE_NUMBER_]", "[_TO_PHONE_NUMBER_]", "Intrusion Detected by Raspberry Pi 🏠 ☣\n\nBrightness => ".$brightness."\n\nVideo =>\n".$video, $media);
			echo "\r\nAPI => Files Send to WhatsApp!";
		}
	}

}else{
	echo "SERVER RESPONSE:\r\nFILE => No File Detected!";
}

function send_data_to_WhatsApp($sid, $token, $from, $to, $body, $media){
	$twilio = new Client($sid, $token);
    $message = $twilio->messages->create("whatsapp:+".$to, array("from" => "whatsapp:+".$from, "body" => $body, "mediaUrl" => array($media)));
}

?>


whatsapp_surveillance.py

Download



# WhatsApp Surveillance Video Camera with Infrared Proximity Sensor
#
# Raspberry Pi 3 Model B+
#
# By Kutluhan Aktar
#
# Get notified via WhatsApp with a video and a captured first-look thumbnail as intrusion alert
# when the proximity sensor detects motion.
#
# For more information and explanation, visit the link below:
# https://www.theamplituhedron.com/projects/WhatsApp-Surveillance-Video-Camera-with-Infrared-Proximity-Sensor/
#
# If you need a host server for this project or a web application by which you can manage the uploaded files easily,
# check out this application on TheAmplituhedron:
# https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/

from picamera import PiCamera
from time import sleep
import datetime
from subprocess import call 
import requests
import RPi.GPIO as GPIO

# Set up BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set up input pins
IR_INPUT = 22
PR_INPUT = 23
GPIO.setup(IR_INPUT, GPIO.IN)
GPIO.setup(PR_INPUT, GPIO.IN)

# Define functions:

# Initiate the camera module with pre-defined setttings.
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 15

def record_trespassing(file_h264, file_mp4, file_capture, text):
    # Add date as timestamp on the generated files.
    camera.annotate_text = text
    # Capture an image as the thumbnail.
    sleep(2)
    camera.capture(file_capture)
    print("\r\nImage Captured! \r\n")
    # Record a 15 seconds video.
    camera.start_recording(file_h264)
    sleep(20)
    camera.stop_recording()
    print("Rasp_Pi => Video Recorded! \r\n")
    # Convert the h224 format to the mp4 format.
    command = "MP4Box -add " + file_h264 + " " + file_mp4
    call([command], shell=True)
    print("\r\nRasp_Pi => Video Converted! \r\n")
    
def send_video_to_server(file_mp4, file_capture, brightness):
    # Define the file path to send the currently recorded video to the server.  
    url = 'https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/file_pathway.php'
    files = {'rasp_video': open(file_mp4, 'rb'), 'rasp_capture': open(file_capture, 'rb')}
    data = {'brightness': brightness}
    # Make an HTTP Post Request to the server.
    request = requests.post(url, files=files, data=data)
    # Print the response from the server.
    print ("Rasp_Pi => Files Transferred! \r\n")
    print(request.text + "\r\n")

# Initiate the loop.
while True:
    print("Waiting...")
    
    if GPIO.input(IR_INPUT):
        brightness = "LOW" if GPIO.input(PR_INPUT) else "OPTIMUM"
        # Get the current date as the timestamp to generate unique file names.
        date = datetime.datetime.now().strftime('%m-%d-%Y_%H.%M.%S')
        capture_img = '/home/pi/Surveillance/intrusion_' + date + '.jpg'
        video_h264 = '/home/pi/Surveillance/intrusion_' + date + '.h264'
        video_mp4 = '/home/pi/Surveillance/intrusion_' + date + '.mp4'
        # Create and send files:
        record_trespassing(video_h264, video_mp4, capture_img, date)
        send_video_to_server(video_mp4, capture_img, brightness)
        
    sleep(1)


WhatsApp Surveillance Video Camera.ino

Download



         ////////////////////////////////////////////////////  
        //      WhatsApp Surveillance Video Camera        //
       //        with Infrared Proximity Sensor          //
      //           -------------------------            //
     //                 Arduino Nano                   //           
    //               by Kutluhan Aktar                // 
   //                                                //
  ////////////////////////////////////////////////////

// Get notified via WhatsApp with a video and a captured first-look thumbnail as intrusion alert when the proximity sensor detects motion.
//
// This code is only for reading analog inputs in order to pass the values to Raspberry Pi.
//
// For more information, visit the project page:
// https://www.theamplituhedron.com/projects/WhatsApp-Surveillance-Video-Camera-with-Infrared-Proximity-Sensor/
//
// If you need a host server for this project or a web application by which you can manage the uploaded files easily, check out this application on TheAmplituhedron:
// https://www.theamplituhedron.com/dashboard/WhatsApp-Surveillance-Camera/
//
// Connections
// Arduino Nano :           
//                                Sharp-GP2Y0A02YK0F IR Sensor
// GND --------------------------- GND
// 5V  --------------------------- +
// A0  --------------------------- Signal
//
//                                Photoresistor
// A1  --------------------------- 
//                                IR_OUTPUT_LED
// D3  --------------------------- 
//                                PR_OUTPUT_LED
// D4  --------------------------- 


// Define sensor pins.
#define IR_PIN A0
#define PR_PIN A1
#define IR_OUTPUT 3
#define PR_OUTPUT 4

// Define data holders.
int distance, brightness;

void setup() {
  Serial.begin(9600);

  pinMode(IR_OUTPUT, OUTPUT);
  pinMode(PR_OUTPUT, OUTPUT);
  
}

void loop() {
  // Get brightness.
  brightness = map(analogRead(PR_PIN), 0, 1023, 0, 100);
  
  // Get distance from Sharp-GP2Y0A02YK0F IR Sensor.
  float volts = analogRead(IR_PIN) * 0.0048828125; // Sensor Value: (5 / 1024)
  distance = 13 * pow(volts, -1); //from datasheet graph
  delay(100);

  Serial.println("Brightness: " + String(brightness) + "%\n\n");
  Serial.println("Distance: " + String(distance) + "cm");

  // Send signal to Raspberry Pi.
  if(distance < 9){ digitalWrite(IR_OUTPUT, HIGH); }else{ digitalWrite(IR_OUTPUT, LOW); }

  if(brightness < 10){digitalWrite(PR_OUTPUT, HIGH); }else{ digitalWrite(PR_OUTPUT, LOW); }
}


Schematics

project-image
Schematic - 36.1

Downloads

Zip Folder

Download


Fritzing File

Download