WEBVTT

00:00.050 --> 00:03.620
This is step number four for this final project.

00:03.620 --> 00:08.360
And in this step we are going to add the email functionality in our program.

00:08.360 --> 00:10.760
So we already take a photo.

00:10.760 --> 00:13.670
After we detect a movement for more than five seconds.

00:13.670 --> 00:17.450
We also save the photo file names inside the log file.

00:17.450 --> 00:21.410
And now we're going to send also that photo by email.

00:21.410 --> 00:25.430
So let's start and let's initialize an email client.

00:25.430 --> 00:32.480
So with Gmail I'm going to import Gmail here for example here import Gmail.

00:32.480 --> 00:36.800
Then where do I set up my email for example here.

00:36.830 --> 00:39.530
Well you could do it wherever you want.

00:39.560 --> 00:46.220
I'm just going to do it here just after I create the global variables and to set up Gmail I will need

00:46.220 --> 00:48.800
the email address and also the password.

00:48.800 --> 00:50.960
And this is the password I have already saved.

00:50.960 --> 00:54.920
So in the previous lesson in the section on emails.

00:54.920 --> 00:59.000
So I'm first going to get the password from the file where I saved it.

00:59.030 --> 01:00.800
Let's add a comment here.

01:00.830 --> 01:04.810
Setup email and then password.

01:06.670 --> 01:08.350
So that's an empty string.

01:08.350 --> 01:09.010
And then I do.

01:09.040 --> 01:21.580
We've open and I have saved the password in home slash pi slash dot local slash share.

01:21.610 --> 01:26.650
Slash dot email password.

01:26.680 --> 01:27.160
All right.

01:27.190 --> 01:28.630
That's where I saved it.

01:28.720 --> 01:33.820
I'm going to open this with only read permission because I don't need more than that.

01:33.880 --> 01:41.170
And then as f for file and the password is going to be f dot read.

01:41.170 --> 01:44.770
And I'm also going to add dot a strip.

01:45.940 --> 01:46.240
Okay.

01:46.240 --> 01:50.980
Just to remove any unwanted space at the end or stuff like that.

01:50.980 --> 01:53.140
So we are sure that we only have the password.

01:53.140 --> 01:57.130
Then I can create my email client.

01:57.130 --> 02:05.610
Let's just name it YAG as we did before is equal to YAG mail dot SMTP uppercase.

02:05.640 --> 02:08.460
And that's going to be the email address you used.

02:08.460 --> 02:11.760
So the one you created as the sender of the email.

02:11.790 --> 02:12.000
Okay.

02:12.030 --> 02:20.400
So formula was course dot Raspberry Pi at gmail.com for you is going to be different.

02:21.180 --> 02:22.920
And then the password.

02:24.720 --> 02:31.740
Once we are done with this I can also do print email setup okay.

02:32.040 --> 02:32.490
All right.

02:32.490 --> 02:33.720
So the email is set up.

02:33.720 --> 02:40.080
Then what we're going to do is we're going to after here we take a photo and we update the photo log

02:40.110 --> 02:40.680
file.

02:40.680 --> 02:44.400
We are also going to send that photo by email.

02:44.490 --> 02:47.550
So let's create a function as we did before.

02:47.550 --> 02:58.830
Here I'm going to create a third function def send a photo by email for example.

02:59.700 --> 03:02.610
And I will pass as an argument the email.

03:03.830 --> 03:05.000
Uh, client.

03:05.000 --> 03:07.400
I could name it my client as I want.

03:07.400 --> 03:09.620
And also the file name.

03:09.650 --> 03:09.890
Okay.

03:09.920 --> 03:10.850
For the photo.

03:11.210 --> 03:15.830
And here, once again, I don't use the global variable directly.

03:15.830 --> 03:18.650
I'm going to pass the variable when I call the function.

03:18.650 --> 03:21.020
So then I get it as a parameter here.

03:21.020 --> 03:25.760
So I could basically export this function and use it anywhere else.

03:25.790 --> 03:31.250
And I will do Gmail client dot send.

03:31.670 --> 03:33.470
I will need to put two.

03:33.500 --> 03:37.700
So that's going to be your uh your email address.

03:37.700 --> 03:38.960
I'm going to change it later.

03:38.960 --> 03:43.700
But for now it's going to be your email address, the one you want to receive the email to.

03:43.730 --> 03:43.970
Okay.

03:44.000 --> 03:46.010
So that one here is a recap.

03:46.010 --> 03:47.540
This one is the sender.

03:47.990 --> 03:50.150
And this one here is the receiver.

03:50.150 --> 03:52.700
And then I put a comma I go to a new line.

03:52.700 --> 04:01.190
Subject is going to be let's say new photo from your from Raspberry Pi.

04:01.220 --> 04:03.020
You can write whatever you want.

04:04.330 --> 04:09.220
I'm gonna also add contents with an S, which is just.

04:09.220 --> 04:14.200
For example, check out the new photo.

04:15.700 --> 04:21.190
And then the most important thing attachment is equal to.

04:21.220 --> 04:23.050
And we directly have the file name.

