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
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.
You can sign up to Twilio with my referral code here.
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.
If you are a novice in programming with Raspberry Pi, you can find well-explained instructions and tutorials on the official Raspberry Pi website.
To go to the official Raspberry Pi setting up tutorial, click here.
- Update Raspberry Pi before running the terminal.
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.
- Create a file named whatsapp_surveillance.py on your IDE.
- 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.
Code testing on the shell:
Display the generated files(.h264,.jpg,.mp4) on Raspberry Pi:
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.
1-) Check whether the brightness is optimum or low for the camera module using the photoresistor.
2-) Detect intruders using the GP2Y0A02YK0F Infrared Proximity Sensor.
3-) If the proximity sensor detects motion, capture a first-look thumbnail image and record a 15 seconds video.
4-) Send the captured image(jpg), the video(mp4), and the brightness value as intrusion alert to the verified phone number via WhatsApp.
Set up Raspberry Pi and its peripherals.
Assembly the camera module holder.
Attach Arduino Nano, sensors, and components on the breadboard.
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.
Testing Code on Shell
Intrusion Alert on WhatsApp
Video Recorded by Raspberry Pi | intrusion_01-17-2020_17.25.01
Comments