WEBVTT

00:00.110 --> 00:05.720
This is the solution of the previous activities or activity number eight, where you have to control

00:05.750 --> 00:11.930
Arduino from Telegram so you can send some text on the LCD screen and you can also choose the color

00:11.930 --> 00:15.050
for the RGB with just one command.

00:15.080 --> 00:16.610
So we have a few things to do.

00:16.610 --> 00:24.560
Let's start, as always, with the Arduino side and let's okay, let's initialize here

00:27.410 --> 00:32.810
with the baud rate while not serial.

00:33.950 --> 00:36.230
Okay, that's pretty fast to do.

00:36.380 --> 00:42.110
And then we will need to initialize the LCD and the RGB.

00:42.410 --> 00:50.990
So let's create the defines, define RGB rate pin, which is 11 define.

00:52.590 --> 00:56.850
RGB green pin, which is ten.

00:56.850 --> 00:59.190
Let's not make a mistake this time on the blue.

01:00.450 --> 01:05.760
Blue pin, which is nine and not 19 like I did before.

01:05.850 --> 01:12.030
And then define LCD s pin.

01:12.030 --> 01:14.130
I'm going to put the number just after.

01:14.310 --> 01:18.270
Define LCD e pin.

01:18.930 --> 01:21.720
Define LCD four.

01:24.240 --> 01:30.870
So we have d4 d5 d6 d7 five.

01:32.230 --> 01:43.990
Six seven with A4 a5 two, three, four and five.

01:44.770 --> 01:44.980
Okay.

01:44.980 --> 01:47.500
You make sure they are correctly configured.

01:48.430 --> 01:54.940
We include the liquid crystal, but.

01:55.570 --> 01:58.490
With a Y here H.

01:59.110 --> 02:01.750
And then we create the liquid crystal here.

02:01.750 --> 02:03.730
So liquid.

02:05.370 --> 02:06.570
Crystal LCD.

02:06.840 --> 02:08.850
So the initialization is always the same.

02:08.850 --> 02:12.000
You can even copy paste the code from the previous.

02:14.440 --> 02:19.930
Blessings if you want to go rcd for.

02:21.470 --> 02:22.280
And then.

02:24.900 --> 02:28.380
D5 d6.

02:30.330 --> 02:31.110
And.

02:33.300 --> 02:34.140
D7.

02:36.810 --> 02:38.250
Okay, let's do.

02:39.780 --> 02:43.080
LCD dot begin 16 by two.

02:45.120 --> 02:54.000
And let's also do the Pinmode with RGB red.

02:57.200 --> 02:58.250
To output.

03:00.170 --> 03:03.140
And then the same for the green.

03:06.390 --> 03:07.770
And the same for the blue.

03:11.240 --> 03:15.440
And we are done with initialization of the Arduino component.

03:15.710 --> 03:17.810
Now in the void loop.

03:18.530 --> 03:21.400
So I'm going to remove that and I'm going to do this again.

03:21.410 --> 03:21.890
Okay.

03:21.890 --> 03:23.810
Explaining it step by step.

03:24.530 --> 03:29.750
So in the void loop, we don't have any other thing to do than checking the sale.

03:29.900 --> 03:43.190
So if cell dot available greater strictly than zero, we are going to read the command is equal to sale

03:43.190 --> 03:49.040
dot read string until backslash n.

03:50.310 --> 03:54.620
And then we are going to check if command is equal to.

03:54.630 --> 03:55.260
Okay.

03:55.380 --> 03:56.790
Let's start with the LCD.

03:57.060 --> 04:01.590
What command do we receive here and what command do we send here?

04:01.740 --> 04:07.590
So this command will contain first something that we can recognize, for example, print text, but

04:07.590 --> 04:11.820
then it's going to contain something that we don't know yet from the Arduino side.

04:11.820 --> 04:12.390
Okay.

04:12.510 --> 04:19.440
So instead of just checking the equality for the comment, I'm going to do, if Command Dot starts with

04:19.440 --> 04:27.240
and let's say that the command starts with print, text, colon, and then we have the text.

