WEBVTT

00:00.080 --> 00:02.090
This is the solution of the activity.

00:02.090 --> 00:08.690
Two, where you have to send some random temperature from the Arduino process that in the Raspberry

00:08.690 --> 00:13.210
Pi so that you can choose to power on or power off the Led.

00:13.310 --> 00:17.840
If, for example, you can power on the Led if the temperature is too low.

00:17.840 --> 00:19.790
So let's start with the Arduino side.

00:19.820 --> 00:21.110
Once again.

00:21.650 --> 00:23.240
I'm going to remove that.

00:23.870 --> 00:27.230
So I already have this.

00:27.260 --> 00:27.890
Okay.

00:28.040 --> 00:29.880
Unsigned long last time.

00:29.900 --> 00:30.350
Action.

00:30.560 --> 00:35.330
I'm going to remove this and just rename it last time.

00:35.810 --> 00:39.440
Let's say temperature sent.

00:39.500 --> 00:40.050
Okay.

00:40.070 --> 00:41.840
Which makes more sense.

00:41.840 --> 00:45.650
And then I'm going to do temperature.

00:46.220 --> 00:49.340
Send delay.

00:51.440 --> 00:53.120
And let's keep one second.

00:53.150 --> 00:53.690
Okay.

00:54.080 --> 00:55.520
So now, void loop.

00:55.550 --> 00:56.390
Unsigned long.

00:56.390 --> 01:04.190
We get the time now with Millis and we check if the time now minus the last time temperature sent is

01:04.190 --> 01:06.530
greater than temperature.

01:06.560 --> 01:07.940
Send delay.

01:08.300 --> 01:14.360
And in that case, we don't forget to update the last time temperature sent to the time now.

01:14.360 --> 01:20.450
So basically, this here, this code here is going to be called every one second.

01:20.450 --> 01:23.640
And we don't block the void loop with the delay.

01:23.730 --> 01:33.670
So to get the temperature I'm going to do int temperature is equal to and let's use the random 5 to

01:33.670 --> 01:34.230
25.

01:34.260 --> 01:34.770
Okay.

01:35.810 --> 01:37.100
And itrillionemove that.

01:38.910 --> 01:42.660
And what we're going to do now is we're going to send this to the cell.

01:42.960 --> 01:48.030
So I'm going to do in the void setup Serial.begin.

01:49.340 --> 01:51.350
So same code.

01:52.210 --> 01:57.170
And then while cell like this.

01:59.060 --> 02:05.240
And here I can do serial dot println with temperature.

02:08.100 --> 02:10.590
All right, let's actually test this code.

02:10.710 --> 02:17.910
So I'm going to do a upload activity to.

02:21.620 --> 02:24.790
Don't uploading and how to test this code.

02:24.790 --> 02:27.940
I just open the soil monitor and let's see what we receive.

02:27.970 --> 02:34.990
12, 12, 14, 18, 23, 15, 17, nine.

02:34.990 --> 02:37.210
So you can see it's correctly working.

02:37.210 --> 02:42.400
We receive a random number between 5 and 25 every second.

02:42.430 --> 02:42.910
Okay.

02:42.910 --> 02:47.380
So the first part of the Arduino code is done now in parallel.

02:47.380 --> 02:53.320
So I say in parallel because we're going to do the two actions, one after the other, but very fast.

02:53.320 --> 02:56.080
So it's just like we are doing them in parallel.

02:56.080 --> 03:01.090
I'm going to do if soil dot available.

03:03.850 --> 03:05.410
Is greater than zero.

03:07.570 --> 03:13.540
So if we have received some data, what we're going to do is the same thing we did in the previous activity.

03:13.570 --> 03:15.340
So I'm going to do that very fast.

03:15.460 --> 03:26.590
String command is equal to self dot read string until backslash n with one single quote.

03:26.590 --> 03:38.230
And then if command is equal to n, we are going to power on the else if command is equal to.

03:39.210 --> 03:50.100
Off, we power off the lid and then else we can process the other command and for example, print an

03:50.100 --> 03:51.370
error or something like that.

03:51.390 --> 03:51.900
Okay.

03:51.900 --> 03:56.610
Now for the Led, I will need to initialize Led.

03:56.610 --> 04:01.560
So let's create a define Led pin 13 here.

04:01.560 --> 04:02.700
Let's do so.

04:02.700 --> 04:05.240
I can do the PIN mode before or after serial.

04:05.250 --> 04:06.780
It doesn't really matter here.

04:06.870 --> 04:13.680
PIN mode led pin with output.

04:14.940 --> 04:28.740
And here I do digital write led pin with high because we power on the Led and digital write led pin

04:28.950 --> 04:31.050
low because we power off the Led.

04:31.320 --> 04:31.590
Okay.

04:31.590 --> 04:36.090
So we have this action and this action running separately.

04:36.090 --> 04:43.920
As you can see here, we send data with sale dot print ln and we receive data with sale dot read string

04:43.920 --> 04:46.800
until but the two are not directly linked.

04:46.800 --> 04:47.100
Okay?

04:47.100 --> 04:50.040
We don't just send data when we receive data.

04:50.070 --> 04:50.490
Okay.

