WEBVTT

00:00.050 --> 00:05.750
This is the solution of the previous activity where you have to take a photo every two seconds on the

00:05.750 --> 00:11.960
Raspberry Pi and then send the photo when you request it, either from Telegram or from the Arduino.

00:12.320 --> 00:14.900
And let's start with this Arduino code, actually.

00:14.900 --> 00:18.590
And well, this code, we've already done that multiple times.

00:18.590 --> 00:24.590
So I'm going to open activity number six, for example.

00:25.070 --> 00:29.420
And on the activity number six, I have the code to read from the push button.

00:29.420 --> 00:31.130
I'm just going to copy this.

00:32.460 --> 00:38.280
And put that here and I'm just going to remove what I don't need.

00:38.850 --> 00:44.520
So we were using the servo motor, but I'm not going to use Servo here, just the push button.

00:45.670 --> 00:47.350
So I'm going to remove that.

00:47.800 --> 00:51.280
I'm going to keep the sire, of course, with this baud rate.

00:51.490 --> 00:54.250
And then I'm going to remove this part.

00:56.130 --> 01:00.710
So as you can see, with all the code that we did is quite module able.

01:00.720 --> 01:05.610
Okay, We can just take some parts, remove some other parts and it's quite easy.

01:05.610 --> 01:12.600
And now when we have the complete code to read from the push button and send button pressed when the

01:12.600 --> 01:15.430
button has been pressed with the debounce mechanism.

01:15.450 --> 01:15.990
Okay.

01:16.320 --> 01:20.280
I am going to upload this to the Arduino directly.

01:21.420 --> 01:23.550
Activity nine.

01:25.410 --> 01:26.460
Let's upload.

01:28.470 --> 01:34.650
And what can't open with my Arduino is not plugged to a Raspberry Pi.

01:35.130 --> 01:37.200
So that's not going to work.

01:38.630 --> 01:39.440
You can see now.

01:39.440 --> 01:41.600
Okay, That's something very interesting.

01:41.690 --> 01:44.450
This time I don't have t zero.

01:44.480 --> 01:46.820
I have t one.

01:47.000 --> 01:53.390
Okay, let's try to disconnect again and connect back.

01:53.990 --> 01:56.240
Let's see again what we have.

01:56.510 --> 02:02.930
And we still have ACM one, so I'm going to use ACM one and that's something to know when we try to

02:02.930 --> 02:04.220
connect from the Raspberry Pi.

02:04.250 --> 02:04.790
Okay.

02:05.120 --> 02:07.250
So upload.

02:10.210 --> 02:11.470
Down uploading.

02:11.470 --> 02:13.960
Let's just check that.

02:13.960 --> 02:18.040
If I press on the push button, I get button pressed.

02:18.070 --> 02:19.510
That's correct.

02:19.630 --> 02:22.060
Now let's go to the Raspberry Pi side.

02:22.060 --> 02:23.530
So we need to do a few things.

02:23.530 --> 02:27.490
We need to read the file to get the push button notification.

02:27.490 --> 02:34.120
We need to take a photo every two seconds and we need to also send it to Telegram and receive callbacks

02:34.120 --> 02:34.990
from Telegram.

02:35.560 --> 02:37.360
Let's do the first thing here.

02:37.360 --> 02:39.670
Maybe to take a photo every two seconds.

02:40.330 --> 02:41.620
I'm going to remove that.

02:46.990 --> 02:57.940
Okay, let's import So from pi camera import pi camera like this and then let's create a camera.

02:57.940 --> 03:02.200
So camera is equal to pi camera.

03:02.230 --> 03:04.240
Let's put the settings directly.

03:04.810 --> 03:06.640
Camera, dot.

03:07.660 --> 03:15.760
Resolution and I'm going to use a quite small one because, well, we don't need more actually, for

03:15.760 --> 03:25.630
what we want to do in camera dot rotation is equal to 180 because I have seen before that the images

03:25.630 --> 03:27.690
I take are upside down.

03:27.700 --> 03:32.740
So check that for yourself before and then maybe use the camera dot rotation setting.

03:32.740 --> 03:41.980
Let's add an image folder name, which is slash home slash pie slash camera.

