This is the solution of the previous activity where you have to take a photo every two seconds on the
Raspberry Pi and then send the photo when you request it, either from Telegram or from the Arduino.
And let's start with this Arduino code, actually.
And well, this code, we've already done that multiple times.
So I'm going to open activity number six, for example.
And on the activity number six, I have the code to read from the push button.
I'm just going to copy this.
And put that here and I'm just going to remove what I don't need.
So we were using the servo motor, but I'm not going to use Servo here, just the push button.
So I'm going to remove that.
I'm going to keep the sire, of course, with this baud rate.
And then I'm going to remove this part.
So as you can see, with all the code that we did is quite module able.
Okay, We can just take some parts, remove some other parts and it's quite easy.
And now when we have the complete code to read from the push button and send button pressed when the
button has been pressed with the debounce mechanism.
Okay.
I am going to upload this to the Arduino directly.
Activity nine.
Let's upload.
And what can't open with my Arduino is not plugged to a Raspberry Pi.
So that's not going to work.
You can see now.
Okay, That's something very interesting.
This time I don't have t zero.
I have t one.
Okay, let's try to disconnect again and connect back.
Let's see again what we have.
And we still have ACM one, so I'm going to use ACM one and that's something to know when we try to
connect from the Raspberry Pi.
Okay.
So upload.
Down uploading.
Let's just check that.
If I press on the push button, I get button pressed.
That's correct.
Now let's go to the Raspberry Pi side.
So we need to do a few things.
We need to read the file to get the push button notification.
We need to take a photo every two seconds and we need to also send it to Telegram and receive callbacks
from Telegram.
Let's do the first thing here.
Maybe to take a photo every two seconds.
I'm going to remove that.
Okay, let's import So from pi camera import pi camera like this and then let's create a camera.
So camera is equal to pi camera.
Let's put the settings directly.
Camera, dot.
Resolution and I'm going to use a quite small one because, well, we don't need more actually, for
what we want to do in camera dot rotation is equal to 180 because I have seen before that the images
I take are upside down.
So check that for yourself before and then maybe use the camera dot rotation setting.
Let's add an image folder name, which is slash home slash pie slash camera.
Okay, let's actually go in the file manager and see.
We have the camera folder here already with new image and new image too.
Well, let's keep this like that.
So the folder exists, but I'm just going to make sure it exists just in case.
Okay.
If you remove it later on and if you try to run this program again.
So let's import os if OS dot path dot exists.
We've image for folder name.
Actually, if it doesn't exist we do s dot mkdir with.
The following.
Okay, that's very quick to do.
And now we have the folder and let's do image.
File name is equal to image folder name plus Slash.
Let's just call it Photo.jpg.
G.
Okay, so every time we're going to take a photo here, we're going to replace the previous one.
Then we need a time dot sleep to actually, maybe if I close this string, it's going to be better.
And we need the time dot slip too, to allow the camera to adjust to the luminosity.
And then.
Well, let's take a photo every two seconds.
So let's do while true.
Let's do camera dot capture with image file name and well, I'm not going to use time dot sleep to hear
why.
Well, I could use that actually just to take a photo every two seconds.
But then as we are going to need to read from Serial, I need to do a non-blocking sleep.
So I'm going to add here.
Last time photo taken equal to and let's import time, time, time and let's say photo delay is equal
to.
Two seconds.
And so now what I'm going to do, I'm not going to add a time sleep, too, but I'm still going to add
time, dot sleep time, dot sleep with zero one, which means that we are still going to block a bit,
but basically just ten milliseconds so we don't use too much CPU and that's going to be completely fine
for the rest.
And then we do if time dot So let's actually create a time now variable with time, dot time.
So if time now minus last time photo taken greater or equal than the photo delay.
Okay.
I've already done that before.
Then we do last time.
Photo taken is equal.
To time now and we take the.
Oh two.
All right, let's try just.
Let's just try this part of the code.
Let's run this so we don't have any communication with Scialfa.
No activity.
Nine.
And we don't have anything printed, of course, so we don't have any output.
But let's go to our folder here.
Camera We have photo.
That's one photo.
And let's wait two seconds and let's click on this.
Okay, let's open it.
Well, we have a problem here.
Let's just refresh.
Okay.
We have another photo that was taken That's different.
Okay.
Same file.
It has been rewritten.
Maybe you need to just refresh the view on the file manager and then you have the photo.
Great.
So this part is working.
And now what we need to do, we need to send that to telegram.
So we're going to first use the way that we can request from Telegram.
Okay.
So for that I'm going to go back to activity eight.
I'm just going to use the code we did before.
Okay.
So we're going to import the data and the common handler.
We are going to create.
And the data a dispatcher.
And then we're going to add some handlers and start pulling.
I'm going to copy all of that.
And where do we put it?
Well, let's just put that after we have initialized the camera and after the time dot sleep.
So we open the file with the bot telegram bot token.
We create an updater, we create a dispatcher, and then we have some command handler.
I'm going to remove that.
And I'm just going to put start and we won't get photo and I'm just going to put get photo.
So we have a start handler and a get photo handler.
Let's create those handlers here.
Actually, the start handler is going to be the same as before.
Let's just put welcome.
And let's create the.
Get photo handler with update context.
And so what we need to do is we need to retrieve the photo that was taken and we need to send it with
bot dot send photo.
So let's do we've open and that's going to be image file name.
Okay.
We already have it.
And with RB as so let's say as photo then we do context dot dot dot send photo with chat id is equal
to let's just reply to the.
A date that effective chatted.
So whoever is triggering the bot and then we don't have text but we have photo is equal to photo.
All right.
This seems okay, let's actually add a print here.
Print camera.
Okay.
And maybe add here print sending photo to telegram.
And one other thing is we need to stop the updater.
So I'm going to add a try here.
With the keyboard interrupt exception.
Except keyboard.
Interrupt.
And we do.
Let's do print stopping telegram of data and a data dot.
Stop.
Okay, let's try this.
Let's try this.
So I'm going to run.
We have camera.
Okay.
So it's going to take photo every two seconds.
And let's go to Telegram.
Let's do slash get photo.
Sending photo to telegram.
You can see here and we receive the photo.
So may take some time.
Depends on your internet connection speed also and well, different things.
But you can see we have the photo.
If I do start also I have the start message.
Welcome.
Okay.
If I do get photo again.
So you can see here we have an error.
Well, because probably we are just requesting the photo when it has been updated.
Okay.
So let's try again.
Okay.
And now it's working with a different photo.
And so, well, you can see it's working.
Maybe an improvement could be that you make sure that the file can be opened because, well, if you
actually take a photo and update the file and try to send the file at the same time, that's not going
to work.
So that could be an improvement you could do or maybe try to use a different file name for each photo.
Okay.
So I'll leave that up to you as an improvement of this activity.
And then, well, we still need to do something else here is that when we have pressed on the button,
we need to send the photo as well.
So I'm going to initialize the communication.
And I'm going to just take from activity eight.
Just going to take this code and let's put it here.
So we initialize the camera.
Let's just.
But the code here.
So first we connect to Serial and then we're going to put that.
He may be after the camera.
So we initialize, shall we initialize the camera?
And then we give some time for both to be set up.
And here I don't need to do sleep.
Two and three, that's going to be five.
I just keep three for both of them.
That's going to be okay.
And I just print.
Um, camera.
Okay.
Here.
Okay.
Here.
And reset the input buffer.
Okay, so everything is correctly set up.
First sale, then camera.
And I am going to.
Before I forget, I'm going to close here, print closing serial, and I'm going to do sale dot close.
Okay.
So we don't forget and then I'm going to read here from sale.
So we have this action which is to take photo and I'm going to add another action which can be after
or before.
It doesn't matter.
Read from sale.
So if, say, dot in waiting is greater than zero, we do messages is equal to say dot read line dot
decode and UTF eight and strip and then we check if message is equal to button.
Let's check this again.
Button pressed.
If this is button pressed, what we do is we send the message.
So to send the message to Telegram.
So we send the photo.
Actually to send the photo to telegram, we need to add a bot.
So I'm going to add from telegram import bot object and let's go where we initialize telegram.
Here we have an updater I'm going to create.
Bot here is equal to bot with the token with the same token.
We have both a boat and an update.
Now we can use this bot to do bot dot send photo chat ID.
So what is the chat ID here?
We don't respond.
Okay, we don't respond.
To a message where we have the update with the effective chat ID.
So we need to know what is the chat ID of where we want to send.
So I'm going to just use the chat ID.
But that was here in the activity seven chat.
I'm just going to use the one that we found previously.
So let's put it there.
Chat ID.
And so I do chat.
ID is equal to chat ID and then what?
I need to also open the photo.
So I'm going to do this.
I sure am going to do this.
So I'm going to do we've let's put the correct indentation.
So when the button is pressed, we open the image file name as a photo and then we send photo is equal
to photo.
We send the photo to the chat ID, so to the chat to the group chat.
The same way we send it when we request with the get photo command.
So now let's run that code again.
Okay, we have an error here.
Cell is not defined here at the beginning.
Well, actually, this is the problem of when you take some code from other programs that maybe you
forget to take everything.
So import.
So that's going to be better like this.
Okay.
Could not connect to sale.
Well, because actually I have to put one.
Okay.
Because if you remember here, I have one, so it's important you check the sale before.
So I'm going to use one here.
Run the script.
Successfully connected to sale camera.
Okay.
Okay, so it's taking a photo every two seconds.
Let's open the telegram app.
And let's press on the push button.
Sending photo to telegram.
And you can see we receive a new photo.
Well, that is me pressing on the push button.
And if I do get a photo, I can also get a photo.
So here we can see that this is not the right timing.
Let's try again and we get a new photo.
All right.
So in this program, you have mixed so on the Python program, on the Raspberry Pi, you have used serial
communication with the camera, with the telegram bot, and now you can mix Raspberry Pi functionalities
and Arduino functionalities in a more complex program.
And this is perfect because we are just about to start the final project of this course.