WEBVTT

00:00.020 --> 00:05.690
This is step number ten and also the final step of this intercom project.

00:05.690 --> 00:11.810
So at this point, we have correctly written the complete Arduino program and the complete Python program.

00:11.810 --> 00:13.720
So what else do we need to do actually?

00:13.730 --> 00:18.920
Well, if you have noticed, whenever you want to start the Python project on the Raspberry Pi, well,

00:18.920 --> 00:20.450
you need to boot the Raspberry Pi.

00:20.480 --> 00:27.380
You need to log in in the desktop or we just command line with SSH and then you need to start the script.

00:27.380 --> 00:30.920
So you need to start the python script manually every time.

00:30.920 --> 00:35.150
Now a Raspberry Pi is nice because it's very small and you can put it anywhere.

00:35.150 --> 00:39.200
So what if we make the program just start on boot?

00:39.200 --> 00:43.640
So when you boot the Raspberry Pi, the program is just also starting.

00:43.640 --> 00:45.140
That would be nice, right?

00:45.140 --> 00:46.640
And that's what we are going to do.

00:46.640 --> 00:52.310
So then the next time you put your Raspberry Pi, you don't need to log in to the desktop.

00:52.340 --> 00:54.380
The application is just going to work.

00:54.560 --> 00:57.110
And to do that we are going to use systemd.

00:57.110 --> 00:59.720
So this is not the course about systemd, right?

00:59.720 --> 01:04.710
This is just a small improvement for the project as a kind of a bonus step.

01:04.710 --> 01:10.110
So I'm just going to show you what you need to do step by step, but very simplified explanation.

01:10.110 --> 01:12.330
I'm not going to dive into the details.

01:12.450 --> 01:15.750
And so first of all, we are going to go.

01:16.580 --> 01:25.040
You in Python program where we have our intercom step nine, I'm going to put it as executable with

01:25.040 --> 01:32.870
c h mode plus X intercom, step nine, and you can see it should turn green.

01:33.050 --> 01:33.320
Okay.

01:33.320 --> 01:35.870
That's something that's going to be important for this step.

01:35.870 --> 01:41.870
And then I can do, for example, intercom step nine can launch it like this.

01:41.990 --> 01:43.550
So this is working.

01:43.550 --> 01:50.140
I have not connected the Arduino board actually here and I can also launch it with Python three intercom

01:50.150 --> 01:51.590
step nine.

01:51.980 --> 01:52.370
Okay.

01:52.370 --> 01:54.680
That is the same thing.

01:54.680 --> 02:07.520
So now that your script is executable, let's go to slash lib slash system D slash system press enter

02:08.240 --> 02:08.960
to clear.

02:09.350 --> 02:10.730
And what do we have?

02:10.730 --> 02:12.350
Well, we have a lot of things.

02:12.890 --> 02:19.310
And basically those you can see many of those files are something dot service.

02:19.700 --> 02:27.020
So we are going to create another file, another service and then make the service start on boot.

02:27.290 --> 02:29.300
So let's just follow the steps here.

02:29.300 --> 02:36.140
So you do sudo nano to create file, you need sudo because we are not in the home directory.

02:36.430 --> 02:37.100
Right.

02:37.100 --> 02:44.990
And then let's name it intercom dash project dot service.

02:45.260 --> 02:45.710
Okay.

02:45.710 --> 02:47.150
And what are we going to write?

02:47.150 --> 02:55.160
So this is a specific structure you need to follow first with brackets Unit Description.

02:56.330 --> 02:59.150
So the description is anything you want.

02:59.180 --> 03:05.630
For example, intercom system, it doesn't really matter here.

03:06.140 --> 03:07.910
And then you have after.

03:07.910 --> 03:09.890
So write exactly like this.

03:09.890 --> 03:14.900
You're going to put multi slash user dot target.

03:15.680 --> 03:20.460
So well this is just to say that the program, the service is going to run after this.

03:20.460 --> 03:23.100
So you have many different steps in the boot.

03:23.130 --> 03:23.790
Okay.

03:23.790 --> 03:29.550
And if you want to run a Python program, you're just going to get you're just going to use multi user

03:29.580 --> 03:30.720
dot target.

