This is step number ten and also the final step of this intercom project.
So at this point, we have correctly written the complete Arduino program and the complete Python program.
So what else do we need to do actually?
Well, if you have noticed, whenever you want to start the Python project on the Raspberry Pi, well,
you need to boot the Raspberry Pi.
You need to log in in the desktop or we just command line with SSH and then you need to start the script.
So you need to start the python script manually every time.
Now a Raspberry Pi is nice because it's very small and you can put it anywhere.
So what if we make the program just start on boot?
So when you boot the Raspberry Pi, the program is just also starting.
That would be nice, right?
And that's what we are going to do.
So then the next time you put your Raspberry Pi, you don't need to log in to the desktop.
The application is just going to work.
And to do that we are going to use systemd.
So this is not the course about systemd, right?
This is just a small improvement for the project as a kind of a bonus step.
So I'm just going to show you what you need to do step by step, but very simplified explanation.
I'm not going to dive into the details.
And so first of all, we are going to go.
You in Python program where we have our intercom step nine, I'm going to put it as executable with
c h mode plus X intercom, step nine, and you can see it should turn green.
Okay.
That's something that's going to be important for this step.
And then I can do, for example, intercom step nine can launch it like this.
So this is working.
I have not connected the Arduino board actually here and I can also launch it with Python three intercom
step nine.
Okay.
That is the same thing.
So now that your script is executable, let's go to slash lib slash system D slash system press enter
to clear.
And what do we have?
Well, we have a lot of things.
And basically those you can see many of those files are something dot service.
So we are going to create another file, another service and then make the service start on boot.
So let's just follow the steps here.
So you do sudo nano to create file, you need sudo because we are not in the home directory.
Right.
And then let's name it intercom dash project dot service.
Okay.
And what are we going to write?
So this is a specific structure you need to follow first with brackets Unit Description.
So the description is anything you want.
For example, intercom system, it doesn't really matter here.
And then you have after.
So write exactly like this.
You're going to put multi slash user dot target.
So well this is just to say that the program, the service is going to run after this.
So you have many different steps in the boot.
Okay.
And if you want to run a Python program, you're just going to get you're just going to use multi user
dot target.
So that is going to wait a bit until the environment is ready to start the script after unit you have
service.
So that's what you actually want to start and you're going to use exec start and exec start is the command
you want to run with the service.
What is the command?
We want to run.
We want to run the Python script.
And so I'm going to open a new terminal here.
So basically we want to run Python three with our script, but here we need to give the exact command
with the exact path and what do I mean by the exact path?
So if you do which Python three, you can see that Python three is actually this command slash user
slash bin slash python three So when you use this in the terminal, actually that's a simplification
for that.
So I'm just going to control shift and C and control shift and V here we're going to use Python three
with.
So let's do Python programs with this and how to get the correct absolute path PWD.
So I'm just going to copy this here with a space slash and copy the name of.
The file.
So we're going to run Python three with the script with the absolute path for the command and the absolute
path for the file.
Then you can choose to use the user is equal to pi.
So basically you're going to run this as the Pi user.
So as the user you are currently connected as.
Okay.
So that's very important here to use the Pi user.
If you don't put that, it's going to use the root user and well, I guess for this program we shouldn't
have any problem.
But that may give you some issues with some programs.
For example, if you have relative path or file names or if you just use something that is only set
up on the Pi user environment and not on the root user.
So I recommend to use user is equal to pi.
And then the last thing is install like this wanted by is equal to and then the same multi user dot
target.
Okay.
And that's the complete description.
That's the complete structure for the service unit service install.
You can do control s control X.
Okay.
And now the file is going to be somewhere here.
What you can do now, so let's do clear.
What you can do is pseudo system Ctl with daemon reload.
And then, well, what you can do if you want to just start the service.
So now the service exists, but it's not going to start on boot.
Okay.
So I'm first going to show you how you can directly start it right now and then how you can enable it
so it can start on boot.
So if you want, you can do sudo system.
So let's actually do CD to have more space sudo systemctl start intercom.
Let's actually press tab and well it's finding the service and if I run this.
You will see we have starting here.
Okay.
And then we have the blue led and well, the application is actually starting, but we don't have any
log here.
Okay.
That's just the service that's going to start.
But in a background process now, if you want to stop, you can do sudo systemctl stop intercom project
and you stop the service.
Okay.
So start and stop.
Allow you to just start and stop the service as you want manually.
But now what you can do is so let's do sudo actually system Ctl list unit files, press enter and we
have a long list.
Okay.
I'm just going to press Q and what we can do instead is.
List unit files and then pipe grep intercom to just find what we need.
And you can see we have the intercom project service which is disabled.
So I'm going to do sudo systemctl enable intercom project service you can see here created similarly.
ET cetera.
If I do the list unit files you see now enabled.
And so now what I'm going to do is I'm going to do sudo reboot.
Let's reboot the Raspberry Pi and I'm currently on VNC so you can see it's attempting to rebooting and
well, the Raspberry Pi is rebooting.
You can see starting.
So starting again and you can see push on button to call.
So actually what happened here is that first the Arduino was powered on again and so the program started.
So that was not from the Raspberry Pi.
And then the service started on boot on the Raspberry Pi.
And when connecting to the Arduino with Serial, this actually had the effect to reboot the Arduino.
And so you saw starting again.
But then the Raspberry Pi was communicating with the Arduino.
And you can see now that everything is set up and we can start to just press on the push button.
Please wait for a few seconds.
And we have just received a notification to open or deny the access.
All right.
And as you see here, I have not manually launched the program.
It has been launched automatically.
Now, let's go back to the terminal.
Okay.
So if you want to stop, you can do sudo systemctl, stop intercom service.
So if you want to stop it, you can do that and it's going to stop.
And also now if you want to disable it, you can do sudo systemctl, disable intercom project, you
can see remove this and now if you do sudo systemctl list unit files with grep intercom or just enter.
When we have the intercom project, which is disabled.
Okay.
So you can easily start and stop with the start and stop command and then enable disable with the disable
and enable command.
That's very easy.
And so now that it is disabled, the next time I'm going to boot the Raspberry Pi, it's not going to
start the service.
If I put enable, it's gonna start the service.
All right.
And now the project is complete so you can create the service, enable it, and then just put your Arduino
and your Raspberry Pi system wherever you want.
Okay.
As long as the Raspberry Pi can connect to Wi-Fi, of course.
And you can just leave your application, run like that and wait for notifications on your phone with
the telegram application.