This is step number six of the final project.
And now what we're going to do is we're going to take a photo from the Raspberry Pi.
When we have the command that the button has been pressed on the Arduino.
And if you don't have the Raspberry Pi camera here at this point, well, you can skip this step and
go to the next step where we send the message and the photo to Telegram.
And actually you can just send the message without sending the photo.
So of course it's better and the project will be complete with the camera.
But if you don't have the camera at this time, you can still finish the project just by sending a text
message to the telegram chat.
All right.
And let's start.
So we already have our code structure with the setup of the serial.
With our void loop, we receive data from Serial, and we can also send data to the Arduino.
And we know, of course, that the Arduino is working correctly from the previous steps.
So let's handle the camera.
And this step is going to be pretty quick.
Actually.
Let's create a new and I'm just going to start here and save it as intercom comm step.
Six.
And so the first thing we are going to do is to initialize the camera.
So from Pi camera, so from the Pi camera module, we import pi, camera object.
And now where are we going to initialize this camera?
So we first initialize cell and then we have a wait after setup.
What I'm going to do is I'm going to initialize camera here.
So init camera I'm going to put a print.
So init.
Camera so we know what we're doing and then camera is equal to pi camera.
Let's choose a resolution.
Camera dot resolution is equal to and let's give a tuple and let's use 640 by 480.
Pretty low resolution, but big enough for us to see what we need to see on the image.
And then camera rotation for me because my images are upside down.
So adjust it depending on your camera.
I'm going to create an image folder name, which is slash org slash api slash camera.
Okay, let's use this camera folder in our home directory that doesn't necessarily exist.
And so we're going to check if not OS.
So if we use OS import.
Twice, if not Dot.
Path dot exists.
So if this folder doesn't exist, we create it as dot mkdir image folder name and then we're going to
create an image file name for the photos we're going to take, which is the folder name plus.
So here we give the complete path.
Let's name it dot.
Camera dot jpg.
Okay, let's try to give a more meaningful name than just image.
And after that, we can say print camera.
Okay.
And then.
Well, we need to wait for at least two seconds so the camera can adjust to the luminosity of the room.
But as we are waiting for three seconds here, all ready for the sale.
This is okay.
So the time dot sleep will be useful for both the sale and the camera.
Well, and then once the camera is initialized, the only thing we need to do is to take a picture when
we have the open door request.
Let's add another log here.
Open the request.
Taking a photo we send.
Please wait for a few seconds on the Arduino and we do camera dot capture image file name.
Okay, let's try this code.
So code is still running on the arduino.
I can see main loop is starting.
Let's open file manager here and I'm going to open the camera folder.
Let's clear everything I have for now.
Here.
And I'm going to press on the push button.
And you can see we have open door requests taking a photo and we have a new photo here.
We're going to open it and well, that's working.
If I press on the push button again, let's do something else.
You can see taking a photo and we have another photo.
Okay.
Every time you press on the button, you send a new request and you take a new photo that's going to
replace the previous one.
And now that we have this photo, we can go to the next step, which is to send it to Telegram.