03:41.980 --> 03:45.640
Okay, let's actually go in the file manager and see.

03:46.000 --> 03:53.620
We have the camera folder here already with new image and new image too.

03:53.650 --> 03:55.660
Well, let's keep this like that.

03:56.260 --> 04:02.380
So the folder exists, but I'm just going to make sure it exists just in case.

04:02.380 --> 04:02.800
Okay.

04:02.800 --> 04:07.160
If you remove it later on and if you try to run this program again.

04:07.160 --> 04:16.910
So let's import os if OS dot path dot exists.

04:17.760 --> 04:21.080
We've image for folder name.

04:21.090 --> 04:30.390
Actually, if it doesn't exist we do s dot mkdir with.

04:31.190 --> 04:31.930
The following.

04:32.000 --> 04:33.560
Okay, that's very quick to do.

04:34.820 --> 04:38.090
And now we have the folder and let's do image.

04:39.340 --> 04:44.710
File name is equal to image folder name plus Slash.

04:45.100 --> 04:47.900
Let's just call it Photo.jpg.

04:49.940 --> 04:50.360
G.

04:50.830 --> 04:56.080
Okay, so every time we're going to take a photo here, we're going to replace the previous one.

04:56.110 --> 05:04.780
Then we need a time dot sleep to actually, maybe if I close this string, it's going to be better.

05:04.780 --> 05:09.760
And we need the time dot slip too, to allow the camera to adjust to the luminosity.

05:09.880 --> 05:10.480
And then.

05:10.480 --> 05:13.250
Well, let's take a photo every two seconds.

05:13.270 --> 05:15.670
So let's do while true.

05:16.480 --> 05:29.580
Let's do camera dot capture with image file name and well, I'm not going to use time dot sleep to hear

05:29.580 --> 05:30.060
why.

05:30.060 --> 05:33.840
Well, I could use that actually just to take a photo every two seconds.

05:33.840 --> 05:40.890
But then as we are going to need to read from Serial, I need to do a non-blocking sleep.

05:40.890 --> 05:43.200
So I'm going to add here.

05:44.700 --> 06:00.030
Last time photo taken equal to and let's import time, time, time and let's say photo delay is equal

06:00.030 --> 06:00.660
to.

06:01.980 --> 06:03.270
Two seconds.

06:03.630 --> 06:08.760
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

06:08.790 --> 06:17.760
time, dot sleep time, dot sleep with zero one, which means that we are still going to block a bit,

06:17.760 --> 06:24.960
but basically just ten milliseconds so we don't use too much CPU and that's going to be completely fine

06:24.960 --> 06:25.860
for the rest.

06:26.220 --> 06:37.020
And then we do if time dot So let's actually create a time now variable with time, dot time.

06:37.740 --> 06:45.450
So if time now minus last time photo taken greater or equal than the photo delay.

06:46.110 --> 06:46.500
Okay.

06:46.870 --> 06:48.690
I've already done that before.

06:48.720 --> 06:50.910
Then we do last time.

06:50.910 --> 06:52.800
Photo taken is equal.

06:54.560 --> 06:58.220
To time now and we take the.

06:59.170 --> 06:59.960
Oh two.

07:00.490 --> 07:02.320
All right, let's try just.

07:02.530 --> 07:04.730
Let's just try this part of the code.

07:04.750 --> 07:08.380
Let's run this so we don't have any communication with Scialfa.

07:08.380 --> 07:10.570
No activity.

07:11.050 --> 07:11.860
Nine.

07:14.180 --> 07:17.860
And we don't have anything printed, of course, so we don't have any output.

07:17.870 --> 07:21.200
But let's go to our folder here.

07:22.010 --> 07:24.110
Camera We have photo.

07:24.740 --> 07:26.330
That's one photo.

07:26.330 --> 07:30.860
And let's wait two seconds and let's click on this.

07:31.970 --> 07:34.910
Okay, let's open it.

07:35.240 --> 07:37.580
Well, we have a problem here.

07:37.610 --> 07:39.230
Let's just refresh.

07:40.160 --> 07:40.700
Okay.

07:40.700 --> 07:43.650
We have another photo that was taken That's different.

07:43.670 --> 07:43.910
Okay.

