WEBVTT

00:00.080 --> 00:05.870
This is the solution of the first activity of this course where you have to power on or power off an

00:06.140 --> 00:10.400
on the Arduino from a user input you get from the Raspberry Pi.

00:10.400 --> 00:11.600
And let's get started.

00:11.600 --> 00:20.360
So I have created a new project file in Python here on Thonny Python and also a new project on Arduino.

00:20.390 --> 00:23.300
Okay, so start from a blank page.

00:23.300 --> 00:29.270
Now, the first thing before we even start to write some code is to define the protocol.

00:29.270 --> 00:34.520
So I've already done that actually for you in the first activity, but I'm going to talk about that

00:34.520 --> 00:36.260
just a few more seconds.

00:36.290 --> 00:36.830
Okay.

00:37.250 --> 00:43.610
So as you can see, it's super important to know in advance what data you are going to send and receive

00:43.610 --> 00:44.870
from both sides.

00:44.900 --> 00:45.500
Okay.

00:45.740 --> 00:54.410
As you can see here, with on and off, we send that and we receive that if we don't agree on both sides,

00:54.410 --> 00:58.370
okay, to send the same data, then we are going to have some problems.

00:58.400 --> 00:58.670
Okay.

00:58.670 --> 01:04.240
Think about what data you need to send and receive and then write this in advance in, for example,

01:04.240 --> 01:06.320
a text document or in a comment.

01:06.330 --> 01:11.940
This is the protocol you're going to use, and for each application you are going to do well.

01:11.940 --> 01:15.930
You are going to define a custom protocol before you write any code.

01:16.080 --> 01:16.260
Okay.

01:16.260 --> 01:21.000
Let's say, for example, here, I wanted to send an Led on an Led off.

01:21.000 --> 01:28.470
Well, if I send this and if I try to read on, well, you can see that if I send led on, I'm not going

01:28.470 --> 01:32.850
to be able to detect on because this is not the same string.

01:32.880 --> 01:33.060
Okay.

01:33.060 --> 01:38.610
So I need to make sure that I send something that is the same on both sides.

01:38.970 --> 01:39.420
All right.

01:39.420 --> 01:42.630
So now let's start with the Arduino code.

01:42.750 --> 01:42.960
Okay.

01:42.960 --> 01:47.640
So once again, we start from Arduino because that's simply easier to debug.

01:47.640 --> 01:47.820
Okay.

01:47.820 --> 01:53.430
We can start with this code, debug it with the monitor, make sure that the communication is correctly

01:53.430 --> 01:58.680
working on that side and then we can create the other side.

01:58.830 --> 02:01.980
So here we have two things to do.

02:02.010 --> 02:06.060
We have to set up the Led and we have to set up the communication.

02:06.060 --> 02:08.040
I'm going to start with the Led.

02:08.070 --> 02:08.280
Okay.

02:08.280 --> 02:13.140
So we first start with the Led and then we use the serial and from the side we can directly make the

02:13.170 --> 02:17.070
Led blink so that we can check that the serial is correctly working.

02:17.070 --> 02:23.700
So let's create a define Led PIN 13 for the Led.

02:23.700 --> 02:31.590
And then in the setup I am going to do pin mode led pin output.

02:32.550 --> 02:35.160
All right, So we have a define.

02:35.160 --> 02:37.590
We have set output mode for the Led.

02:37.590 --> 02:41.970
So now we can do digitalwrite on the Led with high or low.

02:41.970 --> 02:46.170
And so well, when are we going to do digitalwrite with high and low?

02:46.200 --> 02:49.860
We are going to do that when we receive some data from the cell.

02:49.860 --> 02:55.920
So in the while loop, what I'm going to do is I'm going to read from Serial, but before I'm going

02:55.920 --> 03:02.790
to of course, initialize Serial with Begin, let's use this baud rate, okay?

03:02.790 --> 03:09.690
And let's do while on serial like this with exclamation mark.

