WEBVTT

00:00.140 --> 00:04.190
This is step number five of the final project of this course.

00:04.190 --> 00:10.310
And now that we have the Arduino code finished and tested, let's write the code on the Raspberry Pi

00:10.310 --> 00:11.270
with Python.

00:11.270 --> 00:14.060
So the step five is going to be with Python.

00:14.240 --> 00:22.520
Now we are going to use the Raspberry Pi functionalities and also the application logic to define,

00:22.550 --> 00:29.390
okay, what the application will do and of course to interact with the Arduino so we can translate this

00:29.390 --> 00:33.440
application logic into real physical actions.

00:33.440 --> 00:37.820
And we're going to use a slightly different method for the steps here.

00:37.820 --> 00:43.070
So for the Arduino, we just initialize the component and then did the input commands and the output

00:43.070 --> 00:49.310
commands on the Raspberry Pi, we are going to follow the logical order of the user journey.

00:49.310 --> 00:52.550
So the user is basically who is using the application.

00:52.550 --> 00:52.790
Okay.

00:52.790 --> 00:58.970
Because here, as we are writing the application logic, well this is going to be a better way so we

00:58.970 --> 01:05.430
can iterate every time on the next step so we can validate one step and then iterate on the next one.

01:05.430 --> 01:10.950
And so first of all, well, in this step we are going to initialize serial communication.

01:10.950 --> 01:16.350
We're going to send the message on the LCD to give the information that you have to press on the push

01:16.350 --> 01:18.630
button and we're going to read from the push button.

01:18.630 --> 01:23.220
So at the end of this step, we will be able to know when we have pressed on the push button from the

01:23.220 --> 01:24.060
Raspberry Pi.

01:24.060 --> 01:27.120
And then in the following steps, we are going to take a photo.

01:27.120 --> 01:34.140
When the button is pressed, we are going to send that photo and a message to the telegram, chat with

01:34.140 --> 01:41.220
the telegram bot and then we will write the code to open the door and we will write the code to deny

01:41.220 --> 01:42.390
the access.

01:42.540 --> 01:46.860
And so those are going to be step five to step nine.

01:47.250 --> 01:47.670
All right.

01:47.670 --> 01:48.840
And let's get started.

01:48.840 --> 01:57.810
So here I'm going full screen with Python and I keep that on the side to check the Arduino code and

01:57.810 --> 02:00.360
change something if eventually we need to change something.

02:00.360 --> 02:02.160
But I think that should be good.

02:02.430 --> 02:02.850
All right.

02:02.850 --> 02:05.730
So let's write the very beginning.

02:06.180 --> 02:07.860
So the interpreter.

02:10.500 --> 02:10.830
Okay.

02:10.830 --> 02:14.670
And then we are going to import serial library.

02:15.210 --> 02:20.010
And also let's import time directly because we are going to need to use it.

02:20.220 --> 02:26.190
So the first thing we do is to init serial communication with.

02:26.220 --> 02:32.520
So let's say cell is equal to serial dot serial.

02:33.270 --> 02:34.830
Then the port.

02:34.860 --> 02:47.610
Let's actually find the device name ls slash dev slash y and all the options we have that one.

02:47.610 --> 02:49.350
That should be the arduino.

02:49.710 --> 02:53.850
So slash dev slash tty ACM zero.

02:54.690 --> 02:58.380
The baud rate is the same.

02:59.650 --> 03:00.910
And right there.

03:02.240 --> 03:04.730
115,002 hundred.

03:05.060 --> 03:06.680
And then let's put a timeout.

03:06.780 --> 03:09.530
Read timeout of one second.

03:09.740 --> 03:15.140
And so for this project, we're not just going to connect to sales like this, we're going to put that

03:15.140 --> 03:17.690
inside a try except.

03:19.480 --> 03:21.770
So except cereal.

03:21.820 --> 03:23.350
Dot cereal.

03:24.010 --> 03:27.750
Exception in case that we can't connect and we're going to retry.

03:27.760 --> 03:30.850
And so to retry, I'm going to put a while here.