04:27.240 --> 04:30.450
So if it starts with this, we are going to do.

04:31.350 --> 04:33.210
Comment dots remove.

04:33.540 --> 04:37.350
So we remove the first comment.

04:37.350 --> 04:42.600
So 1234, five, six, seven, eight, nine, ten, 11.

04:43.380 --> 04:46.050
So we remove from 0 to 11.

04:47.680 --> 04:55.330
And we are left with the text to print and let's do LCD dot print command.

04:55.450 --> 04:59.590
And before we print the command, we could also, for example, do LCD dot clear.

04:59.950 --> 05:00.420
Okay.

05:00.610 --> 05:04.780
And LCD dot set cursor.

05:06.520 --> 05:08.170
Zero zero.

05:08.350 --> 05:09.010
We clear?

05:09.010 --> 05:10.060
We come back to the beginning.

05:10.060 --> 05:11.680
We print the text.

05:12.340 --> 05:15.760
Okay, Let's go to the next.

05:15.970 --> 05:17.560
So else if.

05:18.660 --> 05:21.670
Command starts with.

05:21.720 --> 05:23.790
Now we have the set led.

05:25.270 --> 05:25.500
Okay.

05:25.560 --> 05:30.630
So we check if it's a print text command or a set led command.

05:31.740 --> 05:33.720
If this is the case.

05:33.870 --> 05:42.360
Okay, before I forget, I'm just going to add else where we do nothing for now and well, I will add

05:42.360 --> 05:44.370
a best practice in the final project.

05:44.370 --> 05:44.560
Okay.

05:44.580 --> 05:48.390
For this else that you're going to see later, one thing at a time.

05:48.480 --> 05:54.660
So if we have the command that starts with set, then one, two, three, four, five, six, seven,

05:54.660 --> 05:55.230
eight.

05:55.260 --> 06:01.530
We do command dot remove zero eight and then we get each color.

06:01.530 --> 06:13.650
So int red is equal to command dot sub string from 0 to 3 and dot to int.

06:14.400 --> 06:14.670
Okay.

06:14.670 --> 06:18.090
You can put this as a help if you want to count.

06:18.780 --> 06:20.130
Let's say we have this.

06:21.570 --> 06:27.000
So zero one, two, three and then four, five, six, seven.

06:27.540 --> 06:30.200
So we do int green.

06:30.240 --> 06:33.360
Of course it's important that when you send the command, you respect the order.

06:33.400 --> 06:33.710
Okay.

06:33.750 --> 06:38.550
Red, green, blue command dot substring.

06:41.340 --> 06:43.650
From 4 to 7.

06:43.650 --> 06:47.820
So we just ignore the comma to int.

06:48.360 --> 06:51.480
INT blue is equal to comment dot

06:54.300 --> 06:55.530
substring.

06:56.100 --> 06:56.450
Okay.

06:56.460 --> 06:58.740
If this is seven, then this is eight.

06:58.770 --> 07:04.590
We put eight and we don't need to provide the end if we just want to go until the end.

07:04.620 --> 07:04.920
Okay.

07:04.920 --> 07:13.620
So we just read the three last characters to inch to inch because we have a string we need to convert

07:13.620 --> 07:16.200
to int so we can use in analog.

07:17.460 --> 07:18.270
Right.

07:19.050 --> 07:22.710
Analog, right with the red first.

07:24.320 --> 07:31.400
And we give red color and let's do the same for the green and the blue.

07:33.490 --> 07:35.740
Green with green.

07:37.890 --> 07:38.910
And blue

07:42.300 --> 07:45.750
with the blue we just got.

07:46.560 --> 07:49.140
Okay, let's remove that.

07:50.160 --> 07:53.010
And I think our program is complete.

07:53.040 --> 07:54.630
Let's just try it.

07:54.660 --> 07:58.770
Okay, Let's upload the code.

07:59.310 --> 08:02.670
Activity eight.

08:03.640 --> 08:04.840
And let's try.

08:07.290 --> 08:07.560
Okay.