03:11.580 --> 03:13.740
So not needed on Arduino Uno.

03:13.740 --> 03:18.540
But for example, if you have a Leonardo or a zero board, well, you're going to need that.

03:18.540 --> 03:20.100
So I'm going to put that anyway.

03:20.370 --> 03:20.790
All right.

03:20.820 --> 03:22.380
Cell is initialized.

03:22.800 --> 03:24.600
We have everything in the void setup.

03:24.600 --> 03:29.880
Now in the void loop I'm going to do if cell dot.

03:30.990 --> 03:35.130
With a label like this with parentheses is greater.

03:35.130 --> 03:40.540
Strictly greater than zero means that we have received some data over Serial.

03:40.560 --> 03:43.650
Then we can read this and we are going to do string.

03:44.100 --> 03:48.510
Let's create a cmd variable which means command.

03:48.510 --> 03:52.620
So usually I prefer to write the full name for the variables.

03:52.620 --> 03:58.320
But when you have something like command where CMD is pretty clear and because we are going to use the

03:58.320 --> 04:03.030
CMD variable a lot in the following, so I'm just going to go with string cmd.

04:03.240 --> 04:03.780
Okay.

04:04.110 --> 04:05.670
Serial dot.

04:07.140 --> 04:11.530
Read string until end.

04:11.550 --> 04:15.690
Backslash n with important single quotes here.

04:16.500 --> 04:16.920
All right.

04:16.920 --> 04:17.850
And semicolon.

04:18.570 --> 04:24.440
So basically we use the same function as before, and now we have the comment.

04:24.450 --> 04:26.580
So we receive a comment from here.

04:26.730 --> 04:29.550
What we are going to do is we are going to check that comment.

04:29.550 --> 04:38.910
So if the comment is equal to UN, okay, So if you want to do a comparison with String with the Arduino

04:38.910 --> 04:41.850
string, you can simply use the two equal sign.

04:42.270 --> 04:47.340
So if the comment is on what do we do?

04:47.520 --> 04:51.690
Well, we do digital, right?

04:54.190 --> 05:01.150
With the pin and height and if the command is off.

05:01.150 --> 05:03.490
So let's do an else if actually.

05:04.060 --> 05:07.840
Command is off.

05:09.010 --> 05:14.950
We are going to do digital right led pin with low.

05:15.130 --> 05:16.390
So if we receive.

05:16.430 --> 05:17.950
On we power on the Led.

05:18.160 --> 05:26.890
If we receive off we power off the Led and then I'm going to add an else because well you could receive

05:26.890 --> 05:32.140
any string here you could receive on, you could receive off but you could receive anything.

05:32.140 --> 05:32.650
Okay.

05:32.650 --> 05:34.720
You don't know what's going to happen.

05:34.720 --> 05:37.540
You don't know what the other side is going to send.

05:37.570 --> 05:38.080
Okay.

05:38.080 --> 05:44.230
And as a basic principle, when you write some code like this, you don't trust the other side.

05:44.260 --> 05:44.650
Okay?

05:44.650 --> 05:47.110
Here you are writing the other side, so that's good.

05:47.110 --> 05:50.860
But usually don't trust what the other side sends you.

05:50.860 --> 05:51.370
Okay?

05:51.640 --> 05:57.680
So make sure to always check your receive on your receive off or you receive something else that can

05:57.680 --> 05:58.100
happen.

05:58.100 --> 05:58.580
Okay.

05:58.580 --> 06:05.360
So if you receive something else, well, what we're going to do in that case is just, well, nothing.

06:05.510 --> 06:07.160
So I'm going to keep it like that for now.

06:07.160 --> 06:12.890
But for example, you could print an error so you can send back an error, for example, or you can

06:12.890 --> 06:15.800
print an error on an LCD screen.

06:15.800 --> 06:18.230
Or you could do another behavior.

06:18.230 --> 06:21.050
For example, you could flush the input buffer.

06:21.050 --> 06:22.970
We are going to do that a bit later.

06:23.000 --> 06:23.510
Okay.

06:23.510 --> 06:26.780
But for now, you can see we check if we have some data.

06:26.780 --> 06:31.010
If yes, we read the data, that's going to be our command string.

06:31.040 --> 06:34.790
We check if it's equal to on, then we do an action.

06:34.790 --> 06:41.180
We check if it's equal to off, we do an action and then else everything else we can do something else

06:41.180 --> 06:43.310
that we want, but for now we do nothing.

06:43.310 --> 06:45.740
All right, let's now try this code.

06:45.770 --> 06:47.540
But we don't need to write the python code.

06:47.540 --> 06:49.640
We can just start this.

06:50.180 --> 06:53.780
Let's make sure that we have the Arduino plugged.

06:55.080 --> 06:55.890
Okay, let's.

06:55.890 --> 06:58.650
Oops, I have selected the wrong board.

06:58.650 --> 07:03.390
So tool Arduino Uno and the port is correct.

07:04.020 --> 07:11.610
Now I'm going to upload this, so let's save it as activity one.

07:12.270 --> 07:12.570
Okay.

07:12.570 --> 07:18.780
Enter compiling, uploading.

07:19.710 --> 07:25.740
And now that the code on the Arduino is running, what we can do is we can already try this behavior

07:25.740 --> 07:27.000
with the serial monitor.

07:27.000 --> 07:28.470
So we're going to open.

07:28.890 --> 07:34.380
So instead of having a python program running on the Raspberry Pi, we just have the serial monitor

07:34.380 --> 07:37.110
running on the Raspberry Pi and we can send command from there.

07:37.140 --> 07:37.620
All right.

07:37.620 --> 07:40.590
So make sure that you have the same baud rate here, okay?

07:41.640 --> 07:41.970
Check.

07:41.970 --> 07:42.780
This is correct.

07:42.780 --> 07:43.470
All right.

07:43.470 --> 07:48.510
And then new line, because we are going to send a new line character at the end.

07:48.540 --> 07:48.700
Okay.

07:48.720 --> 07:50.520
So we don't need to write it manually.

07:50.520 --> 07:52.320
And then I'm going to send some comments.

07:52.320 --> 08:03.090
So let's say I send on you can see the Led here is powered on now I send off the Led is powered off,

08:03.090 --> 08:06.450
I send on again and I can power on the Led.

08:06.480 --> 08:12.540
Now if I send let's say led on which is not correct nothing is going to happen led off.

08:12.750 --> 08:14.190
Nothing is going to happen.

08:14.190 --> 08:16.620
Any other string, nothing is going to happen.

08:16.620 --> 08:23.490
You can see we still have the X which is blinking because we are receiving some data, but then the

08:23.490 --> 08:30.360
data is processed in the code and it goes directly to the else which does nothing.

08:30.360 --> 08:30.740
Okay.

08:30.750 --> 08:34.530
So you have to send exactly the on and off command to do something.

08:34.530 --> 08:40.410
And you can see that with our experimentation, the behavior is correct here.

08:40.410 --> 08:44.560
So we can know that the Arduino code is correct.

08:44.560 --> 08:47.380
And now we can write the code on the Raspberry Pi.

08:47.920 --> 08:51.520
So let's start by initializing the communication.

08:51.520 --> 08:53.050
So I'm going to go with.

08:53.080 --> 08:59.500
So this is a bit of Python three for the interpreter.

08:59.500 --> 09:02.020
And then let's import Serial.

09:02.020 --> 09:05.170
Let's also import time because we're going to need that library.

09:06.450 --> 09:13.850
And I am going to do, sir, is equal to cereal dot cereal with the port.

09:13.860 --> 09:15.870
So the port is still the same for me.

09:16.410 --> 09:21.330
Slash dev slash tty ACM zero.

