WEBVTT

00:00.110 --> 00:06.710
This is the solution of the previous activity where you need to basically inform Telegram when the Python

00:06.710 --> 00:10.370
program has connected and disconnected from the Arduino.

00:10.550 --> 00:15.780
Let's start with the Arduino and well, the code will be super, super easy.

00:15.800 --> 00:17.000
We just do.

00:18.280 --> 00:28.190
Serial dot begin and let's use this while not serial okay.

00:28.210 --> 00:33.400
For compatibility with some Arduino boards and that's it.

00:33.700 --> 00:34.000
Okay.

00:34.000 --> 00:36.310
We just want to initialize the communication.

00:36.310 --> 00:43.660
We don't want to send or receive any message here, so I'm going to just make sure it's connected and

00:43.660 --> 00:44.950
upload the code.

00:45.870 --> 00:49.260
Activity number seven.

00:52.430 --> 00:52.850
Okay.

00:52.850 --> 00:55.610
We should not have any error here, I hope.

00:57.710 --> 00:59.120
And don't uploading.

00:59.120 --> 01:00.050
Great.

01:00.290 --> 01:04.370
So now let's do the interesting part on the Raspberry Pi side.

01:04.370 --> 01:06.500
So here we need to do two things.

01:06.830 --> 01:10.760
First, try to connect to the Arduino and then send a message to Telegram.

01:10.760 --> 01:13.580
So let's actually do I'm going to remove that.

01:13.790 --> 01:21.770
Let's write the serial code before so user bin env python three.

01:23.170 --> 01:32.980
Let's import cereal and let's import time because we will need it and then we are going to retry to

01:32.980 --> 01:33.400
connect.

01:33.400 --> 01:37.600
So basically we have the line I'm going to write here.

01:37.630 --> 01:42.640
Set is equal to cereal dot cereal with the port.

01:42.640 --> 01:47.080
So the T cm0.

01:47.080 --> 01:47.560
Let's check.

01:47.560 --> 01:48.040
It's correct.

01:48.040 --> 01:48.210
Okay.

01:48.220 --> 01:51.640
You can check on the terminal or also here.

01:52.180 --> 01:52.720
Okay.

01:52.870 --> 01:54.340
ACM zero.

01:56.160 --> 01:59.460
The baud rate the same.

02:00.560 --> 02:01.310
On the Arduino.

02:01.670 --> 02:08.360
And then let's add a read timeout of one second, which we are not going to use here.

02:08.960 --> 02:09.230
Okay.

02:09.230 --> 02:13.490
And before we do anything here, let's put that inside a try.

02:14.000 --> 02:17.390
And so let's add an indentation here.

02:17.570 --> 02:22.010
Try accept serial.

02:23.910 --> 02:27.330
Dot cereal exception.

02:29.900 --> 02:38.210
And before I write the code to handle the exception, let's also put this try except inside a while.

02:38.630 --> 02:39.620
True loop.

02:39.620 --> 02:41.540
So an infinite loop.

02:43.410 --> 02:44.370
So now.

02:44.370 --> 02:50.080
Well, we're going to enter the while true, we're going to try to connect to see if it works.

02:50.100 --> 02:59.190
We print successfully connected to Serial and we break.

03:00.660 --> 03:06.360
Out of the loop, because if we don't break, well, we're going to continue to try to connect to Zion,

03:06.600 --> 03:08.360
but we are already connected to.

03:08.370 --> 03:08.940
Okay.

03:09.060 --> 03:12.120
And if it doesn't work, so if we have the exception.

03:13.090 --> 03:19.060
We print could not connect to Serial.

03:21.290 --> 03:32.930
Route trying in, let's say one second dot dot and time dot sleep with one.

03:32.930 --> 03:36.740
So if we have the exception, we don't break out of the loop.

03:36.740 --> 03:39.530
So we come back here and we try again.

03:39.680 --> 03:45.950
Basically, if we manage to go out of this loop, it means we have connected to sail.

03:45.950 --> 03:57.920
And I'm going to do a time dot sleep three here and then say dot reset, input buffer.

