In this blog we would like to show you how to interface Pi camera with our product IA004 -Rpi UPS Board using Raspberry Pi and Node RED. Here we have implemented a camera, that will capture the image and that image will be sent to particular email account. And also, we have implemented the MJPEG streaming and face detection in the captured images.
- Raspberry pi zero w.
- ARMtronix board (IA004).
- 3.7V Li-Ion/Li-Po Battery.
- Raspberry pi Camera module.
Table 1: LED STATE
|LED No||ON State||OFF State|
|LED1(D2)||When the board is running on Battery||When the mains supply is present.|
|LED2(D6)||When the battery is charging.||When the battery is full charged.|
|LED3(D4)||When the Raspberry Pi is ON.||When Raspberry Pi is OFF.|
|LED4(D3)||When the mains supply is present.||When the mains supply is absent|
Steps to setup the Raspberry Pi and Camera module:
- Insert the microSD card into the card slot of the raspberry pi.
- Plug the USB keyboard and mouse into the USB ports of the raspberry pi.
- Plug the HDMI cable into the monitor and connect the other end of the cable into the raspberry pi.
- Connect the ARMtronix IA004 – Rpi UPS Board to the raspberry pi header using 40Pin FRC or stack up using onboard 40 pin female berg header.
- Use the external 230V AC to 5V DC power adapter and connect to USB ports of IA004 – Rpi UPS Board and other options power up are through USB or terminal block.
- Install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi camera port, with the silver connectors facing down (or black colour side of the connectors facing up).
- Switch on to power adapter to boot up the raspberry pi. A power indicator light will begin to glow, which means that the Pi is connected properly.
- Care must be taken while connecting the ARMtronix board to the raspberry Pi, i.e. both must be connected in such a manner that the 1st pin of IA004’s 40-Pin header (J2) is connected to the 1st pin in the header of the raspberry pi as shown in below diagram (Figure 3).
- While connecting the camera to raspberry pi, we have to make the connections such that, at the camera end the silver side of connectors must be facing towards the camera PCB(or black colour coated side facing up) and at the raspberry pi end the silver side of connectors facing down(or black colour side of the connectors facing up). Camera connections as show in below diagram (Figure 4).
If anyone wants to implement the other special features of our product IA004-Rpi UPS board such as setting up the RTC, Headless configuration using onboard USB to UART converter, battery monitoring and auto shutdown and reboot part, they can go through our previous blog of the same board IA004-Rpi UPS board (Applications of Raspberry Pi UPS Board(IA004)).
Setting up the Camera Module:
- Before you take your Camera Module out of the box, be aware that it can be damaged by static electricity. Make sure you have discharged yourself by touching an earthed object.
- Install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi camera port as we discussed earlier in this blog (Figure 4) .
- Now boot the Raspberry Pi (plug the power in and turn it on).
- Once booted, first thing we have to do is, update the Raspberry Pi by running the commands in a terminal window as “sudo apt update” and after that “sudo apt full-upgrade”.
- Now run the “sudo raspi-config” command to go into the Raspberry Pi configuration tool.
- Now we have the raspberry Pi configuration window, where we have to go to the “Interfacing Options” with the help of arrow keys and click Enter.
- After hitting the Enter button, we will get some interfacing options. In that we have to select the “Camera” option and we have to enable it by selecting “yes”.
- Now we have successfully enabled the camera interface and this will work after rebooting the raspberry Pi. So, after this modification we have to select “Finish” and select to reboot the Raspberry Pi.
- After rebooting the raspberry pi, the camera module starts to work properly. Now we can test the working of the camera module by running the command “raspistill -v -o test.jpg”. where, ‘raspistill’ is a command line application that allows you to capture images with camera module.
- After running the above command, we will get the image saved in the path “/home/pi” as filename “test.jpg”. If we failed to get this image, then the reason for this failure might be we have not upgraded (commands “sudo apt update” and “sudo apt full-upgrade”) the raspberry pi properly which are the basic steps to follow and recheck the above steps.
Node-RED concept for capturing the images and send it to the given email ID:
As we explained features of our product IA004-Rpi UPS board in previous blog, as the name suggests it is UPS board and we can use it when there is no power supply so in that condition raspberry pi will powered on using the battery voltage and when there is main power supply, battery will charge.
Here we are running the raspberry pi through IA004-Rpi UPS board for capturing the images and send it to the given email ID. By this way Pi camera will also work when there is no mains voltage. The setup connections for this application is as show in the below diagram.
We are using Node-RED concept to capture the image and send it via email. Before we begin with the Node-RED flow, first we have to ensure that, we have installed all the required nodes for this application.
To capture the image from the camera we must have camerapi node. If we don’t have that, Click on the top right corner in the Node-RED, then go to ‘manage palette’, select ‘install’ option and there we have to search for “node-red-contrib-camerapi” and press install or also we can install by running the “npm install node-red-contrib-camerapi” command in the root directory of the Node-RED install or home directory.
To send the image through email we have to install “node-red-node-email” package. To install this, follow the same steps as discussed above. Here also we can install by running the “npm install node-red-node-email” command in the root directory of the Node-RED install or home directory. Once we have all the nodes then we can make the below shown flow diagram.
Here when we press the input node, it will send some input to ‘camerapi’ node. Then that node will capture the image and saved it in raspberry pi (default path: /home/pi/Pictures/) and Next, the function node is introduced, that will create the attachment of the image to send this to any particular email address. After that, in email node we have to give receiver email address, sender email address and app password of sender email.
The we have to make the following changes in the camerapi (CAMERA) node.
Here the camerapi node will give the image path for every image it captured in msg.payload. so, we will use that msg.payload for the function node. The below figure shows the code of the function node IMAGE PATH.
The following changes we have to make in the properties of ‘EMAIL’ node.
NOTE: Here in Password tab we have to enter the app password generated by using sender email address. Follow the bellow given steps to generate the app password.
- Go to your ‘Google Account’.
- Select ‘Security’.
- Under “Signing in to Google” select App Passwords. You may need to sign in. If you don’t have this option, it might be because:
- 2-Step Verification is not set up for your account.
- 2-Step Verification is only set up for security keys.
- Your account is registered through work, school, or other organization.
- You have turned on Advanced Protection.
- At the bottom, choose Select app and choose the app (mail) > Select device and choose the device (windows computer) > Generate.
- Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device. Copy that app password and paste it in the password tab of ‘EMAIL’ node properties.
- Tap Done.
In this blog, further we have implemented the MJPEG live streaming using the Raspberry Pi Camera Module. To begin with this part, we have to follow the below mentioned steps.
- First step is to update and upgrade the operating system (Go to ‘setting up the Camera module’ section for the commands).
- Next, we have to enable the camera as we did it in earlier (if you already enabled the camera in interfacing options, then ignore this step).
- After enabling the camera, we have to create a project folder for MJPEG streamer using the command “mkdir projects” and go to the folder ‘projects’ by running the command “cd projects/” in the terminal.
- Next, we have to download the MJPEG streamer. To download the project, you will need a source control system called git. It may not be installed on a fresh image. Run the command “sudo apt-get install git” to install the git.
- Now that we have git installed, use it to clone a copy of the mjpg-streamer to the Raspberry Pi by the command “git clone https://github.com/jacksonliam/mjpg-streamer.git”.
- After downloading the MJPEG streamer next step is to compile the MJPEG streamer. If you aren’t familiar with compilers, then just run the following commands to compile the MJPEG streamer.
- cd mjpg-streamer/
- cd mjpg-streamer-experimental/
- sudo apt-get install cmake
- sudo apt-get install python-imaging
- sudo apt-get install libjpeg-dev
- make CMAKE_BUILD_TYPE=Debug
- sudo make install
- Next step is to set an environment variable. This is so the program can know where to find the libraries that it needs. we will always need to run this first. Don’t forget the period (‘.’) at the end of the command.
- Export LD_LIBRARY_PATH=.
- Now we have to enter the following command to run the MJPEG streamer.
- ./mjpg streamer -o “output_http.so -w ./www” -i “input_raspicam.so”
- Next, to see the streaming, browse to the Pi from another computer.
- Click on the Stream tab on the left to see the stream. Or:
Result of the MJPEG streamer:
Node-RED concept for taking the images from the MJPEG stream and send it to the given email ID if the face is detected:
To implement this, make the same connections as we did for the image capturing flow (Refer Figure 11). Before starting the node red flow, we have to run the MJPEG streamer as discussed earlier. Once the streaming is started, we are basically taking the images from the stream using the “http request” node and then we are saving the image in jpg format. After that we have to pass that image to “opencv” node which is used for the face detection purpose. Next, we are checking that whether the face is detected or not in that image. If the image is detected, then we are sending it to the given email address via “email” node, if not, then we are neglecting that image.
For this process, we have to make the node red flow as shown in the following figure. And we have to install the “node-red-contrib-opencv” and “node-red-node-email” node red libraries.
Here we are showing the changes you have to make to each node for it to work properly.
- “http request” node: we have to make the following changes.
- “file” node: To save the image we have to make the following changes in the node.
- “opencv” node: Here opencv node is used to find the face detection in the particular image. We have to make the node settings as follows.
- “function” node: After the face detection process, we have to send the image through email if the face is detected in the image. So, for that we have to add the following function in the node to decide that face is detected or not.
- “Email” node: Changes we have to make in the function node are as similar as we discussed in image capturing section. For details go through the email node setting of the image capturing section.
By this, we have successfully implemented the MJPEG live streaming and sending the images through email whenever the face is detected in that image.and we have also successfully tested that the raspberry pi (raspberry pi zero w) and camera module will work on the battery voltage upto approximate of 12 hours.
Image example, that is sent through email: