WEBVTT

00:00.080 --> 00:00.560
Great.

00:00.560 --> 00:07.370
You have now successfully initiated serial communication between your Raspberry Pi and Arduino boards.

00:07.400 --> 00:14.060
Let's now see how to send some data starting from the Arduino side to your Raspberry Pi.

00:14.150 --> 00:15.800
So this is going to send data.

00:15.830 --> 00:17.770
This is going to receive data.

00:17.780 --> 00:23.350
So once again, we're going to start on the Arduino side because that's going to be easier to debug.

00:23.360 --> 00:29.000
So on the void setup, we initialize the communication with that baud rate and we make sure the serial

00:29.000 --> 00:29.990
is ready.

00:30.290 --> 00:31.760
So that's pretty much it.

00:31.790 --> 00:37.760
Now what we want to do is to send some string, let's say hello every one second.

00:37.760 --> 00:40.220
So we're going to do that in the void loop.

00:40.940 --> 00:48.210
We are going to do serial dot println with Hello from Arduino.

00:49.370 --> 00:55.760
As you can see, when you use Serial.println or if you use silent print, if you have ever used that

00:55.760 --> 00:58.670
before to debug your Arduino, well, that's the same thing.

00:58.700 --> 01:07.130
To send data over Serial and I'm going to use println here so we can add a new line character after

01:07.130 --> 01:08.300
this string.

01:08.300 --> 01:11.000
And I'm going to add also a delay.

01:11.000 --> 01:18.500
So let's use delay for simplicity here of one second so we can send that every second and not all the

01:18.500 --> 01:18.940
time.

01:18.950 --> 01:19.460
All right.

01:19.460 --> 01:21.830
Make sure that nothing is running on the Raspberry Pi.

01:21.860 --> 01:23.240
So no Python script.

01:23.270 --> 01:25.430
Let's upload the code to the Arduino.

01:26.760 --> 01:28.110
Okay, done uploading.

01:28.110 --> 01:35.460
And now what we can do before we write anything with Python, we can open the serial monitor to debug

01:35.460 --> 01:40.530
what we have and you can see we have hello from Arduino every second.

01:42.140 --> 01:49.220
So by starting on the Arduino, when you use serial communication, you can make sure that this side

01:49.220 --> 01:51.980
is working with the serial monitor.

01:51.980 --> 01:53.800
So this tool is super useful.

01:53.810 --> 01:59.810
So now we know that the code is correctly working and correctly sending this string over here.

02:00.200 --> 02:05.090
So that's going to be easier to debug when we actually write the Python program because we know that

02:05.090 --> 02:06.560
this side is working.

02:06.560 --> 02:09.580
So now here what are we going to do?

02:09.590 --> 02:16.550
So we want to be able to read this string that we receive and to print it on the terminal.

02:16.550 --> 02:17.800
How can we do that?

02:17.810 --> 02:22.190
Well, we are going to create an infinite loop here to read the string.

02:22.190 --> 02:28.910
And if you see here, basically on the Arduino, we have a void setup which is going to run once to

02:28.910 --> 02:30.170
initialize stuff.

02:30.170 --> 02:33.200
And here that's pretty much what we have also.

02:33.200 --> 02:37.310
Okay, we are going to run that once to initialize communication.

02:37.310 --> 02:43.200
So this part of the code on the Raspberry Pi with Python is actually similar to the void setup we have

02:43.230 --> 02:48.690
on the Arduino and then we have an infinite loop to read and send data.

02:48.690 --> 02:57.330
So we are going to also create our own infinite loop, our own void loop on the Raspberry Pi with Python.

02:57.330 --> 02:57.630
Okay.

02:57.630 --> 03:03.630
So we can have a structure that is similar to what we have on the Arduino and how do we create a void

03:03.630 --> 03:04.140
loop?

03:04.140 --> 03:05.430
Well, we simply do.

03:05.460 --> 03:07.350
While true.

03:07.530 --> 03:08.580
And that's it.

03:08.610 --> 03:10.170
We have an infinite loop.

03:10.170 --> 03:10.470
Okay.

03:10.620 --> 03:15.330
While true and in this loop we are going to read serial communication.

03:15.330 --> 03:23.100
And actually the first thing I'm going to do is do time dot sleep with, let's say, 0.01 second.

03:23.130 --> 03:23.370
Okay.

03:23.370 --> 03:29.880
So here you can run the void loop as fast as you can, basically as fast as the microcontroller can.

03:29.910 --> 03:33.240
That's not a problem because anyway, the Arduino is just doing that.

03:33.360 --> 03:40.350
But on the Raspberry Pi, if you run an infinite loop at full speed, what's going to happen is that

03:40.380 --> 03:47.580
it's going to take all the resources of your CPU for just one Python program and it may make the Python

03:47.580 --> 03:51.240
program very slow or anything else that you do very slow.