03:31.180 --> 03:36.910
While true but that's inside the while true.

03:37.120 --> 03:50.140
So if it works we do print successfully connected to cereal and we break out of the while.

03:50.140 --> 04:00.100
If it doesn't work we do print could not connect to cereal retrying.

04:02.080 --> 04:04.480
In one second, two seconds.

04:04.480 --> 04:05.020
Whatever.

04:07.660 --> 04:17.890
And then of course time dot sleep with one here and let's add another log here actually print to make

04:17.890 --> 04:18.970
it more complete.

04:20.260 --> 04:24.610
Trying to connect to Serial.

04:25.480 --> 04:25.660
Okay.

04:25.660 --> 04:31.540
So we have first trying to connect to cell and then successfully connected or could not connect retrying

04:32.080 --> 04:44.230
after we are so when we reach line 16 here let's just put the wait so wait after setup and let's just

04:44.230 --> 04:49.660
put print waiting for three seconds.

04:52.720 --> 04:52.800
Okay.

04:52.870 --> 04:54.550
So we know we have to wait.

04:55.090 --> 04:57.520
So time dot sleep.

04:57.520 --> 05:05.020
We give some time for the Arduino to be ready, especially now that we also have, as you can see in

05:05.020 --> 05:06.890
the setup, we have a delay.

05:06.910 --> 05:07.990
One second.

05:08.020 --> 05:08.530
Okay.

05:09.870 --> 05:15.820
So the time that the cell is initialized and plus this one second, we're just going to wait for three

05:15.820 --> 05:16.920
seconds on the Raspberry Pi.

05:16.920 --> 05:21.090
So we know that after those three seconds, everything is okay.

05:21.390 --> 05:27.810
And I'm going to do self dot reset input buffer.

05:28.560 --> 05:34.380
So anything that was received from the Arduino before those three seconds is going to be discarded.

05:35.160 --> 05:36.690
And then let's say print.

05:37.710 --> 05:38.370
Okay.

05:39.510 --> 05:47.370
What we can do also we'll just after we have initialized the serial and waited for three seconds, is

05:47.370 --> 05:50.220
to send actually two commands to the Arduino.

05:50.250 --> 05:55.860
Because when we start the application, if you remember, we have something on the screen that says

05:55.860 --> 05:57.930
push on the button to call.

05:57.930 --> 06:00.870
And we also have the Led, which is blue.

06:01.350 --> 06:01.460
Okay?

06:01.620 --> 06:04.920
So blue is basically the waiting state.

06:05.190 --> 06:12.130
And so before we write any while loop here, we're going to add some comments.

06:12.130 --> 06:27.670
So cell dot write, we're going to do print, text, push on button to call with backslash and of course

06:27.700 --> 06:33.220
dot encode UTF eight.

06:34.410 --> 06:36.420
Who's better with quotes.

06:37.780 --> 06:41.170
Like this and say that right.

06:41.710 --> 06:45.520
So we just send the two commands one after the other.

06:45.920 --> 06:46.060
Okay.

06:46.060 --> 06:47.770
So the first command is going to be.

06:47.860 --> 06:57.270
So with the read string here, read string until backslash N is going to read this until backslash n

06:57.280 --> 06:58.660
So it's going to get that.

06:58.780 --> 07:03.370
And then the other command is going to be in the buffer of the Arduino and it's going to be read just

07:03.370 --> 07:04.150
after it.

07:04.150 --> 07:09.460
And because we go very fast in the void loop, it's just like we are going to execute the two commands

07:09.460 --> 07:10.600
at the same time.

07:10.750 --> 07:18.880
Let's do set Led and let's put 000 and then so red, green, blue.

07:19.960 --> 07:27.940
With backslash n and encode UTF eight.

07:29.350 --> 07:29.920
All right.

07:29.920 --> 07:34.570
And as you can see, well, we're going to send a few comments to the Arduino from the Raspberry Pi.

07:34.570 --> 07:36.520
And every time we need to do that, say that.

07:36.520 --> 07:37.210
Right.

07:37.210 --> 07:42.450
So we have the command, but we need also to add the backslash n and do encode.

