WEBVTT

00:00.110 --> 00:06.110
This is step number nine of the final project, and that will be the last step where we write Python

00:06.110 --> 00:06.760
code.

00:06.770 --> 00:10.460
After this, our Python program is going to be complete for this project.

00:10.460 --> 00:17.690
And so what we still need to do now is to handle the deny the deny comment from Telegram.

00:17.840 --> 00:25.430
So when we have an open door request, we can deny so that we don't open the door and we print an error

00:25.430 --> 00:26.900
message for the user.

00:26.900 --> 00:31.550
And as you will see, this step is going to be quite quick and quite easy now because when all the hard

00:31.550 --> 00:32.840
work was done before.

00:32.840 --> 00:37.790
So now we just need to add one functionality on top of what we have done.

00:37.820 --> 00:39.770
I'm going to also do new.

00:42.140 --> 00:45.110
And just paste that here.

00:46.480 --> 00:49.480
Uh, let's remove the lines we don't need here.

00:52.940 --> 00:56.410
Just one here and let's name it.

00:56.420 --> 00:58.670
So let's save it as.

01:01.930 --> 01:02.710
Intercom.

01:03.790 --> 01:05.170
Step nine.

01:06.130 --> 01:10.300
So what we need to do now is to add a callback for the deny command.

01:10.330 --> 01:12.040
We already have the updater.

01:12.580 --> 01:16.290
Everything is correctly set up, so we don't need to do that again.

01:16.300 --> 01:19.640
We just need to create here another callback.

01:19.660 --> 01:28.990
So after the open, for example, the deny access handler a date and context.

01:30.540 --> 01:36.300
But pass And let's add let's not forget that because if you forget, that's of course not going to work.

01:36.330 --> 01:45.990
Dispatcher Dot add Handler Command handler with deny.

01:46.230 --> 01:48.800
Okay, let's check again.

01:48.810 --> 01:50.040
We ask for deny.

01:50.070 --> 01:53.220
So let's get exactly deny.

01:53.910 --> 02:03.570
And the function is going to be deny access handler and I'm going to come back to the run async just

02:03.570 --> 02:04.410
in a second.

02:04.800 --> 02:09.090
So what do we want to do in this deny access handler?

02:09.120 --> 02:14.760
Well, first, what we don't want to do is to open the door so we're not going to send open door and

02:14.760 --> 02:16.590
we are not going to send close door.

02:16.940 --> 02:17.220
Okay.

02:17.220 --> 02:21.840
So because Well, I have put some print here, so let's do print.

02:22.990 --> 02:25.840
Denying access.

02:26.940 --> 02:31.590
And to deny the access we do send to Arduino.

02:31.590 --> 02:40.380
So we're going to send something for the LCD screen, a sound and also put the Led to read.

02:41.340 --> 02:43.080
Okay, So let's put three.

02:43.980 --> 02:52.230
The first command is print text access denied.

02:53.580 --> 02:58.950
And then the second one is going to be play buzzer.

02:59.310 --> 03:02.670
So this time we can use, let's say 200.

03:02.670 --> 03:06.360
So we have 300 here for 500 milliseconds.

03:06.360 --> 03:10.080
So half a second here, when we close the door, we have 200.

03:10.080 --> 03:14.160
So that's lower pitch with half seconds.

03:14.190 --> 03:19.590
Now we are going to send also 200, but for one complete second.

03:19.980 --> 03:25.080
Okay, you can experiment with different frequencies to find what you like best.

03:25.080 --> 03:25.500
Okay.

03:25.650 --> 03:36.520
I'm just going to keep things simple here and set led with 255 zero and zero.

03:38.440 --> 03:38.770
Okay.

03:38.770 --> 03:42.700
After we send those commands, we're going to wait a few seconds.

03:42.730 --> 03:43.600
Time.sleep.

03:43.600 --> 03:45.340
Let's wait for five seconds.

03:45.940 --> 03:46.180
Okay?

03:46.180 --> 03:50.680
So we can leave some time for the user to see that the access is denied.