07:43.940 --> 07:44.540
Same file.

07:44.540 --> 07:45.950
It has been rewritten.

07:45.980 --> 07:53.030
Maybe you need to just refresh the view on the file manager and then you have the photo.

07:53.150 --> 07:53.630
Great.

07:53.630 --> 07:55.670
So this part is working.

07:55.910 --> 07:59.240
And now what we need to do, we need to send that to telegram.

07:59.240 --> 08:04.310
So we're going to first use the way that we can request from Telegram.

08:04.340 --> 08:04.850
Okay.

08:05.540 --> 08:08.870
So for that I'm going to go back to activity eight.

08:08.900 --> 08:11.420
I'm just going to use the code we did before.

08:11.690 --> 08:11.900
Okay.

08:11.900 --> 08:15.810
So we're going to import the data and the common handler.

08:18.830 --> 08:21.200
We are going to create.

08:25.350 --> 08:27.570
And the data a dispatcher.

08:27.720 --> 08:31.380
And then we're going to add some handlers and start pulling.

08:31.380 --> 08:33.150
I'm going to copy all of that.

08:34.660 --> 08:36.310
And where do we put it?

08:36.340 --> 08:41.740
Well, let's just put that after we have initialized the camera and after the time dot sleep.

08:41.920 --> 08:47.170
So we open the file with the bot telegram bot token.

08:47.320 --> 08:52.540
We create an updater, we create a dispatcher, and then we have some command handler.

08:52.540 --> 08:53.950
I'm going to remove that.

08:55.720 --> 09:04.840
And I'm just going to put start and we won't get photo and I'm just going to put get photo.

09:04.840 --> 09:07.240
So we have a start handler and a get photo handler.

09:07.240 --> 09:09.400
Let's create those handlers here.

09:09.640 --> 09:13.260
Actually, the start handler is going to be the same as before.

09:13.270 --> 09:15.010
Let's just put welcome.

09:19.150 --> 09:22.600
And let's create the.

09:27.050 --> 09:33.020
Get photo handler with update context.

09:34.860 --> 09:41.670
And so what we need to do is we need to retrieve the photo that was taken and we need to send it with

09:41.670 --> 09:43.920
bot dot send photo.

09:44.070 --> 09:50.550
So let's do we've open and that's going to be image file name.

09:50.650 --> 09:50.870
Okay.

09:50.880 --> 09:52.050
We already have it.

09:53.100 --> 10:09.690
And with RB as so let's say as photo then we do context dot dot dot send photo with chat id is equal

10:09.690 --> 10:11.460
to let's just reply to the.

10:12.800 --> 10:17.170
A date that effective chatted.

10:17.180 --> 10:26.780
So whoever is triggering the bot and then we don't have text but we have photo is equal to photo.

10:27.260 --> 10:27.590
All right.

10:27.590 --> 10:30.650
This seems okay, let's actually add a print here.

10:30.680 --> 10:33.950
Print camera.

10:33.980 --> 10:34.700
Okay.

10:36.650 --> 10:45.590
And maybe add here print sending photo to telegram.

10:48.660 --> 10:53.220
And one other thing is we need to stop the updater.

10:53.220 --> 10:55.500
So I'm going to add a try here.

10:58.230 --> 11:00.840
With the keyboard interrupt exception.

11:03.130 --> 11:05.350
Except keyboard.

11:07.770 --> 11:08.880
Interrupt.

11:11.040 --> 11:12.180
And we do.

11:12.360 --> 11:23.550
Let's do print stopping telegram of data and a data dot.

11:24.000 --> 11:24.660
Stop.

11:25.200 --> 11:26.660
Okay, let's try this.

11:26.670 --> 11:28.030
Let's try this.

11:28.030 --> 11:29.100
So I'm going to run.

11:32.070 --> 11:33.070
We have camera.

11:33.090 --> 11:33.720
Okay.

11:33.720 --> 11:36.210
So it's going to take photo every two seconds.

11:36.330 --> 11:39.030
And let's go to Telegram.

11:39.060 --> 11:42.720
Let's do slash get photo.

11:43.770 --> 11:45.060
Sending photo to telegram.