03:30.720 --> 03:37.740
So that is going to wait a bit until the environment is ready to start the script after unit you have

03:38.880 --> 03:39.840
service.

03:39.840 --> 03:49.260
So that's what you actually want to start and you're going to use exec start and exec start is the command

03:49.260 --> 03:52.470
you want to run with the service.

03:52.470 --> 03:53.460
What is the command?

03:53.460 --> 03:54.450
We want to run.

03:54.450 --> 03:56.520
We want to run the Python script.

03:56.520 --> 04:00.750
And so I'm going to open a new terminal here.

04:01.560 --> 04:07.530
So basically we want to run Python three with our script, but here we need to give the exact command

04:07.530 --> 04:10.560
with the exact path and what do I mean by the exact path?

04:10.560 --> 04:17.850
So if you do which Python three, you can see that Python three is actually this command slash user

04:17.850 --> 04:24.540
slash bin slash python three So when you use this in the terminal, actually that's a simplification

04:24.540 --> 04:25.500
for that.

04:25.530 --> 04:32.250
So I'm just going to control shift and C and control shift and V here we're going to use Python three

04:32.250 --> 04:33.210
with.

04:33.420 --> 04:43.370
So let's do Python programs with this and how to get the correct absolute path PWD.

04:45.350 --> 04:54.830
So I'm just going to copy this here with a space slash and copy the name of.

04:55.610 --> 04:57.020
The file.

04:57.020 --> 05:03.080
So we're going to run Python three with the script with the absolute path for the command and the absolute

05:03.080 --> 05:04.970
path for the file.

05:05.210 --> 05:09.590
Then you can choose to use the user is equal to pi.

05:09.620 --> 05:14.750
So basically you're going to run this as the Pi user.

05:14.750 --> 05:18.710
So as the user you are currently connected as.

05:18.920 --> 05:19.310
Okay.

05:19.310 --> 05:22.010
So that's very important here to use the Pi user.

05:22.190 --> 05:29.210
If you don't put that, it's going to use the root user and well, I guess for this program we shouldn't

05:29.210 --> 05:30.020
have any problem.

05:30.020 --> 05:33.290
But that may give you some issues with some programs.

05:33.290 --> 05:38.840
For example, if you have relative path or file names or if you just use something that is only set

05:38.840 --> 05:42.980
up on the Pi user environment and not on the root user.

05:42.980 --> 05:45.650
So I recommend to use user is equal to pi.

05:45.650 --> 05:58.470
And then the last thing is install like this wanted by is equal to and then the same multi user dot

05:58.920 --> 05:59.580
target.

06:00.090 --> 06:00.360
Okay.

06:00.360 --> 06:02.130
And that's the complete description.

06:02.130 --> 06:06.450
That's the complete structure for the service unit service install.

06:06.480 --> 06:09.690
You can do control s control X.

06:10.380 --> 06:10.830
Okay.

06:10.830 --> 06:13.590
And now the file is going to be somewhere here.

06:13.950 --> 06:16.350
What you can do now, so let's do clear.

06:16.350 --> 06:25.710
What you can do is pseudo system Ctl with daemon reload.

06:28.960 --> 06:33.100
And then, well, what you can do if you want to just start the service.

06:33.100 --> 06:36.220
So now the service exists, but it's not going to start on boot.

06:36.250 --> 06:36.730
Okay.

06:36.730 --> 06:42.130
So I'm first going to show you how you can directly start it right now and then how you can enable it

06:42.130 --> 06:43.570
so it can start on boot.

06:43.570 --> 06:46.750
So if you want, you can do sudo system.

06:47.200 --> 06:56.080
So let's actually do CD to have more space sudo systemctl start intercom.

06:56.350 --> 07:03.970
Let's actually press tab and well it's finding the service and if I run this.

07:07.210 --> 07:09.190
You will see we have starting here.

07:09.730 --> 07:10.150
Okay.

07:10.150 --> 07:15.730
And then we have the blue led and well, the application is actually starting, but we don't have any

07:15.730 --> 07:16.240
log here.

07:16.270 --> 07:16.480
Okay.

07:16.480 --> 07:18.760
That's just the service that's going to start.

07:18.760 --> 07:27.790
But in a background process now, if you want to stop, you can do sudo systemctl stop intercom project

