WEBVTT

00:00.440 --> 00:03.640
You can send some data from your Arduino to your Raspberry Pi.

00:03.650 --> 00:08.900
And let's start again to see how to send data from the Raspberry Pi to the Arduino.

00:08.900 --> 00:11.350
And so we are not going to start from scratch.

00:11.360 --> 00:17.330
Okay, I'm just going to remove that code here and that code here.

00:18.100 --> 00:20.920
So we keep the same structure on the Arduino.

00:20.950 --> 00:27.340
We're going to initialize Serial and make sure it's up and on the Raspberry Pi, we're the same thing.

00:27.340 --> 00:35.380
We open the serial, we wait a bit, reset input buffer, and then we have this infinite while loop,

00:35.380 --> 00:42.940
which is basically the same as while loop with the accept keyboard interrupt to close the serial communication

00:42.940 --> 00:44.890
when we kill the program.

00:45.010 --> 00:45.250
Okay.

00:45.250 --> 00:47.800
And here we're going to do our stuff here.

00:47.800 --> 00:52.210
So as always, let's start with Arduino here.

00:52.210 --> 00:55.360
What we want to do is we don't want to send data.

00:55.360 --> 00:58.930
We want to receive data and to receive data.

00:58.930 --> 01:04.240
Well, we are going to do basically the same as what we did previously with the Raspberry Pi, which

01:04.240 --> 01:07.750
is to first check if we have received some data.

01:07.750 --> 01:11.170
And if yes, read the next line.

01:11.290 --> 01:20.930
Okay, So what we can do is if serial dot so we have a function which is dot available.

01:21.140 --> 01:21.350
Okay.

01:21.350 --> 01:23.930
You can see it should turn orange.

01:23.930 --> 01:31.010
So parentheses, if this is strictly greater than zero, then we have received some data.

01:31.010 --> 01:37.940
So this function is going to return the number of bytes that the Arduino has received in the buffer,

01:37.940 --> 01:43.820
which is the same thing as the serial in waiting attribute we have in the Raspberry Pi.

01:43.850 --> 01:45.410
Okay, that's the same thing.

01:45.410 --> 01:47.510
So this is the number of bytes.

01:47.510 --> 01:49.490
We don't really care about the number of bytes.

01:49.520 --> 01:55.040
We just care that the number of bytes is greater than zero, which means we have received something.

01:55.040 --> 01:58.880
If this is the case, well, I'm going to just put string.

01:59.060 --> 02:06.740
Let's say message is equal to we can do serial dot read string.

02:07.490 --> 02:09.920
This is going to read the next string.

02:09.920 --> 02:15.620
So it's going to also decode it and put that inside a string message.

02:15.620 --> 02:16.940
So very simple.

02:16.940 --> 02:21.170
Now if you do that and that's where I'm going to talk about the timeout.

02:21.170 --> 02:26.660
So basically there is a reading timeout of one second by default.

02:26.660 --> 02:32.780
So when you use this, the program is going to be stuck for at least one second.

02:32.810 --> 02:33.020
Okay.

02:33.020 --> 02:39.920
So basically, as long as you receive some strings, it's going to wait one second to be sure that it

02:39.920 --> 02:43.820
hasn't received more than what you've already received.

02:43.820 --> 02:49.460
And so if we just run like this and then we send data from the Raspberry Pi to the Arduino, well,

02:49.460 --> 02:53.180
we're going to see some delay in the processing on the Arduino.

02:53.540 --> 02:56.150
So what we can do, there are two options.

02:56.150 --> 03:03.170
The first option is to do serial dot set timeout with.

03:03.200 --> 03:05.900
So the default is 1000 milliseconds.

03:06.200 --> 03:08.840
What we can do is maybe put ten milliseconds.

03:08.990 --> 03:14.270
So that's going to be much faster, but still it's going to be stuck for at least ten milliseconds.

03:14.480 --> 03:22.430
And the lower you go, actually, the higher the risk of missing some data or of just reading so fast

03:22.430 --> 03:26.900
that you don't receive everything and you receive an incomplete message.

03:26.900 --> 03:29.600
So ten milliseconds should work.

03:29.750 --> 03:34.160
But I'm not going to spend too much time on this because we are not going to use set timeout.

03:34.160 --> 03:42.680
Instead, I'm going to use another function which is read string until and I'm going to put the backslash

03:42.710 --> 03:49.040
n character with and this is important single quotes and not double quotes.

03:49.070 --> 03:50.600
Okay, single quotes.

03:50.630 --> 03:52.850
Again, this is super, super important.

03:52.850 --> 03:59.720
So this function is going to read the string until it gets to this character and this function is going

03:59.720 --> 04:03.710
to return as soon as it detects this character.

04:03.710 --> 04:06.110
So the timeout still applies.

04:06.110 --> 04:10.340
So we still have a timeout of one second that's going to block this one second.

04:10.340 --> 04:16.040
But if you receive that character, the timeout doesn't apply anymore and you just get the string.

04:16.040 --> 04:22.850
So basically if we send here from the Raspberry Pi a string with a backslash n character at the end,

04:22.850 --> 04:28.160
so a new line, then we're going to be able to read it immediately with this function.

04:28.160 --> 04:34.820
And so this is basically the function I'm going to use for all the codes read string until which is

04:34.820 --> 04:42.230
quite powerful, quite efficient, and it's going to give us directly a string that we can process afterwards.

04:42.470 --> 04:42.830
Okay.

04:42.830 --> 04:48.860
And then once we have received this message, we can process it and do whatever we want in the Arduino.

04:48.860 --> 04:49.520
All right.

04:49.520 --> 04:53.510
Now I'm going to go back to the Python code on the Raspberry Pi.

04:53.540 --> 04:57.410
So first I'm going to upload that actually to the Arduino.

04:58.130 --> 05:03.320
So now it's running and it's waiting for data on Python.

05:03.320 --> 05:07.430
If you want to send something on the sail, what you can do is do.

05:07.840 --> 05:10.610
So you use the set object we have here, okay?

05:10.610 --> 05:17.330
From cell cell dot write and then you're going to put.

05:17.690 --> 05:18.470
You want to write?

05:18.470 --> 05:22.460
Let's say hello from Raspberry Pi.

05:23.350 --> 05:26.720
And we are going to add a backslash n character at the end.

05:26.740 --> 05:28.960
So that's very important.

05:28.990 --> 05:29.520
Okay.

05:29.530 --> 05:33.940
Because if you don't send that, this is going to wait one second.

05:33.970 --> 05:36.520
So it's still going to receive hello from Raspberry Pi.

05:36.550 --> 05:43.660
But after one second, if you add the backslash n as soon as this function detects the backslash, N

05:43.690 --> 05:49.330
is going to return with this exact string and nothing is going to be stuck.

05:49.480 --> 05:51.580
And then we're coming back here.

05:51.610 --> 05:56.920
As you have seen previously, when we read data from Samuel, we need to decode it.

05:56.950 --> 06:00.910
Well, here when we write data, we need to encode it.

06:00.910 --> 06:09.490
So you're going to do dot Encode with UTF eight, so the same UTF eight as before.

06:09.580 --> 06:16.810
And it's very important here that the dot encode is directly on the string.

06:16.870 --> 06:21.700
Okay, you don't do this, okay, you don't do self dot, right?

06:21.700 --> 06:28.970
And then dot encode No, you do set dot right with the string dot encode and then you end the parenthesis

06:28.970 --> 06:29.570
here.

06:29.750 --> 06:29.870
Okay.

06:29.870 --> 06:32.960
So make sure you have the correct order for the parentheses.

06:32.990 --> 06:34.610
That's super, super important here.

06:35.300 --> 06:39.470
All right, so this is going to send hello from Raspberry Pi to the Arduino.

06:39.530 --> 06:42.080
Maybe we can change the time.sleep here.

06:42.080 --> 06:43.280
Let's put one second.

06:43.280 --> 06:44.990
So we send this every second.