09:21.600 --> 09:21.840
Okay.

09:21.840 --> 09:23.580
The baud rate very important.

09:23.730 --> 09:25.740
Exact same here.

09:26.370 --> 09:28.470
And then the read timeout.

09:28.950 --> 09:31.830
Let's put one second.

09:32.490 --> 09:36.660
Well for this case, we don't really need the timeout here.

09:36.660 --> 09:41.280
The read timeout because we're not going to read anything but I still put it anyway.

09:41.460 --> 09:47.700
So here we open the cell communication and we get the cell in the cell object here.

09:48.210 --> 09:51.240
I'm not going to do the retry code, okay?

09:51.240 --> 09:55.590
Just to keep it simple for this activity, but you can do it if you want to.

09:55.620 --> 10:01.890
So once we have this, I'm going to do a time dot sleep with three seconds.

10:01.920 --> 10:02.330
Okay.

10:02.340 --> 10:08.550
To wait because this is going to connect to the Arduino and here on the Arduino, no, it's going to

10:08.550 --> 10:09.780
restart the program.

10:09.780 --> 10:13.620
So we make sure that we have spent enough time so the Arduino is ready.

10:13.770 --> 10:26.130
And then we're going to do set dot reset input buffer, which is well, not needed as well here because

10:26.130 --> 10:30.270
we don't have anything in the input buffer because the Arduino doesn't send anything.

10:30.270 --> 10:36.210
But still, I'm going to keep it for best practice because we're going to initialize the cell the same

10:36.210 --> 10:37.140
way every time.

10:37.140 --> 10:37.740
All right.

10:37.740 --> 10:40.830
And let's say print cell.

10:40.830 --> 10:41.310
Okay.

10:42.570 --> 10:43.470
Like this.

10:43.500 --> 10:46.980
Now what we can do is we can do our while.

10:47.970 --> 10:48.810
True.

10:48.810 --> 10:49.350
Okay.

10:49.740 --> 10:53.340
So this is kind of the void setup of the Raspberry Pi.

10:53.340 --> 10:54.860
And this is the void loop.

10:54.870 --> 10:59.190
All right, void setup, void loop, void setup and void loop.

10:59.190 --> 11:05.340
So while true, so what I'm going to do is, of course, I'm going to do a try and accept.

11:05.340 --> 11:18.190
So let's just put pass here, try accept keyboard interrupt and when we press Ctrl C, I'm going to

11:18.190 --> 11:18.730
do so.

11:18.730 --> 11:29.770
I'm going to say print close serial communication and set dot close before we exit the program.

11:29.770 --> 11:30.340
Okay.

11:30.880 --> 11:36.070
So again, this is going to be the same every time and in the while true.

11:36.100 --> 11:36.610
What do we do?

11:36.610 --> 11:38.890
So this is the behavior of our void loop.

11:38.890 --> 11:44.230
Well, basically, we want the user input and from that user input we send data to the Arduino.

11:44.320 --> 11:50.650
So I'm going to do, let's say user input is equal to input.

11:50.920 --> 11:51.460
Okay.

11:51.550 --> 11:55.390
I can put some text here to send some instruction.

11:55.390 --> 12:03.640
So for example, select on or off, All right.

12:03.670 --> 12:06.610
With a colon and a space.

12:06.610 --> 12:08.980
So we have this instruction.

12:09.010 --> 12:15.310
The input is going to block the program until we give something when we press enter and we're going

12:15.310 --> 12:17.860
to get that inside the user input variable.

12:17.890 --> 12:24.160
Once we have this, we could directly send the string to the Arduino because we validate the data here

12:24.160 --> 12:24.700
also.

12:24.700 --> 12:34.030
But we can also say, for example, if user input and how to validate this, well we want it to be on

12:34.030 --> 12:34.810
or off.

12:34.810 --> 12:37.150
So what we can do is in.