08:07.560 --> 08:09.600
We first have an error here.

08:11.100 --> 08:12.960
Yes, because I forgot.

08:12.960 --> 08:15.960
So this is quite a sneaky error here.

08:15.960 --> 08:16.950
I forgot.

08:18.940 --> 08:23.050
The parentheses because we open parentheses, we open one, so we have to close two.

08:23.290 --> 08:25.030
And of course, the same here.

08:26.300 --> 08:26.690
Now.

08:28.420 --> 08:28.720
Okay.

08:28.720 --> 08:29.920
The compiling works now.

08:29.920 --> 08:32.350
The uploading should work as well.

08:32.620 --> 08:35.440
And well, let's try something here.

08:35.440 --> 08:40.840
So I'm going to open the serial monitor and let's send some commands.

08:40.840 --> 08:43.510
For example, we want to print something.

08:43.510 --> 08:48.820
So print text with let's put a space here, for example.

08:48.820 --> 08:50.590
Hello world.

08:50.830 --> 08:54.280
You can see we have Hello World on the screen here.

08:54.310 --> 09:00.400
And we have well, we have actually one space before because you can see we have added one space.

09:00.400 --> 09:05.290
So everything that we have after print text, colon is going to be printed.

09:05.320 --> 09:08.950
Let's change the string with Abcd, for example.

09:09.130 --> 09:11.530
You can see now everything has been cleared.

09:11.560 --> 09:17.050
We start back at the beginning and we print Abcd if I do.

09:17.410 --> 09:26.080
GB No, actually it's set led with let's put 200 and then zero and then 180.

09:27.250 --> 09:27.490
Okay.

09:27.490 --> 09:32.900
We have a purple, a purple color, so it's correctly working.

09:32.990 --> 09:33.470
Okay.

09:33.470 --> 09:40.280
So now that we know the Arduino code is working, let's write the code so that we can actually control

09:40.280 --> 09:41.810
this from Telegram.

09:42.050 --> 09:42.290
Okay.

09:42.290 --> 09:46.520
So on the Raspberry Pi, let's first connect to Serial.

09:46.850 --> 09:51.890
And I'm going to here I'm not going to write the code entirely.

09:52.940 --> 09:58.430
I'm going to use the code we did previously with the retry.

09:58.470 --> 09:58.970
Okay.

10:01.160 --> 10:02.420
So like this.

10:03.620 --> 10:04.610
And then.

10:06.660 --> 10:07.440
This.

10:09.580 --> 10:12.040
So we connect.

10:13.090 --> 10:14.290
We try to connect to.

10:14.710 --> 10:16.540
And then we wait for three seconds.

10:17.170 --> 10:17.920
Okay.

10:17.920 --> 10:26.560
And we have this infinite loop running inside a try except.

10:26.560 --> 10:31.600
Block And now I am going to add, so I'm just going to leave it there.

10:32.350 --> 10:39.520
I'm just going to comment that this is a comment and I'm going to add the Telegram functionality.

10:39.550 --> 10:51.340
So from Telegram dot txt, we are going to import this time updater and comment handler.

10:52.060 --> 10:57.790
I'm going to get the token like we did before, so I'm just going to get the token.

10:58.960 --> 10:59.860
Like this.

11:01.360 --> 11:11.320
So, for example, here and then we are going to create not a bot object, but an update operator is

11:11.320 --> 11:13.210
equal to update.

11:15.190 --> 11:17.590
We've token equals.

11:17.620 --> 11:20.500
Telegram, Token.

11:20.710 --> 11:29.530
Okay, so we do that after we have correctly initialized the cell here we add a dispatcher, which is

11:29.530 --> 11:36.100
a data dot dispatcher, and then we are going to add some callbacks.

11:36.640 --> 11:42.440
So let's add the callbacks right here before this code.

11:42.460 --> 11:47.200
So def let's say we have a start handler.

11:47.770 --> 11:58.210
Okay, let's always create a start handler update context and in the start handler we do context.

11:58.720 --> 12:16.370
Dot, dot, dot, send message with chat ID is equal to update dot effective chat.id and text is equal