06:45.020 --> 06:47.210
Now the code is running on the Arduino.

06:47.210 --> 06:50.330
I'm going to run the code also on the Raspberry Pi.

06:50.360 --> 06:52.010
Let's see what we get.

06:53.810 --> 06:54.690
Okay, So, yeah.

06:54.740 --> 06:55.190
Okay.

06:55.190 --> 07:01.580
So it's running actually, let's do control C and let's add a print here.

07:01.970 --> 07:07.190
Print send message to Arduino.

07:08.090 --> 07:08.810
All right.

07:08.930 --> 07:12.740
So we can have a better idea of what's going on.

07:15.610 --> 07:15.770
Okay.

07:15.930 --> 07:17.050
Send message to Arduino.

07:17.070 --> 07:18.960
Send message to Arduino Et-cetera.

07:22.430 --> 07:23.000
Now?

07:23.000 --> 07:30.710
Well, what we did before, when we actually sent some data from Arduino to Raspberry Pi is that we

07:30.710 --> 07:35.630
could directly print the data we got on the terminal here or on the shell.

07:35.660 --> 07:43.460
But now we send data from Raspberry Pi to Arduino, and how can we actually check and print and debug

07:43.460 --> 07:45.350
this data we've received?

07:45.380 --> 07:51.200
We could do serial dot println with the message.

07:51.200 --> 07:51.740
Okay.

07:51.740 --> 07:57.500
But the thing is that here on the Arduino we are using println to send message to the Raspberry Pi and

07:57.500 --> 07:58.700
also to debug.

07:58.730 --> 07:59.090
Okay.

07:59.090 --> 08:04.490
So you can't just use these to print the message, for example, on the serial monitor because you're

08:04.490 --> 08:08.450
already using the serial communication with the Raspberry Pi.

08:08.480 --> 08:15.170
So that's one thing that is very important is that on the Arduino side, well, when you receive something

08:15.170 --> 08:19.310
from Serial, don't just print it or you can just print it like this.

08:19.340 --> 08:26.160
If you want to test just with the Arduino and the monitor, but then when you communicate with the Raspberry

08:26.160 --> 08:29.860
Pi, don't just print some logs of some debug.

08:29.880 --> 08:36.180
For example, here, if you print serial communication ready here, if you print received some string,

08:36.180 --> 08:40.200
well, everything that you print actually is going to be sent back to the Raspberry Pi.

08:40.290 --> 08:40.410
Okay.

08:40.410 --> 08:43.470
And that's something you don't want if it's just for debugging.

08:43.650 --> 08:50.250
So, well, we are left with this and well, how to debug that?

08:50.280 --> 08:53.640
That's actually a good question and there are different ways to do this.

08:53.640 --> 08:57.870
So first, of course, as I told you, you could just print what you received.

08:58.200 --> 09:02.640
Run with the serial monitor, check if everything good, you just remove the print.

09:02.970 --> 09:05.880
Another option would be to use an external component.

09:05.880 --> 09:10.320
For example, an LCD screen that you plug to your Arduino.

09:10.320 --> 09:13.500
And that's actually what we are going to do in the following of this course.

09:13.500 --> 09:18.180
So you could receive some stuff from sale and print that on the LCD screen.

09:18.180 --> 09:24.150
And what you can also see as a confirmation that things are working, I mean, at least that you receive

09:24.150 --> 09:25.400
data here is.

09:25.410 --> 09:29.370
So let's run the script again and let's look at the Arduino board.

09:30.540 --> 09:36.360
And you can see the X is blinking every one second and I'm going to kill this.

09:36.900 --> 09:37.140
Okay.

09:37.140 --> 09:42.390
And the X is not blinking anymore, so ah, x is for reception.

09:42.510 --> 09:49.110
So as you can see, if the ah x is on, it means you are receiving data on the Arduino.

09:49.350 --> 09:49.830
All right.

09:49.830 --> 09:56.640
And now that we have seen how to send data only from one side to the other, let's check how to do a

09:56.640 --> 09:58.710
bidirectional communication.