03:58.820 --> 03:59.240
Okay.

03:59.240 --> 04:03.890
And then, well, we have our code which for now does nothing.

04:03.890 --> 04:11.060
So what we could do is just maybe directly close the cell communication or maybe add another Time.sleep,

04:11.060 --> 04:17.600
for example, for 10s and then close the communication or just add a void loop, kind of a void loop

04:17.600 --> 04:18.080
here.

04:18.080 --> 04:24.540
So an infinite loop to keep the program up and then just close the cell communication when we press

04:24.540 --> 04:26.880
Ctrl C, that's what I'm going to do here.

04:27.260 --> 04:29.370
So another while true.

04:30.560 --> 04:41.270
We've time dot sleep well, we can put 0.01 or whatever because here we don't do anything in that while

04:41.270 --> 04:45.770
loop and I'm going to put this time the while loop inside.

04:46.640 --> 04:47.230
He try.

04:47.240 --> 04:48.050
Except.

04:49.970 --> 04:55.550
Except So here it's going to be keyboard interrupt.

04:55.760 --> 05:06.980
So in the keyboard, interrupt print closing serial and we do set dot close.

05:07.880 --> 05:08.080
Okay.

05:08.090 --> 05:15.140
Note that here to connect we have the try except inside the while true but here we have the while true

05:15.140 --> 05:18.100
inside the try except okay that's something different.

05:18.110 --> 05:27.620
So now while the serial code is working, we can add the telegram, but let's just run test it.

05:29.330 --> 05:31.940
Activity seven.

05:33.570 --> 05:38.220
Successfully connected to serial and then control C.

05:38.250 --> 05:39.150
Closing Serial.

05:39.180 --> 05:43.650
If I disconnect the Arduino board, let's try again.

05:45.060 --> 05:53.550
Retrain in one second and connect the Arduino board and successfully connect it to control C and okay,

05:53.550 --> 05:54.330
it works.

05:54.630 --> 05:55.500
Here we have.

05:55.500 --> 05:55.980
Okay.

05:55.980 --> 05:59.030
I did control C when we were in the time.sleep.

05:59.100 --> 05:59.280
Okay.

05:59.280 --> 06:00.330
I didn't wait.

06:02.010 --> 06:03.210
The correct amount of time.

06:03.210 --> 06:05.580
Maybe let's just add a print here.

06:07.670 --> 06:08.370
Sale.

06:09.260 --> 06:09.820
Okay.

06:12.020 --> 06:12.920
And.

06:13.890 --> 06:15.270
He wait?

06:16.520 --> 06:18.180
Three seconds.

06:19.040 --> 06:19.340
Okay.

06:19.340 --> 06:21.770
And now let's write the telegram part.

06:21.770 --> 06:24.980
So import telegram.

06:24.980 --> 06:27.560
So here we just want to send some notification.

06:27.560 --> 06:30.860
We don't want to interact with the user on the chat.

06:30.890 --> 06:36.230
Okay, So let's just do actually from Telegram.

06:37.170 --> 06:44.700
Import, but and let's create a telegram bot so we can create it.

06:44.730 --> 06:47.630
Well, we could create it before sale.

06:47.970 --> 06:49.710
Just going to create it here.

06:49.950 --> 06:50.130
Okay.

06:50.130 --> 06:55.560
After we have connected to sale but is equal to bot.

06:56.980 --> 07:00.700
We've spoken and what we need to give the token.

07:00.700 --> 07:02.590
So let's do.

07:03.820 --> 07:05.800
We've open.

07:06.610 --> 07:15.760
And then let's find this file where we have the token actually, which is in slash home slash API slash

07:15.910 --> 07:22.000
dot, local slash share and slash dot telegram.

07:23.560 --> 07:25.350
But token.

07:25.360 --> 07:33.580
So if you don't have this, please go back and watch the lesson where I saved the token and then with

07:33.580 --> 07:38.080
R as F and we can do.

07:39.610 --> 07:40.420
Telegram.

07:42.010 --> 07:42.700
Token.