12:16.370 --> 12:18.050
to let's say hello.

12:21.170 --> 12:23.840
Now, maybe just welcome.

12:24.440 --> 12:27.170
Okay, We have the start handler.

12:27.560 --> 12:32.180
Then we create a let's name it lxd handler.

12:32.420 --> 12:32.930
Okay.

12:34.110 --> 12:37.050
A date context.

12:38.050 --> 12:42.130
And what do we do when we receive a comment from the LCD?

12:42.280 --> 12:46.690
We are going to send that command to the Raspberry Pi.

12:46.720 --> 12:49.880
So how to get the text?

12:49.900 --> 12:56.950
The text is update dot message, dot text.

12:57.160 --> 12:58.870
I'm going to remove that now.

13:00.190 --> 13:01.270
And now.

13:01.920 --> 13:03.780
The string to send.

13:03.780 --> 13:09.750
So the command is going to be equal to print text.

13:09.930 --> 13:10.130
Okay.

13:10.170 --> 13:12.600
Because we need to send print text here.

13:13.570 --> 13:18.310
And then plus text and then plus backslash.

13:18.310 --> 13:21.970
N and we do say dot.

13:23.140 --> 13:29.800
Right command dot encode with UTF eight.

13:31.510 --> 13:32.200
Okay.

13:32.320 --> 13:40.660
And also what we could do is just send back a message to our bot to confirm that we have done the operation.

13:40.660 --> 13:41.200
Okay.

13:41.440 --> 13:45.260
Context dot, dot, dot send.

13:45.280 --> 13:47.230
So actually that's going to be the same as here.

13:50.050 --> 13:51.430
And the text.

13:52.270 --> 13:53.140
Let's just send.

13:53.160 --> 13:53.680
Okay.

13:54.810 --> 14:01.620
All right, now we have diff RGB handler, so you just name it as you want.

14:01.620 --> 14:05.670
That's just a function update context.

14:05.790 --> 14:08.280
But as you can see, I usually put handler.

14:08.310 --> 14:08.760
Okay.

14:08.760 --> 14:12.150
I put the name of the command and then handler.

14:12.150 --> 14:15.630
So that's it's easier to navigate the code.

14:16.480 --> 14:22.960
Okay, we have the text which is also update dot message dot text.

14:23.350 --> 14:26.290
We create the command is equal to.

14:27.430 --> 14:29.590
So the command this time is set.

14:32.150 --> 14:38.270
With Colon and we expect the user from Telegram to give us the correct format.

14:38.300 --> 14:38.680
Okay.

14:38.750 --> 14:40.710
For the command we're going to send.

14:40.820 --> 14:42.860
So this I'm just going to leave it like that.

14:42.860 --> 14:50.390
But as an improvement, what you could do is to validate the data here also and then try to find another

14:50.390 --> 15:00.020
way to send data with a less strict format plus backslash n at the end, let's not forget that.

15:00.020 --> 15:02.780
And then we just do the same thing here.

15:04.470 --> 15:09.060
We write the comment and we send back a message to the chat.

15:09.360 --> 15:13.350
Okay, now let's not forget to add those handlers.

15:13.350 --> 15:13.560
So.

15:13.560 --> 15:17.070
Dispatcher Dot add Handler.

15:19.110 --> 15:28.650
Command handler and we have so the keyword start is going to go with start handler.

15:29.490 --> 15:33.090
And then let's add another two of those.

15:34.830 --> 15:40.530
We will have the command LCD, which is going to go with the LCD handler.

15:40.590 --> 15:46.650
So you don't necessarily have to put the same command here and the same name for the function.

15:46.650 --> 15:47.060
It doesn't.

15:47.130 --> 15:48.240
It is not linked.

15:48.240 --> 15:48.450
Okay.

15:48.540 --> 15:54.480
Just use it for simplicity and Argb command for the RGB handler.

15:55.110 --> 15:55.470
Okay.

15:55.470 --> 16:05.460
After we have all the commands that are ready to be dispatched, we do a data dot, start polling.

