WhatsApp Surveillance Video Camera with Infrared Proximity Sensor

January 21, 2020

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

Keywords
Brands
Hardware
  • [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_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_2
project_image_3
project_image_4

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_5
project_image_6
project_image_7

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.

- Update Raspberry Pi before running the terminal.

project_image_8
project_image_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.

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_10

- 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_11
project_image_12
project_image_13
project_image_14

Code testing on the shell:

project_image_15
project_image_16
project_image_17

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

project_image_18
project_image_19

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_20
project_image_21

Features

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

project_image_22

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

project_image_23

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

project_image_24

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_25
project_image_26
project_image_27
project_image_28

Connections

Set up Raspberry Pi and its peripherals.

project_image_29
project_image_30

Assembly the camera module holder.

project_image_31
project_image_32

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

project_image_33
project_image_34

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_35
project_image_36

Demonstration




Project GitHub Repository

The project's GitHub repository provides:

  • Code files

Schematics

project_image_37

Code

Select File

  • Webhook
  • whatsapp_surveillance.py
  • WhatsApp-Surveillance-Video-Camera.ino

Custom assets

See on other platforms