12:38.700 --> 12:41.350
On off.

12:41.970 --> 12:51.750
So basically, if the user input is in that list, which contains two values, then we're gonna do something.

12:51.750 --> 12:53.910
If not, so else.

12:53.940 --> 12:55.500
Basically, we don't do anything.

12:55.500 --> 12:57.270
So here I'm not going to put an else.

12:57.660 --> 13:05.340
So if the user input is correct, then we do set dot right and well, actually, I'm not just going

13:05.340 --> 13:13.320
to send user input, I'm going to add a backslash N Okay, so let's do for example str to send.

13:13.320 --> 13:20.610
So basically the string to send is equal to user input plus backslash.

13:20.610 --> 13:21.840
N okay.

13:21.840 --> 13:23.880
And we send str.

13:25.380 --> 13:25.890
Who censor.

13:25.890 --> 13:32.970
We write as STR to send dot encode with UTF eight.

13:33.780 --> 13:34.030
Okay.

13:34.050 --> 13:38.970
Make sure to do the encode on the string and not on the write function.

13:38.970 --> 13:42.000
Very important, so you should have open parentheses here.

13:42.030 --> 13:48.600
Open parentheses here and close and close at the end and well, it seems that pretty much it.

13:48.630 --> 13:55.020
Maybe we could add a print here so we can see on the Raspberry Pi side what's happening.

13:55.020 --> 13:57.360
So let's see.

13:57.600 --> 13:58.800
Let's print here.

13:59.220 --> 14:10.950
Print, send comments to Arduino with so plus user input.

14:12.790 --> 14:19.550
So we know that we are sending a command to the Arduino and then we can check if that works on the Arduino.

14:19.570 --> 14:25.030
If we correctly blink the Led, but we already know it works, so let's run that.

14:25.720 --> 14:29.920
I'm going to click on Run here and let's save this as.

14:31.330 --> 14:36.320
Activity one dot python.

14:36.340 --> 14:38.050
Okay, so we have the activity.

14:38.050 --> 14:42.530
One sketch for Arduino and the activity one python file.

14:42.550 --> 14:47.980
Let's press enter and let's see what we got here.

14:48.370 --> 14:49.320
Okay, so.

14:49.540 --> 14:51.900
Okay, select on or off.

14:51.910 --> 14:53.080
You can see the problem is stuck.

14:53.080 --> 15:02.320
Now the Arduino has reboot and now I'm going to send on and you can see we have the Led here which is

15:02.320 --> 15:03.130
powered on.

15:03.160 --> 15:06.340
I send off the Led is powered off.

15:06.340 --> 15:10.920
Let's send anything else, let's say led on nothing happens.

15:10.930 --> 15:13.240
Let's send on again it works.

15:13.570 --> 15:18.760
And now if I press Ctrl C, you can see close serial communication.

15:19.120 --> 15:22.000
All right, so that's the end of the first activity of this course.

15:22.000 --> 15:28.120
Now, if you have a slightly different code than me on both sides and if it still works well, that's

15:28.120 --> 15:28.630
great too.

15:28.630 --> 15:33.170
Okay, this so my solution is just one possible solution.

15:33.170 --> 15:33.560
All right?

15:33.560 --> 15:35.840
It is not the solution.

15:35.990 --> 15:41.930
And if you couldn't find the solution by yourself, well, don't worry, because as I told you, the

15:41.930 --> 15:44.690
activities here in this course are quite challenging.

15:44.690 --> 15:51.590
And what I recommend you to do is to actually come back to this activity maybe in 1 or 2 days or one

15:51.590 --> 15:52.100
week.

15:52.100 --> 15:58.880
Just watch the introduction of the activity again and then do it by yourself and you will see that it's

15:58.880 --> 16:02.870
going to be much easier for you and you're going to get more understanding.

16:02.990 --> 16:03.380
All right.

16:03.380 --> 16:05.420
And now let's go to the next activity.