07:42.460 --> 07:49.120
So what I'm going to do is I'm going to create a function here, just a function after we initialize

07:49.120 --> 07:57.310
sale def sent to Arduino with the text we want to send to Adrian.

07:57.490 --> 07:57.730
Okay.

07:57.730 --> 08:03.790
So then instead of just doing self dot right, we just do send to Arduino with the command directly.

08:03.790 --> 08:07.690
We don't need to worry about the backslash n about the encoding etcetera.

08:07.690 --> 08:09.580
And so we are going to construct a string.

08:09.580 --> 08:15.430
So string to send is equal to text plus backslash.

08:15.430 --> 08:23.600
N Actually I'm going to do text dot strip, just in case we send something with some backslash and character,

08:23.600 --> 08:24.710
some carriage return.

08:24.710 --> 08:29.030
We're going to remove them to be sure there is none and then we're going to add one.

08:29.030 --> 08:41.090
So we only have one backslash n at the end and then say dot write string to send dot encode UTF eight.

08:41.990 --> 08:42.830
All right.

08:43.550 --> 08:48.920
And so we're going to save a bit of code and make less mistakes when we need to send something to the

08:48.920 --> 08:49.430
Arduino.

08:49.430 --> 08:55.700
And so I'm going to do send to Arduino with just this.

08:59.000 --> 08:59.450
Okay.

08:59.450 --> 09:06.440
And then send to Arduino with set led until there.

09:09.810 --> 09:10.290
Okay.

09:10.290 --> 09:11.430
And I can remove that.

09:11.430 --> 09:13.590
And the code is now a bit cleaner.

09:14.580 --> 09:21.300
And well, one iteration you could also add is add other functions on top of that, for example, print

09:21.300 --> 09:23.290
text on Arduino set LCD.

09:23.550 --> 09:29.280
And so you're going to construct the string directly, for example, print text on a function and you

09:29.280 --> 09:33.620
just need to send this text or you just need to send the colors.

09:33.630 --> 09:36.780
And so you can have one function for each command you send.

09:36.780 --> 09:43.410
But I'm going to keep things simple here and just send the entire command, but without having to worry

09:43.410 --> 09:46.830
about this backslash n at the end and the encoding.

09:47.400 --> 09:49.670
So now everything is okay.

09:49.680 --> 09:53.880
We have put some text on the screen and set the Led.

09:54.480 --> 09:57.240
What we're going to do is the while loop.

09:57.240 --> 10:03.660
So all of that is kind of the void setup, okay, of the Raspberry Pi and then the void loop is going

10:03.660 --> 10:06.390
to be the next block of code.

10:06.690 --> 10:16.920
And so let's put run main loop, maybe let's put okay and then main loop is starting.

10:19.190 --> 10:22.960
So we do the while true.

10:22.970 --> 10:29.720
Let's put directly time dot sleep for 0.01 second.

10:29.750 --> 10:32.480
So we're going to run this loop at 100Hz.

10:32.690 --> 10:36.050
And let's put directly this inside.

10:36.260 --> 10:45.440
So I'm going to put more spaces here inside a try except structure.

10:48.150 --> 10:49.380
Except keyboard

10:52.140 --> 10:53.220
interrupt.

10:53.760 --> 10:55.140
And what do we do here?

10:55.140 --> 11:11.030
We do say dot close with a message, print closing, cell communication, and then let's say print end

11:11.070 --> 11:12.210
of program here.

11:15.120 --> 11:20.190
And maybe we can also add before I'm going to add dash, dash, dash.

11:20.190 --> 11:25.350
So basically every log that we're going to print, then we have dash, dash, dash, which makes a clear

11:25.350 --> 11:26.940
separation for the end.

11:26.970 --> 11:31.920
It's just for us, the developer to get nicer logs when we run the application.

11:31.920 --> 11:32.460
Okay.

11:32.820 --> 11:37.800
So we have this cell is correctly handled.

11:37.800 --> 11:45.900
And now let's do if, say, dot in waiting, because let's not forget what we want is to receive the

