This is step number five of the final project of this course.
And now that we have the Arduino code finished and tested, let's write the code on the Raspberry Pi
with Python.
So the step five is going to be with Python.
Now we are going to use the Raspberry Pi functionalities and also the application logic to define,
okay, what the application will do and of course to interact with the Arduino so we can translate this
application logic into real physical actions.
And we're going to use a slightly different method for the steps here.
So for the Arduino, we just initialize the component and then did the input commands and the output
commands on the Raspberry Pi, we are going to follow the logical order of the user journey.
So the user is basically who is using the application.
Okay.
Because here, as we are writing the application logic, well this is going to be a better way so we
can iterate every time on the next step so we can validate one step and then iterate on the next one.
And so first of all, well, in this step we are going to initialize serial communication.
We're going to send the message on the LCD to give the information that you have to press on the push
button and we're going to read from the push button.
So at the end of this step, we will be able to know when we have pressed on the push button from the
Raspberry Pi.
And then in the following steps, we are going to take a photo.
When the button is pressed, we are going to send that photo and a message to the telegram, chat with
the telegram bot and then we will write the code to open the door and we will write the code to deny
the access.
And so those are going to be step five to step nine.
All right.
And let's get started.
So here I'm going full screen with Python and I keep that on the side to check the Arduino code and
change something if eventually we need to change something.
But I think that should be good.
All right.
So let's write the very beginning.
So the interpreter.
Okay.
And then we are going to import serial library.
And also let's import time directly because we are going to need to use it.
So the first thing we do is to init serial communication with.
So let's say cell is equal to serial dot serial.
Then the port.
Let's actually find the device name ls slash dev slash y and all the options we have that one.
That should be the arduino.
So slash dev slash tty ACM zero.
The baud rate is the same.
And right there.
115,002 hundred.
And then let's put a timeout.
Read timeout of one second.
And so for this project, we're not just going to connect to sales like this, we're going to put that
inside a try except.
So except cereal.
Dot cereal.
Exception in case that we can't connect and we're going to retry.
And so to retry, I'm going to put a while here.
While true but that's inside the while true.
So if it works we do print successfully connected to cereal and we break out of the while.
If it doesn't work we do print could not connect to cereal retrying.
In one second, two seconds.
Whatever.
And then of course time dot sleep with one here and let's add another log here actually print to make
it more complete.
Trying to connect to Serial.
Okay.
So we have first trying to connect to cell and then successfully connected or could not connect retrying
after we are so when we reach line 16 here let's just put the wait so wait after setup and let's just
put print waiting for three seconds.
Okay.
So we know we have to wait.
So time dot sleep.
We give some time for the Arduino to be ready, especially now that we also have, as you can see in
the setup, we have a delay.
One second.
Okay.
So the time that the cell is initialized and plus this one second, we're just going to wait for three
seconds on the Raspberry Pi.
So we know that after those three seconds, everything is okay.
And I'm going to do self dot reset input buffer.
So anything that was received from the Arduino before those three seconds is going to be discarded.
And then let's say print.
Okay.
What we can do also we'll just after we have initialized the serial and waited for three seconds, is
to send actually two commands to the Arduino.
Because when we start the application, if you remember, we have something on the screen that says
push on the button to call.
And we also have the Led, which is blue.
Okay?
So blue is basically the waiting state.
And so before we write any while loop here, we're going to add some comments.
So cell dot write, we're going to do print, text, push on button to call with backslash and of course
dot encode UTF eight.
Who's better with quotes.
Like this and say that right.
So we just send the two commands one after the other.
Okay.
So the first command is going to be.
So with the read string here, read string until backslash N is going to read this until backslash n
So it's going to get that.
And then the other command is going to be in the buffer of the Arduino and it's going to be read just
after it.
And because we go very fast in the void loop, it's just like we are going to execute the two commands
at the same time.
Let's do set Led and let's put 000 and then so red, green, blue.
With backslash n and encode UTF eight.
All right.
And as you can see, well, we're going to send a few comments to the Arduino from the Raspberry Pi.
And every time we need to do that, say that.
Right.
So we have the command, but we need also to add the backslash n and do encode.
So what I'm going to do is I'm going to create a function here, just a function after we initialize
sale def sent to Arduino with the text we want to send to Adrian.
Okay.
So then instead of just doing self dot right, we just do send to Arduino with the command directly.
We don't need to worry about the backslash n about the encoding etcetera.
And so we are going to construct a string.
So string to send is equal to text plus backslash.
N Actually I'm going to do text dot strip, just in case we send something with some backslash and character,
some carriage return.
We're going to remove them to be sure there is none and then we're going to add one.
So we only have one backslash n at the end and then say dot write string to send dot encode UTF eight.
All right.
And so we're going to save a bit of code and make less mistakes when we need to send something to the
Arduino.
And so I'm going to do send to Arduino with just this.
Okay.
And then send to Arduino with set led until there.
Okay.
And I can remove that.
And the code is now a bit cleaner.
And well, one iteration you could also add is add other functions on top of that, for example, print
text on Arduino set LCD.
And so you're going to construct the string directly, for example, print text on a function and you
just need to send this text or you just need to send the colors.
And so you can have one function for each command you send.
But I'm going to keep things simple here and just send the entire command, but without having to worry
about this backslash n at the end and the encoding.
So now everything is okay.
We have put some text on the screen and set the Led.
What we're going to do is the while loop.
So all of that is kind of the void setup, okay, of the Raspberry Pi and then the void loop is going
to be the next block of code.
And so let's put run main loop, maybe let's put okay and then main loop is starting.
So we do the while true.
Let's put directly time dot sleep for 0.01 second.
So we're going to run this loop at 100Hz.
And let's put directly this inside.
So I'm going to put more spaces here inside a try except structure.
Except keyboard
interrupt.
And what do we do here?
We do say dot close with a message, print closing, cell communication, and then let's say print end
of program here.
And maybe we can also add before I'm going to add dash, dash, dash.
So basically every log that we're going to print, then we have dash, dash, dash, which makes a clear
separation for the end.
It's just for us, the developer to get nicer logs when we run the application.
Okay.
So we have this cell is correctly handled.
And now let's do if, say, dot in waiting, because let's not forget what we want is to receive the
button pressed command.
So if we have something in the serial we do message is equal to set dot read line dot decode, UTF eight
dot strip.
So here you could also create a function like receive line from Arduino or something like that.
But as we are only going to get one command, well, I'm just going to leave that code here.
But if you were to use this pretty often, then creating a function would be a good idea.
And let's do if message is equal to button pressed, let's check that we have.
Button pressed.
That's the same in that case.
Let's do print.
Open the request, because that's what this means.
And then what we're going to do is we're going to take a photo with the camera, send it to Telegram.
But that's for the next step.
What we could already do is to send to Arduino the command to print some text.
So print text.
So basically to give the user the information, the feedback that, well, the request has been received.
Okay.
If you press on the button and nothing happens, well, you can't know if it worked.
So if you press on the button and you see the text, like, please wait a few seconds, then you know
that the request has been sent.
So we're going to change the text.
Can the LCD please wait for a few seconds?
Okay.
And then we are going to handle the request.
And when we decide to open the door or to deny the access, we're going to also change the text.
But that's for a future step.
For now, we just acknowledge that we have received the request.
And I'm going to add also an else here because as we did for the Arduino, so on the Arduino, we read
a string.
And if the string doesn't correspond to anything that we expect, we clear the input buffer.
We're going to do the same here.
If we receive something that is not button pressed, then we're going to do set dot reset input buffer.
So we just clear everything else.
And so we give another chance to read correct data when we enter the while loop again.
Okay, Now let's save this program as intercom, let's say step and we are in step number five.
Okay, so the Python program will actually start at step five.
Okay.
The Arduino is going to be step two, three and four.
And for the Python program, we will have step five, six, seven, eight and nine.
And let's save this.
So the program here, the Arduino program is still uploaded and running on the Raspberry Pi.
Now we can run this.
So let's run the code on the Raspberry Pi.
Successfully connected.
You can see starting here.
Okay, Main loop is starting and you can see push on button to call.
The Led is blue.
Push on the button you can see open door request.
We have received a log here and please wait for a few seconds.
So we have correctly received the information that someone is ringing the bell and that's the end of
this.
Step number five.