This is the solution of the previous activity where you need to basically inform Telegram when the Python
program has connected and disconnected from the Arduino.
Let's start with the Arduino and well, the code will be super, super easy.
We just do.
Serial dot begin and let's use this while not serial okay.
For compatibility with some Arduino boards and that's it.
Okay.
We just want to initialize the communication.
We don't want to send or receive any message here, so I'm going to just make sure it's connected and
upload the code.
Activity number seven.
Okay.
We should not have any error here, I hope.
And don't uploading.
Great.
So now let's do the interesting part on the Raspberry Pi side.
So here we need to do two things.
First, try to connect to the Arduino and then send a message to Telegram.
So let's actually do I'm going to remove that.
Let's write the serial code before so user bin env python three.
Let's import cereal and let's import time because we will need it and then we are going to retry to
connect.
So basically we have the line I'm going to write here.
Set is equal to cereal dot cereal with the port.
So the T cm0.
Let's check.
It's correct.
Okay.
You can check on the terminal or also here.
Okay.
ACM zero.
The baud rate the same.
On the Arduino.
And then let's add a read timeout of one second, which we are not going to use here.
Okay.
And before we do anything here, let's put that inside a try.
And so let's add an indentation here.
Try accept serial.
Dot cereal exception.
And before I write the code to handle the exception, let's also put this try except inside a while.
True loop.
So an infinite loop.
So now.
Well, we're going to enter the while true, we're going to try to connect to see if it works.
We print successfully connected to Serial and we break.
Out of the loop, because if we don't break, well, we're going to continue to try to connect to Zion,
but we are already connected to.
Okay.
And if it doesn't work, so if we have the exception.
We print could not connect to Serial.
Route trying in, let's say one second dot dot and time dot sleep with one.
So if we have the exception, we don't break out of the loop.
So we come back here and we try again.
Basically, if we manage to go out of this loop, it means we have connected to sail.
And I'm going to do a time dot sleep three here and then say dot reset, input buffer.
Okay.
And then, well, we have our code which for now does nothing.
So what we could do is just maybe directly close the cell communication or maybe add another Time.sleep,
for example, for 10s and then close the communication or just add a void loop, kind of a void loop
here.
So an infinite loop to keep the program up and then just close the cell communication when we press
Ctrl C, that's what I'm going to do here.
So another while true.
We've time dot sleep well, we can put 0.01 or whatever because here we don't do anything in that while
loop and I'm going to put this time the while loop inside.
He try.
Except.
Except So here it's going to be keyboard interrupt.
So in the keyboard, interrupt print closing serial and we do set dot close.
Okay.
Note that here to connect we have the try except inside the while true but here we have the while true
inside the try except okay that's something different.
So now while the serial code is working, we can add the telegram, but let's just run test it.
Activity seven.
Successfully connected to serial and then control C.
Closing Serial.
If I disconnect the Arduino board, let's try again.
Retrain in one second and connect the Arduino board and successfully connect it to control C and okay,
it works.
Here we have.
Okay.
I did control C when we were in the time.sleep.
Okay.
I didn't wait.
The correct amount of time.
Maybe let's just add a print here.
Sale.
Okay.
And.
He wait?
Three seconds.
Okay.
And now let's write the telegram part.
So import telegram.
So here we just want to send some notification.
We don't want to interact with the user on the chat.
Okay, So let's just do actually from Telegram.
Import, but and let's create a telegram bot so we can create it.
Well, we could create it before sale.
Just going to create it here.
Okay.
After we have connected to sale but is equal to bot.
We've spoken and what we need to give the token.
So let's do.
We've open.
And then let's find this file where we have the token actually, which is in slash home slash API slash
dot, local slash share and slash dot telegram.
But token.
So if you don't have this, please go back and watch the lesson where I saved the token and then with
R as F and we can do.
Telegram.
Token.
Is equal to.
F dot read to read all the file because we just have the token dot our strip.
To remove any unwanted character and token is equal to telegram token.
Okay, we have the bot ready.
Let's just do chat ID.
So actually what chat id we have I'm going to open.
Our telegram key send notification program we just used before and I'm just going to use this chat ID
here.
So use the same as we used previously.
Okay.
The bot is now correctly set up and now we can send the message.
So basically we need to send the message when the serial is connected.
So the serial is actually connected here.
But then in the code we wait for three seconds, we reset the input and we consider that it is okay
here.
So I'm going to wait.
Okay.
To send a message to Telegram after this time.
Dot sleep.
Okay.
So let's do that here.
Let's do bot dot send message chat id is equal to chat ID and text is equal to, let's say connected
to Arduino with Serial.
Let's go to a new line here.
Here.
You can send whatever you want.
Maybe you can send information such as the port, the baud rate.
Okay.
But here we keep things simple.
So we send a message when we have the communication ready to be used.
And then we also going to send a message here just before we close the sale or maybe just after we close.
It doesn't really matter here, but dot send message with chat id is equal to chat ID.
Then text equals to.
Disconnected
from Arduino.
All right, now we can try this code.
So I'm going to use the Telegram app on the Raspberry Pi.
You can use the Telegram app on any desktop, any environment you want on your computer, on your phone.
It doesn't really matter if you use if you have the telegram app on your phone, you're going to receive
some notifications.
Okay, let's put that here and let's run the script.
Okay, wait three seconds and then say, okay, we have a notification here connected to Arduino with
Serial.
And then if I press control C closing serial, you can see disconnected from Arduino.
Okay.
So using this can be a good way to actually know, for example, if your program is correctly working.
Okay.
So if you have a project with Arduino and Raspberry Pi and for example, you want to keep the connection
alive for a long time, you could try to detect when the communication is down and then send a message
to the Telegram app so you can be notified as soon as possible and then you can eventually fix this
problem by yourself.
All right.
And that's the end of this activity.