04:24.220 --> 04:25.660
And I close the parentheses.

04:25.870 --> 04:26.260
All right.

04:26.260 --> 04:31.030
So we directly send the attachments that corresponds to the photo we have taken.

04:31.030 --> 04:34.870
So let's go to here.

04:34.870 --> 04:44.350
When we take the photo and we update the log file I'm going to then call send photo by email I'm going

04:44.380 --> 04:46.810
to provide a YAG.

04:46.840 --> 04:50.950
YAG is the YAG mail client I have created right here.

04:50.980 --> 04:51.640
Okay.

04:51.880 --> 04:55.720
Then I'm going to pass as the first argument here and then the file name.

04:55.720 --> 04:57.040
What is the file name?

04:57.040 --> 05:00.520
Well, this is the file name that I got right here.

05:00.520 --> 05:01.510
Photo file name.

05:01.510 --> 05:05.230
So photo file name.

05:05.230 --> 05:05.300
Finding.

05:05.420 --> 05:10.670
All right, so you see, what we do here is we call three functions one by one.

05:10.670 --> 05:17.300
And sometimes we even like here we even use the result of one function inside the following functions.

05:17.300 --> 05:18.530
I'm going to okay.

05:18.560 --> 05:28.100
This time I'm not going to save it as step three I'm going to do file save as step four okay.

05:29.120 --> 05:35.090
And before we run this let's do a very quick recap because that's going to be the end of that program

05:35.090 --> 05:35.930
here.

05:35.930 --> 05:41.570
So what do we have at the very beginning we have all of the imports for the modules we need to use.

05:41.600 --> 05:43.370
And we have three functions.

05:43.370 --> 05:49.010
We want to take a photo, one to update the photo log file and one to send the photo by email.

05:49.250 --> 05:49.550
All right.

05:49.580 --> 05:51.830
Then we have a bunch of global variables.

05:51.830 --> 05:57.230
And then well basically when we start to run this Python program is going to run all of the codes here.

05:57.230 --> 05:58.910
So it's going to set up the email.

05:58.910 --> 06:01.130
Then it's going to set up the camera.

06:01.730 --> 06:04.430
Then we remove the log file if there is one.

06:04.430 --> 06:06.460
We also set up the Gpios.

06:06.460 --> 06:08.920
And then well we have those two functions.

06:08.920 --> 06:14.680
So those two functions are here because they are directly related to all those callbacks here that we

06:14.680 --> 06:15.610
register.

06:15.610 --> 06:18.910
And in those functions we also use the global variables.

06:18.940 --> 06:19.120
Okay.

06:19.150 --> 06:22.630
So that's why I put them after all of this initialization.

06:22.660 --> 06:28.090
And so after we initialize everything we register the two callbacks and we pause the program.

06:28.090 --> 06:32.290
So we are just waiting for those events to happen.

06:32.440 --> 06:40.600
I'm just going to add a new log here just before we pause to say everything has been setup.

06:41.260 --> 06:41.470
Okay.

06:41.500 --> 06:43.300
So we know that's the end of the setup.

06:43.300 --> 06:49.240
And now to test the program I will need to write something valid here.

06:49.390 --> 06:49.690
All right.

06:49.720 --> 06:53.080
So I will replace that with my own email address.

06:54.880 --> 07:00.730
I'm going to save with Ctrl S and then let's run the program.

07:00.760 --> 07:00.970
Okay.

07:01.000 --> 07:02.560
So let's stop that one.

07:02.590 --> 07:05.320
Let's run the project step for.

07:07.380 --> 07:08.430
Email setup.

07:08.430 --> 07:09.090
Okay.

07:09.120 --> 07:09.750
Then you see.

07:09.780 --> 07:10.830
Camera setup okay.

07:10.860 --> 07:13.740
Previous log file removed Gpios.

07:13.770 --> 07:14.220
Okay.

07:14.250 --> 07:19.170
Everything has been setup, so let's make a movement for five seconds.

07:20.670 --> 07:24.630
Okay so it took a photo and you see sending it by email.

07:24.630 --> 07:27.570
We didn't get any error which means it probably worked.

07:27.600 --> 07:30.000
Now let's check the emails.

07:30.420 --> 07:33.600
And I just received a new email here.

07:33.600 --> 07:40.710
So the last one you see zero minutes ago and what is quite blur because I'm also moving my hand here.

07:40.770 --> 07:43.560
So that's the photo that I have received.

07:43.560 --> 07:46.320
You see new photo from Raspberry Pi.

07:46.350 --> 07:48.060
Check out the new photo.

07:48.060 --> 07:50.640
So it correctly worked.

07:50.640 --> 07:52.620
And every time you see I have a new one.

07:52.620 --> 07:59.280
So every time I receive a new photo here when there is a new movement detected.

08:00.060 --> 08:00.450
All right.

08:00.450 --> 08:02.640
And that's the end of this first program.

08:02.640 --> 08:04.050
So that's the end of step four.

08:04.050 --> 08:08.520
And then in the next step we're going to start the second program for this project.