03:50.890 --> 03:54.520
And after five seconds we come back to the initial state.

03:54.730 --> 04:00.730
So we will send to Arduino three times.

04:06.110 --> 04:07.850
And what we do here?

04:07.880 --> 04:14.300
Well, we do print, text, push on, button to call.

04:14.720 --> 04:17.980
So the exact same message.

04:17.990 --> 04:25.160
And then well, actually, we don't need to play the buzzer anymore because here we play a sound when

04:25.160 --> 04:27.620
we open the door, when we close the door.

04:27.620 --> 04:30.380
So that's a physical action or when we deny access.

04:30.380 --> 04:33.980
But after that, no need to add another sound.

04:34.010 --> 04:42.730
So let's do set directly with 000 and then so red, green and blue on.

04:43.130 --> 04:51.830
And what we can do also is like we did for the open command is that when we deny access from Telegram,

04:51.830 --> 05:04.580
we can say bot dot send message chat ID is equal to dot effective chat.

05:05.990 --> 05:20.950
Dot id text denying access so that when we do the deny command here from Telegram, we also have a confirmation.

05:20.960 --> 05:25.040
Okay, here we have a confirmation opening the door so we know that's happening.

05:25.040 --> 05:30.650
And with denying access, we have a confirmation that we are denying the access.

05:30.770 --> 05:36.710
And now, well, we're going to have the same problem as with the open door handler.

05:36.710 --> 05:41.240
So basically here, if you don't put run async.

05:42.130 --> 05:43.150
All the callbacks.

05:43.150 --> 05:49.120
So all the deny access you're going to call are going to be put in a queue and are going to be executed

05:49.120 --> 05:52.150
one by one instead of in parallel.

05:52.150 --> 05:55.270
And you don't want that because you have a Time.sleep five here.

05:55.270 --> 06:00.820
So if you press four times, then you're just going to do that for five seconds and then five seconds

06:00.820 --> 06:05.350
Et-cetera So you're going to continue to run the callbacks for 20s.

06:06.370 --> 06:07.570
So we do.

06:08.690 --> 06:16.310
Run async also for the deny request.

06:16.310 --> 06:20.870
And we also going to use those two flags here.

06:20.900 --> 06:21.350
Okay.

06:21.350 --> 06:30.510
So Global Open, the request and global handling Doe.

06:31.220 --> 06:39.830
So we are first going to do if open door request and put that inside the if.

06:41.370 --> 06:41.940
Why is that?

06:41.940 --> 06:45.630
Because we don't want to deny an access if we don't have the request.

06:45.660 --> 06:45.920
Okay.

06:45.930 --> 06:50.040
Imagine that your intercom at your home is just printing access denied.

06:50.070 --> 06:53.610
By itself, without anyone to request to open the door.

06:53.640 --> 06:54.970
That's going to be a bit weird.

06:54.990 --> 07:02.460
Okay, so if we have an open door request, well, actually, from telegram, we can send either open

07:02.460 --> 07:03.650
or deny comment.

07:03.660 --> 07:15.070
So we either going to go here or to go here, and then at the end we can do open door request is false.

07:15.090 --> 07:15.630
Okay.

07:16.230 --> 07:18.330
So then we can try again.

07:18.570 --> 07:26.130
And also we are not going to deny the access if we have the handling door currently on.

07:26.130 --> 07:31.940
So basically, if we are opening the door, we're not going to deny access at the same time.

07:31.950 --> 07:32.430
Okay.

07:32.430 --> 07:36.360
So I'm going to put not handling door.

07:36.390 --> 07:38.400
This is just a question of logic here.

07:38.400 --> 07:44.020
And the same thing is that if we are currently denying the access, we are not going to open the door.

07:44.020 --> 07:49.120
We are going to wait that this action is finished to start another action.

07:49.120 --> 08:01.000
And so I'm going to put here handling door is equal to true and here handling door is equal to false.

08:01.000 --> 08:07.330
So even if we don't physically handle the door, we handle the door mechanism.