04:50.490 --> 04:53.100
We have separated the two actions.

04:53.190 --> 04:55.470
Let's upload this code to the Arduino.

04:58.300 --> 04:58.540
Okay.

04:58.540 --> 05:01.810
And what you can do, for example, is you can just open the monitor.

05:01.840 --> 05:04.420
You're going to see that you receive some numbers.

05:04.540 --> 05:05.110
Okay.

05:05.110 --> 05:11.870
And let's just give on and let's see that the Led is powered on.

05:11.890 --> 05:12.940
Okay, That's working.

05:14.360 --> 05:22.070
And as you may see, the random here is maybe not that random because we have the same data as before.

05:22.070 --> 05:26.060
So you could make it more random, but I'm not going to spend time on this.

05:26.060 --> 05:26.300
Okay.

05:26.300 --> 05:28.400
That's outside of the scope of this course.

05:28.400 --> 05:31.070
If you want, you can search more about this by yourself.

05:31.280 --> 05:31.700
All right.

05:31.700 --> 05:33.590
So the Arduino code is done.

05:34.040 --> 05:36.440
Let's go to the Raspberry Pi side.

05:36.770 --> 05:36.980
Okay.

05:36.980 --> 05:39.830
So we read from Serial and we receive a number.

05:39.830 --> 05:50.120
So if we want to read from Serial, what we are going to do is to do if, say, dot in waiting is greater

05:50.120 --> 05:51.110
than zero.

05:51.110 --> 05:53.210
And here no parenthesis.

05:53.240 --> 05:59.570
Okay, you have parentheses here with dot available, no parentheses here.

06:00.350 --> 06:04.880
So if we have received something, then we read that thing.

06:04.880 --> 06:13.490
Okay, so let's say temperature is equal to C dot read line.

06:14.500 --> 06:19.240
Dot decode with UTF.

06:20.490 --> 06:24.250
Eight and strip.

06:26.880 --> 06:28.590
Okay, So the same thing we did before.

06:28.590 --> 06:31.620
So we read the line, we decode it because it's bytes.

06:31.620 --> 06:38.790
So we decode it into a string and we remove any other special character we have at the end.

06:38.790 --> 06:45.330
And what I'm going to do is I'm also going to cast this into an integer, okay?

06:45.330 --> 06:48.810
Because what we want to process the temperature as an integer.

06:48.810 --> 06:51.180
So I'm going to cast it as an integer.

06:51.240 --> 07:01.170
And to do that I simply do INT and I put everything in the parentheses and now if temperature is lower,

07:01.170 --> 07:03.150
strictly lower than 15, let's say.

07:04.230 --> 07:06.540
Then we are going to power on the Led.

07:07.290 --> 07:09.210
So let's put a comment here.

07:09.240 --> 07:12.540
Power on Led and then else.

07:12.570 --> 07:20.490
So basically, if the temperature is greater or equal than 50, we are going to power off.

07:21.330 --> 07:23.400
So let's actually do a print.

07:23.650 --> 07:24.360
Print.

07:25.930 --> 07:31.060
Temperature lower than 15.

07:32.710 --> 07:33.640
Lead on.

07:34.690 --> 07:35.770
And here.

07:37.350 --> 07:38.130
Print.

07:39.510 --> 07:49.530
Temperature lower or greater actually than 15 of.

07:50.790 --> 07:53.730
And then, of course, we send the command.

07:53.730 --> 07:56.220
So the command is still on and off.

07:56.220 --> 07:56.700
Okay.

07:56.880 --> 08:09.000
So say dot write with on and backslash n dot encode with UTF.

08:09.740 --> 08:12.470
Eight like this.

08:12.710 --> 08:16.250
And then I'm going to copy this here in the else.

08:16.250 --> 08:19.340
And instead of on, we send off.

08:19.670 --> 08:21.530
So as you can see the structure here.

08:21.530 --> 08:24.890
So we continue with our while true.

08:24.890 --> 08:29.780
So the infinite loop, if we have received some data, we read that data.

08:29.870 --> 08:31.630
So we cast it as an int.

08:31.640 --> 08:37.290
So here it's very important, as you see here, that we also have defined a protocol.

08:37.310 --> 08:43.090
The protocol is simply that here we send the number, here we receive a number, okay?

08:43.100 --> 08:45.800
And here we send on and off.

08:45.800 --> 08:48.080
Here we receive on and off.

08:48.080 --> 08:48.590
Okay.

08:48.590 --> 08:54.020
So on both sides, we know what we receive and what we send.

08:54.410 --> 08:59.240
Okay, let's now try this code so it's already uploaded on the Arduino.

08:59.270 --> 09:02.660
I'm going to run that on the Raspberry Pi.

09:05.070 --> 09:09.810
So activity 2.py.

09:09.840 --> 09:11.670
It's going to be added automatically.

09:13.910 --> 09:15.530
And let's see what we get.

09:16.700 --> 09:16.970
Okay.

09:16.970 --> 09:24.650
You can see that when the temperature is lower than 15, we have the Led on and when the temperature

09:24.650 --> 09:27.530
is greater than 15, we have the Led off.

09:29.240 --> 09:29.690
All right.

09:29.690 --> 09:32.030
And that's the end of this activity.