03:51.240 --> 03:55.110
So that's why I'm going to add a time dot sleep with.

03:55.110 --> 04:01.770
Let's use this, which means that this is going to be executed 100 times per second, which is more

04:01.770 --> 04:04.680
than enough for this lesson and also for this course.

04:04.680 --> 04:08.010
So we have our own kind of void loop.

04:08.010 --> 04:10.260
What do we do in that void loop?

04:10.260 --> 04:17.100
Well, we are going to check first if we have received something from the Arduino and if yes, we are

04:17.100 --> 04:18.420
going to read that thing.

04:18.420 --> 04:18.990
Okay.

04:18.990 --> 04:19.890
So we can do.

04:19.920 --> 04:21.330
If say so.

04:21.330 --> 04:27.390
We use the cell that we have created here dot in waiting.

04:27.570 --> 04:33.390
This is going to return the number of bytes that have been received from the Arduino.

04:33.540 --> 04:36.180
So we don't really care about the number of bytes.

04:36.180 --> 04:40.080
What we care about is have we received some data?

04:40.080 --> 04:45.420
If we have received some data, it means that the number of bytes is simply greater than zero.

04:45.450 --> 04:51.900
We don't need to count them, but just knowing that it's greater than zero means we have received something.

04:51.930 --> 04:54.000
Okay, one very important thing here.

04:54.030 --> 04:56.400
Don't put parentheses, okay?

04:56.400 --> 04:57.690
This is not a function.

04:57.690 --> 05:00.210
This is an attribute of cell.

05:00.690 --> 05:03.720
So press enter with the indentation here.

05:03.720 --> 05:09.930
If we have received something, let's say here we are going to read the next line.

05:09.930 --> 05:10.110
Okay.

05:10.140 --> 05:16.440
So line is equal to cell dot read line with parentheses.

05:16.470 --> 05:18.180
This is going to read the next line.

05:18.180 --> 05:19.920
What is the next line?

05:19.920 --> 05:28.290
So basically the next line is going to be everything until we get this backslash n character, which

05:28.290 --> 05:30.870
is basically a new line character.

05:30.870 --> 05:33.120
And now you can see that's very important here.

05:33.150 --> 05:35.250
That's why I have added line.

05:35.250 --> 05:43.860
So print l n So basically this is going to send hello from Arduino with a new line character after this.

05:43.860 --> 05:52.020
And so on the Raspberry Pi with read line it's going to read Hello from Arduino until that new line

05:52.020 --> 05:53.640
character that's going to be here.

05:53.670 --> 05:55.110
So now we have our line.

05:55.110 --> 05:57.390
But basically this is encoded okay?

05:57.420 --> 06:03.630
Cell is not just going to send the string like this, it's going to encode it so it can better communicate.

06:03.630 --> 06:03.960
Okay.

06:03.960 --> 06:07.110
So basically what you receive here are just bytes.

06:07.140 --> 06:07.680
Okay?

06:07.680 --> 06:11.100
If you want to read them as a string, you can do dot.

06:11.800 --> 06:23.830
Decode and then UTF eight that is going to decode what you get with ReadLine into a correct string that

06:23.830 --> 06:24.880
you can read here.

06:25.330 --> 06:28.180
And then let's do print line.

06:28.600 --> 06:29.350
All right.

06:29.350 --> 06:31.810
So basically, this is going to run forever.

06:31.810 --> 06:38.980
And until you press Ctrl C at 100Hz, it's going to check if you have received something of a cell.

06:39.010 --> 06:43.420
If yes, it's going to read it and decode it and print it.

06:43.450 --> 06:48.910
Now I'm going to do something here is of course, I'm not going to close the communication before we

06:48.910 --> 06:50.050
actually start to use it.

06:50.050 --> 06:54.580
So I'm going to put that at the end of the program.

06:54.730 --> 07:02.890
And maybe you will see a problem here is that if we press Ctrl C inside this while loop, we are never

07:02.890 --> 07:08.510
going to go to line 16 and we are never going to close correctly the communication.

07:08.580 --> 07:08.800
Okay.

07:08.800 --> 07:12.210
So if I put this line here, it's never going to be called.

07:12.230 --> 07:20.420
So what I am going to do before I run the program is I'm going to put this while loop inside a try except

07:20.420 --> 07:21.290
structure.

07:21.380 --> 07:21.590
Okay.

07:21.590 --> 07:25.100
So I can catch the keyboard interrupt exception.

07:25.100 --> 07:29.300
And basically when we price control C, we are going to close the communication.

07:29.340 --> 07:30.830
Okay, so let's do that now.

07:31.040 --> 07:35.330
Let's do try and put that with one more indentation.

07:35.330 --> 07:45.530
So you select everything and you press tab and then come back to new line except keyboard interrupt

07:45.530 --> 07:46.370
like this.

07:46.700 --> 07:55.760
So basically, for example, when we press control C and we do C dot close inside the handle of the