08:07.330 --> 08:07.630
Okay?

08:07.630 --> 08:10.770
We just keep it locked for five seconds.

08:10.780 --> 08:10.990
Okay?

08:10.990 --> 08:16.870
So if you send the deny command, you're going to have handling door is equal to true for the duration

08:16.870 --> 08:17.590
of.

08:18.420 --> 08:18.860
This.

08:18.870 --> 08:20.400
So basically five seconds.

08:20.400 --> 08:26.910
And then if you try to open the door while you are in this, that's not going to work because the handling

08:26.910 --> 08:28.710
door is going to be true.

08:28.740 --> 08:29.160
Okay.

08:29.160 --> 08:35.310
So with those two flags, we make sure that we only process the comment from Telegram when we have a

08:35.310 --> 08:38.220
request and that we only process one comment.

08:38.250 --> 08:46.170
Either we open the door once or we deny the access once, and then we need to wait for another open

08:46.170 --> 08:47.160
door request.

08:47.490 --> 08:47.790
Okay.

08:47.790 --> 08:56.190
And here we don't send requests if we are currently inside the request or if we are currently handling

08:56.190 --> 08:56.730
the door.

08:56.910 --> 08:57.480
All right.

08:57.480 --> 08:59.380
So now is the time to try this.

08:59.400 --> 09:00.870
Let's run that.

09:00.870 --> 09:03.420
So the Arduino is still the same.

09:04.050 --> 09:04.920
Could not connect.

09:04.920 --> 09:08.640
Yes, if I actually plug the Arduino.

09:11.180 --> 09:11.570
Okay.

09:11.570 --> 09:16.250
Starting and then main loop is starting.

09:16.250 --> 09:19.850
So let's press on the push button.

09:19.850 --> 09:21.560
We have a notification.

09:22.250 --> 09:26.230
Someone is ringing the bell and that someone is.

09:26.240 --> 09:28.280
Well, we don't see that person.

09:28.280 --> 09:33.080
So let's deny the access and let's look at what we have.

09:33.950 --> 09:34.250
Okay.

09:34.250 --> 09:37.640
We have a sound from the buzzer read Access Denied.

09:37.640 --> 09:41.720
And after five seconds we have again the initial state.

09:41.720 --> 09:43.310
So that's correctly working.

09:43.310 --> 09:49.310
Now what you can do, you can try again and you can deny the access again or you can accept and open

09:49.310 --> 09:49.910
the door.

09:50.150 --> 09:50.780
ET cetera.

09:50.780 --> 09:51.470
ET cetera.

09:51.470 --> 09:57.050
So let's correctly stop this program with control C Here you can see that it was quite faster.

09:57.080 --> 09:57.470
Okay.

09:57.490 --> 09:59.780
Depends on the updater, I guess.

10:00.110 --> 10:03.200
And, well, our python code is now finished.

10:03.200 --> 10:08.150
So this code is actually you can see 130 lines.

10:08.660 --> 10:15.660
And as a quick recap, we have so we have some inputs, then some global variables.

10:15.690 --> 10:22.110
We initialize the serial, we have a function for the serial, we initialize the camera and then we

10:22.110 --> 10:23.970
have our telegram callbacks.

10:23.970 --> 10:29.040
So three callbacks, we initialize the bot and the updater.

10:30.270 --> 10:33.990
And after everything has been initialized, we wait for three seconds.

10:34.020 --> 10:40.530
We put the initial state of the application and we start the main loop in the main loop.

10:40.560 --> 10:45.120
Well, basically we just read from Serial, and if we have received something, we check if we have

10:45.120 --> 10:51.810
a button pressed comment and if we have a button pressed comment, we try to send a request to telegram

10:51.810 --> 10:57.810
with a message, a photo, and some instructions so that the user can give open or deny.

10:57.810 --> 11:00.540
And that's going to call the telegram callback.

11:00.570 --> 11:01.050
All right.

11:01.050 --> 11:04.980
And that is the end of this step number nine of the final project.