11:45.060 --> 11:48.180
You can see here and we receive the photo.

11:48.180 --> 11:49.770
So may take some time.

11:49.770 --> 11:53.970
Depends on your internet connection speed also and well, different things.

11:53.970 --> 11:55.830
But you can see we have the photo.

11:56.460 --> 11:59.610
If I do start also I have the start message.

12:00.660 --> 12:01.470
Welcome.

12:02.010 --> 12:02.520
Okay.

12:02.520 --> 12:06.810
If I do get photo again.

12:08.020 --> 12:09.550
So you can see here we have an error.

12:09.580 --> 12:15.160
Well, because probably we are just requesting the photo when it has been updated.

12:15.510 --> 12:15.620
Okay.

12:15.640 --> 12:17.110
So let's try again.

12:22.730 --> 12:23.090
Okay.

12:23.090 --> 12:25.940
And now it's working with a different photo.

12:27.010 --> 12:29.500
And so, well, you can see it's working.

12:29.500 --> 12:35.560
Maybe an improvement could be that you make sure that the file can be opened because, well, if you

12:35.560 --> 12:40.420
actually take a photo and update the file and try to send the file at the same time, that's not going

12:40.420 --> 12:40.960
to work.

12:40.960 --> 12:46.600
So that could be an improvement you could do or maybe try to use a different file name for each photo.

12:46.840 --> 12:47.010
Okay.

12:47.020 --> 12:50.680
So I'll leave that up to you as an improvement of this activity.

12:50.680 --> 12:56.080
And then, well, we still need to do something else here is that when we have pressed on the button,

12:56.080 --> 12:57.910
we need to send the photo as well.

12:58.090 --> 13:01.510
So I'm going to initialize the communication.

13:02.260 --> 13:05.500
And I'm going to just take from activity eight.

13:05.550 --> 13:11.080
Just going to take this code and let's put it here.

13:11.980 --> 13:13.600
So we initialize the camera.

13:14.880 --> 13:16.650
Let's just.

13:17.850 --> 13:19.980
But the code here.

13:20.160 --> 13:25.500
So first we connect to Serial and then we're going to put that.

13:27.570 --> 13:29.190
He may be after the camera.

13:29.190 --> 13:32.400
So we initialize, shall we initialize the camera?

13:32.400 --> 13:35.970
And then we give some time for both to be set up.

13:35.970 --> 13:38.730
And here I don't need to do sleep.

13:38.730 --> 13:40.350
Two and three, that's going to be five.

13:40.350 --> 13:43.140
I just keep three for both of them.

13:43.140 --> 13:44.520
That's going to be okay.

13:44.520 --> 13:45.600
And I just print.

13:46.900 --> 13:47.730
Um, camera.

13:47.740 --> 13:48.190
Okay.

13:48.190 --> 13:48.550
Here.

13:49.060 --> 13:49.420
Okay.

13:49.420 --> 13:50.110
Here.

13:50.380 --> 13:52.570
And reset the input buffer.

13:53.020 --> 13:55.210
Okay, so everything is correctly set up.

13:55.210 --> 13:59.110
First sale, then camera.

13:59.380 --> 14:01.510
And I am going to.

14:01.810 --> 14:13.390
Before I forget, I'm going to close here, print closing serial, and I'm going to do sale dot close.

14:13.700 --> 14:13.760
Okay.

14:13.840 --> 14:18.880
So we don't forget and then I'm going to read here from sale.

14:18.880 --> 14:28.310
So we have this action which is to take photo and I'm going to add another action which can be after

14:28.310 --> 14:28.690
or before.

14:28.720 --> 14:29.740
It doesn't matter.

14:30.490 --> 14:32.320
Read from sale.

14:32.800 --> 14:46.260
So if, say, dot in waiting is greater than zero, we do messages is equal to say dot read line dot

14:46.370 --> 15:01.760
decode and UTF eight and strip and then we check if message is equal to button.

15:01.760 --> 15:03.470
Let's check this again.

15:03.470 --> 15:04.490
Button pressed.

15:09.720 --> 15:14.820
If this is button pressed, what we do is we send the message.

15:14.820 --> 15:16.560
So to send the message to Telegram.

15:16.560 --> 15:17.460
So we send the photo.