11:45.930 --> 11:48.060
button pressed command.

11:48.060 --> 12:00.990
So if we have something in the serial we do message is equal to set dot read line dot decode, UTF eight

12:02.280 --> 12:04.110
dot strip.

12:05.130 --> 12:12.000
So here you could also create a function like receive line from Arduino or something like that.

12:12.390 --> 12:17.820
But as we are only going to get one command, well, I'm just going to leave that code here.

12:17.820 --> 12:23.430
But if you were to use this pretty often, then creating a function would be a good idea.

12:23.430 --> 12:31.740
And let's do if message is equal to button pressed, let's check that we have.

12:32.570 --> 12:33.490
Button pressed.

12:33.500 --> 12:36.200
That's the same in that case.

12:36.230 --> 12:37.520
Let's do print.

12:38.660 --> 12:43.700
Open the request, because that's what this means.

12:44.210 --> 12:49.460
And then what we're going to do is we're going to take a photo with the camera, send it to Telegram.

12:49.460 --> 12:51.800
But that's for the next step.

12:52.040 --> 12:59.790
What we could already do is to send to Arduino the command to print some text.

12:59.810 --> 13:01.940
So print text.

13:02.570 --> 13:10.200
So basically to give the user the information, the feedback that, well, the request has been received.

13:10.240 --> 13:10.690
Okay.

13:10.700 --> 13:15.110
If you press on the button and nothing happens, well, you can't know if it worked.

13:15.110 --> 13:21.890
So if you press on the button and you see the text, like, please wait a few seconds, then you know

13:21.890 --> 13:23.810
that the request has been sent.

13:23.810 --> 13:26.540
So we're going to change the text.

13:26.580 --> 13:34.010
Can the LCD please wait for a few seconds?

13:34.010 --> 13:34.230
Okay.

13:34.250 --> 13:37.130
And then we are going to handle the request.

13:37.130 --> 13:43.790
And when we decide to open the door or to deny the access, we're going to also change the text.

13:43.820 --> 13:46.150
But that's for a future step.

13:46.160 --> 13:50.630
For now, we just acknowledge that we have received the request.

13:50.840 --> 13:58.640
And I'm going to add also an else here because as we did for the Arduino, so on the Arduino, we read

13:59.330 --> 14:00.560
a string.

14:00.560 --> 14:07.430
And if the string doesn't correspond to anything that we expect, we clear the input buffer.

14:07.430 --> 14:08.810
We're going to do the same here.

14:09.200 --> 14:17.630
If we receive something that is not button pressed, then we're going to do set dot reset input buffer.

14:17.630 --> 14:19.760
So we just clear everything else.

14:19.760 --> 14:25.640
And so we give another chance to read correct data when we enter the while loop again.

14:26.330 --> 14:35.130
Okay, Now let's save this program as intercom, let's say step and we are in step number five.

14:35.670 --> 14:38.850
Okay, so the Python program will actually start at step five.

14:38.880 --> 14:39.210
Okay.

14:39.240 --> 14:42.570
The Arduino is going to be step two, three and four.

14:42.870 --> 14:47.880
And for the Python program, we will have step five, six, seven, eight and nine.

14:47.880 --> 14:49.590
And let's save this.

14:49.950 --> 14:57.330
So the program here, the Arduino program is still uploaded and running on the Raspberry Pi.

14:57.540 --> 15:00.120
Now we can run this.

15:00.300 --> 15:02.760
So let's run the code on the Raspberry Pi.

15:02.790 --> 15:04.050
Successfully connected.

15:04.050 --> 15:05.820
You can see starting here.

15:05.850 --> 15:10.110
Okay, Main loop is starting and you can see push on button to call.

15:10.140 --> 15:11.520
The Led is blue.

15:11.850 --> 15:15.210
Push on the button you can see open door request.

15:15.210 --> 15:19.050
We have received a log here and please wait for a few seconds.

15:19.050 --> 15:26.430
So we have correctly received the information that someone is ringing the bell and that's the end of

15:26.430 --> 15:26.640
this.

15:26.640 --> 15:27.840
Step number five.