07:27.940 --> 07:30.280
and you stop the service.

07:30.400 --> 07:30.640
Okay.

07:30.640 --> 07:31.690
So start and stop.

07:31.720 --> 07:36.610
Allow you to just start and stop the service as you want manually.

07:36.610 --> 07:48.820
But now what you can do is so let's do sudo actually system Ctl list unit files, press enter and we

07:48.820 --> 07:51.760
have a long list.

07:52.390 --> 07:52.570
Okay.

07:52.570 --> 07:56.620
I'm just going to press Q and what we can do instead is.

07:57.850 --> 08:05.140
List unit files and then pipe grep intercom to just find what we need.

08:05.140 --> 08:09.490
And you can see we have the intercom project service which is disabled.

08:09.610 --> 08:20.770
So I'm going to do sudo systemctl enable intercom project service you can see here created similarly.

08:21.010 --> 08:21.610
ET cetera.

08:21.610 --> 08:22.420
ET cetera.

08:22.660 --> 08:26.980
If I do the list unit files you see now enabled.

08:26.980 --> 08:30.370
And so now what I'm going to do is I'm going to do sudo reboot.

08:30.400 --> 08:37.750
Let's reboot the Raspberry Pi and I'm currently on VNC so you can see it's attempting to rebooting and

08:37.750 --> 08:39.460
well, the Raspberry Pi is rebooting.

08:39.460 --> 08:40.810
You can see starting.

08:46.330 --> 08:51.880
So starting again and you can see push on button to call.

08:51.880 --> 08:59.020
So actually what happened here is that first the Arduino was powered on again and so the program started.

08:59.020 --> 09:01.120
So that was not from the Raspberry Pi.

09:01.120 --> 09:05.140
And then the service started on boot on the Raspberry Pi.

09:05.140 --> 09:11.980
And when connecting to the Arduino with Serial, this actually had the effect to reboot the Arduino.

09:11.980 --> 09:14.140
And so you saw starting again.

09:14.140 --> 09:17.290
But then the Raspberry Pi was communicating with the Arduino.

09:17.290 --> 09:23.410
And you can see now that everything is set up and we can start to just press on the push button.

09:23.410 --> 09:24.850
Please wait for a few seconds.

09:24.850 --> 09:30.430
And we have just received a notification to open or deny the access.

09:30.790 --> 09:31.270
All right.

09:31.270 --> 09:35.110
And as you see here, I have not manually launched the program.

09:35.110 --> 09:36.880
It has been launched automatically.

09:36.880 --> 09:39.430
Now, let's go back to the terminal.

09:39.700 --> 09:40.240
Okay.

09:40.240 --> 09:48.680
So if you want to stop, you can do sudo systemctl, stop intercom service.

09:48.680 --> 09:51.770
So if you want to stop it, you can do that and it's going to stop.

09:51.770 --> 09:59.750
And also now if you want to disable it, you can do sudo systemctl, disable intercom project, you

09:59.750 --> 10:12.710
can see remove this and now if you do sudo systemctl list unit files with grep intercom or just enter.

10:14.020 --> 10:17.140
When we have the intercom project, which is disabled.

10:17.620 --> 10:17.820
Okay.

10:17.860 --> 10:23.710
So you can easily start and stop with the start and stop command and then enable disable with the disable

10:23.710 --> 10:24.620
and enable command.

10:24.620 --> 10:25.510
That's very easy.

10:25.510 --> 10:31.480
And so now that it is disabled, the next time I'm going to boot the Raspberry Pi, it's not going to

10:31.480 --> 10:32.650
start the service.

10:32.680 --> 10:35.590
If I put enable, it's gonna start the service.

10:35.620 --> 10:36.160
All right.

10:36.160 --> 10:44.050
And now the project is complete so you can create the service, enable it, and then just put your Arduino

10:44.050 --> 10:47.410
and your Raspberry Pi system wherever you want.

10:47.440 --> 10:47.710
Okay.

10:47.710 --> 10:50.800
As long as the Raspberry Pi can connect to Wi-Fi, of course.

10:50.800 --> 10:56.890
And you can just leave your application, run like that and wait for notifications on your phone with

10:56.890 --> 10:58.270
the telegram application.