15:17.460 --> 15:21.810
Actually to send the photo to telegram, we need to add a bot.

15:22.290 --> 15:33.480
So I'm going to add from telegram import bot object and let's go where we initialize telegram.

15:33.480 --> 15:35.790
Here we have an updater I'm going to create.

15:35.790 --> 15:42.930
Bot here is equal to bot with the token with the same token.

15:45.180 --> 15:47.370
We have both a boat and an update.

15:47.640 --> 15:55.970
Now we can use this bot to do bot dot send photo chat ID.

15:55.980 --> 15:57.810
So what is the chat ID here?

15:57.810 --> 15:59.670
We don't respond.

16:00.090 --> 16:02.040
Okay, we don't respond.

16:04.200 --> 16:08.340
To a message where we have the update with the effective chat ID.

16:08.460 --> 16:12.480
So we need to know what is the chat ID of where we want to send.

16:13.320 --> 16:15.600
So I'm going to just use the chat ID.

16:17.660 --> 16:20.510
But that was here in the activity seven chat.

16:20.720 --> 16:23.750
I'm just going to use the one that we found previously.

16:25.250 --> 16:27.770
So let's put it there.

16:28.850 --> 16:29.510
Chat ID.

16:32.980 --> 16:34.080
And so I do chat.

16:34.120 --> 16:38.080
ID is equal to chat ID and then what?

16:38.080 --> 16:40.780
I need to also open the photo.

16:42.770 --> 16:45.170
So I'm going to do this.

16:47.290 --> 16:48.850
I sure am going to do this.

16:49.990 --> 16:55.210
So I'm going to do we've let's put the correct indentation.

16:56.100 --> 17:03.660
So when the button is pressed, we open the image file name as a photo and then we send photo is equal

17:03.660 --> 17:04.860
to photo.

17:04.890 --> 17:10.860
We send the photo to the chat ID, so to the chat to the group chat.

17:11.890 --> 17:16.510
The same way we send it when we request with the get photo command.

17:17.080 --> 17:19.630
So now let's run that code again.

17:23.370 --> 17:25.140
Okay, we have an error here.

17:25.440 --> 17:30.870
Cell is not defined here at the beginning.

17:34.850 --> 17:41.630
Well, actually, this is the problem of when you take some code from other programs that maybe you

17:41.630 --> 17:43.580
forget to take everything.

17:43.580 --> 17:44.660
So import.

17:44.810 --> 17:46.940
So that's going to be better like this.

17:49.950 --> 17:50.340
Okay.

17:50.370 --> 17:51.810
Could not connect to sale.

17:51.970 --> 17:59.690
Well, because actually I have to put one.

17:59.700 --> 17:59.880
Okay.

17:59.880 --> 18:06.270
Because if you remember here, I have one, so it's important you check the sale before.

18:06.270 --> 18:09.630
So I'm going to use one here.

18:10.650 --> 18:11.640
Run the script.

18:12.780 --> 18:15.290
Successfully connected to sale camera.

18:15.310 --> 18:15.590
Okay.

18:16.030 --> 18:20.100
Okay, so it's taking a photo every two seconds.

18:20.430 --> 18:23.100
Let's open the telegram app.

18:24.390 --> 18:26.340
And let's press on the push button.

18:27.780 --> 18:29.700
Sending photo to telegram.

18:30.790 --> 18:33.010
And you can see we receive a new photo.

18:33.040 --> 18:36.160
Well, that is me pressing on the push button.

18:36.160 --> 18:41.470
And if I do get a photo, I can also get a photo.

18:41.470 --> 18:46.000
So here we can see that this is not the right timing.

18:46.000 --> 18:50.170
Let's try again and we get a new photo.

18:50.410 --> 18:50.860
All right.

18:50.860 --> 18:57.790
So in this program, you have mixed so on the Python program, on the Raspberry Pi, you have used serial

18:57.790 --> 19:05.440
communication with the camera, with the telegram bot, and now you can mix Raspberry Pi functionalities

19:05.440 --> 19:08.950
and Arduino functionalities in a more complex program.

19:09.130 --> 19:14.290
And this is perfect because we are just about to start the final project of this course.