07:55.760 --> 07:56.480
interrupt.

07:56.480 --> 08:05.750
And I'm going also to just add here, print close cell communication.

08:06.320 --> 08:06.590
Okay.

08:06.590 --> 08:09.210
So we know that this has been executed.

08:09.230 --> 08:09.620
All right.

08:09.620 --> 08:11.090
So now let's run this.

08:11.090 --> 08:13.640
So the code here is already running on the Arduino.

08:13.670 --> 08:16.430
I'm going to run here from the Raspberry Pi.

08:18.380 --> 08:18.830
Okay.

08:18.830 --> 08:19.790
So you can see.

08:20.270 --> 08:20.630
Okay.

08:20.630 --> 08:23.960
And then hello from Arduino every second.

08:24.230 --> 08:24.860
Great.

08:25.250 --> 08:29.450
Now I'm going to press control C, I'm going to click here and press control C.

08:30.780 --> 08:33.800
And you can see close cell communication.

08:33.810 --> 08:34.150
Okay.

08:34.170 --> 08:39.660
So if you are running the script from the terminal, well, to kill the script, you just do control

08:39.690 --> 08:43.560
C and if you are running it directly from Thonny Python ide.

08:43.890 --> 08:46.860
Well, you need to click here and press control.

08:46.890 --> 08:47.670
C Okay.

08:47.670 --> 08:51.600
If you click on the stop button, it's not going to do the same.

08:51.600 --> 08:54.210
So make sure that you click here and you press control.

08:54.240 --> 08:57.600
C Okay, close communication as you can see.

08:57.600 --> 09:00.150
Well we receive the string every second.

09:00.150 --> 09:06.930
That's great, but we still have some new line and some characters that actually are not part of that

09:06.930 --> 09:07.500
string.

09:07.500 --> 09:14.010
So when you receive something with Red Line, you may often have some new line characters that are added

09:14.010 --> 09:15.870
or some carriage return.

09:15.870 --> 09:21.750
And in that case, what you can do is after the code, you can do dot r strip.

09:22.410 --> 09:28.500
So this function is going to remove any new line character or any carriage return that you have on a

09:28.500 --> 09:28.980
string.

09:28.980 --> 09:32.530
So you just have the hello from Arduino and nothing else.

09:32.680 --> 09:34.330
Okay, let's run that again.

09:37.000 --> 09:37.660
Sale.

09:37.690 --> 09:38.310
Okay.

09:38.320 --> 09:43.510
You can see hello from Arduino every second without any other character.

09:43.510 --> 09:47.440
I click here, I press Ctrl, C, close cell communication.

09:47.590 --> 09:53.530
And one thing I want to show you is if you look at the so I'm going to run the script if you look at

09:53.530 --> 09:54.820
the Arduino board.

09:56.460 --> 10:04.050
You will see the text is going to blink every one second at the same time that you send the string.

10:04.350 --> 10:04.950
All right.

10:05.130 --> 10:11.370
You can see here and now I'm going to do control C and it stops blinking.

10:11.370 --> 10:13.230
And now, well, it's just pulling on.

10:13.530 --> 10:17.520
So basically, you have this led that's going to help.

10:17.520 --> 10:23.170
You also see when you are sending data from Arduino to Raspberry Pi.

10:23.190 --> 10:25.410
So T means transmission.

10:25.410 --> 10:31.110
And so it's yet another tool to help you see what's going on in your application.

10:31.110 --> 10:36.600
And one last thing I want to show you is that actually, let's say you use a different baud rate here.

10:36.600 --> 10:39.360
So let's say I use 9600.

10:39.930 --> 10:44.070
I still use this one on the Arduino, but I use that one on the Raspberry Pi.

10:44.250 --> 10:45.960
So let's run the program.

10:48.040 --> 10:50.100
And let's see what happens.

10:50.890 --> 10:53.710
Okay, so the communication can be started.

10:53.740 --> 10:54.400
Great.

10:55.060 --> 10:56.110
But then.

10:56.110 --> 11:00.340
Well, it seems that we don't receive anything.

11:00.400 --> 11:00.640
Okay.

11:00.640 --> 11:05.680
The communication is not working, so you can see that if you don't have the same baud rate, you're

11:05.680 --> 11:10.150
going to have weird behavior because the communication is going to be opened.

11:10.180 --> 11:13.060
But then anything else here is not going to work.

11:13.140 --> 11:18.430
Okay, press Ctrl C close communication, but we haven't received anything.

11:18.880 --> 11:19.390
Okay.

11:19.600 --> 11:23.260
I go back to this one.

11:23.980 --> 11:29.560
Let's run the script and say, okay, hello from Arduino.

11:30.130 --> 11:33.070
Cetera Press Ctrl C and it's working correctly.

11:33.070 --> 11:34.690
So baud rate.

11:34.690 --> 11:37.870
Super, super, super important.