07:44.460 --> 07:45.510
Is equal to.

07:46.700 --> 07:52.400
F dot read to read all the file because we just have the token dot our strip.

07:54.070 --> 08:02.710
To remove any unwanted character and token is equal to telegram token.

08:03.370 --> 08:05.900
Okay, we have the bot ready.

08:05.920 --> 08:08.830
Let's just do chat ID.

08:09.700 --> 08:13.150
So actually what chat id we have I'm going to open.

08:14.550 --> 08:23.710
Our telegram key send notification program we just used before and I'm just going to use this chat ID

08:23.730 --> 08:24.360
here.

08:24.720 --> 08:27.660
So use the same as we used previously.

08:29.920 --> 08:30.310
Okay.

08:30.340 --> 08:35.470
The bot is now correctly set up and now we can send the message.

08:35.470 --> 08:39.730
So basically we need to send the message when the serial is connected.

08:40.060 --> 08:42.190
So the serial is actually connected here.

08:42.190 --> 08:48.280
But then in the code we wait for three seconds, we reset the input and we consider that it is okay

08:48.280 --> 08:48.700
here.

08:48.700 --> 08:49.930
So I'm going to wait.

08:49.930 --> 08:50.500
Okay.

08:50.770 --> 08:54.220
To send a message to Telegram after this time.

08:54.220 --> 08:54.910
Dot sleep.

08:54.940 --> 08:55.330
Okay.

08:55.330 --> 08:56.710
So let's do that here.

08:56.710 --> 09:09.730
Let's do bot dot send message chat id is equal to chat ID and text is equal to, let's say connected

09:10.390 --> 09:15.610
to Arduino with Serial.

09:15.640 --> 09:17.590
Let's go to a new line here.

09:21.130 --> 09:21.220
Here.

09:21.340 --> 09:22.410
You can send whatever you want.

09:22.440 --> 09:27.660
Maybe you can send information such as the port, the baud rate.

09:28.410 --> 09:28.860
Okay.

09:28.860 --> 09:30.900
But here we keep things simple.

09:31.170 --> 09:35.540
So we send a message when we have the communication ready to be used.

09:35.550 --> 09:41.520
And then we also going to send a message here just before we close the sale or maybe just after we close.

09:41.530 --> 09:50.820
It doesn't really matter here, but dot send message with chat id is equal to chat ID.

09:53.270 --> 09:55.910
Then text equals to.

09:57.380 --> 09:58.520
Disconnected

10:00.950 --> 10:03.830
from Arduino.

10:06.480 --> 10:10.320
All right, now we can try this code.

10:10.680 --> 10:14.800
So I'm going to use the Telegram app on the Raspberry Pi.

10:14.820 --> 10:20.940
You can use the Telegram app on any desktop, any environment you want on your computer, on your phone.

10:20.940 --> 10:26.340
It doesn't really matter if you use if you have the telegram app on your phone, you're going to receive

10:26.340 --> 10:27.690
some notifications.

10:27.780 --> 10:32.250
Okay, let's put that here and let's run the script.

10:33.820 --> 10:40.900
Okay, wait three seconds and then say, okay, we have a notification here connected to Arduino with

10:40.900 --> 10:41.290
Serial.

10:41.980 --> 10:48.730
And then if I press control C closing serial, you can see disconnected from Arduino.

10:48.850 --> 10:49.260
Okay.

10:49.270 --> 10:55.570
So using this can be a good way to actually know, for example, if your program is correctly working.

10:55.570 --> 10:55.990
Okay.

10:55.990 --> 11:02.020
So if you have a project with Arduino and Raspberry Pi and for example, you want to keep the connection

11:02.020 --> 11:08.560
alive for a long time, you could try to detect when the communication is down and then send a message

11:08.560 --> 11:14.650
to the Telegram app so you can be notified as soon as possible and then you can eventually fix this

11:14.650 --> 11:16.060
problem by yourself.

11:16.180 --> 11:16.510
All right.

11:16.510 --> 11:18.430
And that's the end of this activity.