16:06.180 --> 16:14.460
And the last thing is that when we actually close the program, we do closing sale and we then do print.

16:16.850 --> 16:22.520
Stopping telegram data.

16:24.080 --> 16:24.280
Okay.

16:24.440 --> 16:24.650
Where?

16:24.650 --> 16:28.340
We have to wait a few seconds and we do a data dot.

16:28.700 --> 16:29.480
Stop.

16:30.680 --> 16:32.150
So we start and we stop.

16:32.660 --> 16:35.030
All right, let's try this code.

16:35.900 --> 16:41.300
So we have written more than 50 lines of code, and let's just try.

16:41.330 --> 16:42.560
Let's see if it works.

16:43.370 --> 16:44.270
Activity.

16:46.960 --> 16:47.410
Eight.

16:50.420 --> 16:52.520
So let's see what we get here.

16:52.850 --> 16:55.270
Successfully connected to soil.

16:55.430 --> 16:55.970
Okay.

16:57.010 --> 17:03.260
Now let's open telegram and let's just send some comments.

17:03.260 --> 17:05.800
So LCD Hello.

17:05.830 --> 17:07.450
Let's see what we get.

17:08.320 --> 17:08.620
Okay.

17:08.620 --> 17:12.970
You can see on the screen we have not just hello, but we have slash LCD space.

17:12.970 --> 17:13.510
Hello.

17:13.690 --> 17:18.640
So we need to come back to our program and well, here we haven't tested.

17:18.640 --> 17:22.390
Okay, So it was kind of luck if it was going to work or not.

17:22.390 --> 17:29.110
But when you see that we get update dot message dot text, we get the complete text, okay.

17:29.380 --> 17:32.380
Even with the command and the space.

17:32.680 --> 17:36.240
So we need to remove that before we send it, of course.

17:36.250 --> 17:42.700
So what we can do is actually something similar that we did here in the on the Arduino is to remove

17:42.700 --> 17:44.380
the first N characters.

17:44.380 --> 17:45.580
Okay, let's do that.

17:45.610 --> 17:47.290
It's going to be simpler.

17:47.440 --> 17:49.150
So here we receive.

17:51.110 --> 17:55.420
So we received slash LCD space and then comment.

17:57.260 --> 18:02.520
So we have one, two, three, four, five characters to remove.

18:02.540 --> 18:07.310
So what we can do is just do five and colon and bracket.

18:07.310 --> 18:10.800
So a string is also an array of characters.

18:10.840 --> 18:14.750
Okay, So if you do that, you're going to remove the first five characters.

18:14.780 --> 18:17.750
Okay, let's run again.

18:21.890 --> 18:24.800
And once it is connected, let's send.

18:25.880 --> 18:26.810
The same.

18:27.600 --> 18:27.980
LCD.

18:28.020 --> 18:28.710
Hello.

18:31.590 --> 18:37.230
And you can see now we have hello on the screen.

18:37.590 --> 18:44.770
And so I'm going to come back to the program to change, of course, here for the RGB handler.

18:44.790 --> 18:46.860
So for the RGB handler we have.

18:48.550 --> 18:51.780
Slash RGB and then comment.

18:51.940 --> 18:56.320
So we have also one, two, three, four, five characters to remove.

18:56.350 --> 19:03.670
So we do this five colon bracket to take everything after the fifth character.

19:04.330 --> 19:05.740
Let's run that.

19:09.800 --> 19:11.270
Say, ah, okay.

19:11.510 --> 19:15.050
And let's try with Slash.

19:16.790 --> 19:24.530
Let's give the same color as before 200 zero 180 and let's see what we get.

19:24.560 --> 19:29.240
You can see now the RGB is purple.

19:29.270 --> 19:37.100
Okay, let's bring some text LCD, It works and well, it correctly works.

19:37.940 --> 19:42.830
We can control the Arduino components from Telegram Chat.

19:43.160 --> 19:43.580
All right.

19:43.580 --> 19:46.160
And this is the end of this activity.
