WEBVTT

00:00.080 --> 00:02.260
This is activity two of this course.

00:02.270 --> 00:08.840
And in this new activity, what you are going to do is you are going to send some data from the Arduino

00:08.840 --> 00:16.470
to the Raspberry Pi and then process that data on the Raspberry Pi and send back something to the Arduino.

00:16.500 --> 00:16.690
Okay.

00:16.700 --> 00:23.900
So that's something new because what we've done before is to just send data directly from one side to

00:23.900 --> 00:29.150
the other one or to send some data from the Raspberry Pi and respond from the Arduino.

00:29.180 --> 00:34.640
Here we initiate the communication from the Arduino and we respond from the Raspberry Pi.

00:34.640 --> 00:38.780
But as you will see, that is not more complicated than what we have done before.

00:38.810 --> 00:39.290
Okay.

00:39.290 --> 00:44.060
So if you correctly understood the previous things, that's going to be okay for you.

00:44.090 --> 00:46.340
So what is your challenge?

00:46.340 --> 00:51.060
Your challenge is to send some temperature from the Arduino to the Raspberry Pi.

00:51.080 --> 00:56.420
So the temperature here, we don't have a temperature sensor and I'm not going to use one.

00:56.420 --> 00:58.630
We are just going to fake some temperatures.

00:58.640 --> 00:58.850
Okay.

00:58.850 --> 01:00.030
For this example.

01:00.030 --> 01:05.880
And so you send the temperature from the Arduino and then you're going to check this temperature in

01:05.880 --> 01:08.310
the Raspberry Pi in the Python program.

01:08.310 --> 01:14.820
And if this temperature is lower than a certain threshold, you are going to power on the Led.

01:14.970 --> 01:17.040
So the built in led on the Arduino.

01:17.070 --> 01:22.380
If the temperature is above the threshold, you are going to power off the Led.

01:23.010 --> 01:34.560
So on the Arduino side is to send temperature and then on parallel is to receive data from sale and

01:34.800 --> 01:38.580
power on or off the Led.

01:39.840 --> 01:46.740
So here I have given you some code structure here you can already use.

01:46.770 --> 01:51.510
So basically to send the temperature, let's say we want to send the temperature every one second.

01:51.510 --> 01:57.070
So every 1000 milliseconds, we are not going to use the delay because if we use the delay in the while

01:57.120 --> 02:02.610
loop and then we try to receive data from cell, well, the delay is going to block the program and

02:02.610 --> 02:06.030
we are going to receive data from cell with some delay, actually.

02:06.030 --> 02:06.200
Okay.

02:06.210 --> 02:12.330
So if you block the program for one second, you're going to have a delay of maximum one second to receive

02:12.330 --> 02:13.290
data from the cell.

02:13.290 --> 02:14.310
And you don't want that.

02:14.310 --> 02:17.190
You want to receive data as soon as it is here.

02:17.220 --> 02:17.730
Okay.

02:17.730 --> 02:21.630
So what we're going to do is we're going to do some kind of multitasking.

02:21.630 --> 02:26.070
And as a recap, I have shown you a structure here for multitasking.

02:26.070 --> 02:28.080
So basically that's very simple.

02:28.110 --> 02:28.310
Okay?

02:28.320 --> 02:35.280
You create so for an action you want to do, you create this variable unsigned long with the last time

02:35.280 --> 02:40.000
you have done the action, you initialize it to zero or millis doesn't matter.

02:40.000 --> 02:46.840
And then you have an action delay, which is the delay you want to spend between two actions that you

02:46.840 --> 02:47.020
do.

02:47.020 --> 02:49.480
So here, for example, 1000 millisecond.

02:49.840 --> 02:56.350
Then in the while loop you get the current time with Millis and then you check the difference between

02:56.350 --> 02:57.070
the current time.

02:57.070 --> 03:00.070
So the time now and the last time you have done the action.

03:00.100 --> 03:05.950
This gives you a duration and if this duration is greater than the action delay, then you can do the

03:05.950 --> 03:06.400
action.

03:06.400 --> 03:13.240
And of course you update the last time the action was done to the current time and then you do the action.

03:13.240 --> 03:18.940
So here you can select a random number for the temperature and send it to the cell.

03:19.090 --> 03:29.020
And here on parallel you can do so read cell basically this.

03:29.030 --> 03:30.310
So this is going to be very fast.

03:30.310 --> 03:31.360
This is going to be very fast.

03:31.360 --> 03:33.550
So it's going to go very, very, very fast.

03:33.550 --> 03:39.340
So it's just like if you were doing some kind of multithreading on your Arduino and to get the random

03:39.340 --> 03:42.550
number, I have given you the random function here of the Arduino.

03:42.550 --> 03:47.410
So basically, if you want a random number between, for example, five and 25, you just do random

03:47.410 --> 03:49.300
five comma 25.

03:49.330 --> 03:53.350
Okay, I'm going to use Celsius degrees here, but doesn't really matter.

03:53.350 --> 03:57.340
So if you do this, the random number will be between 5 and 25.

03:57.370 --> 04:03.400
Now on the Raspberry Pi, what you're going to do is read Serial, okay?

04:03.580 --> 04:05.740
And then you're going to receive some temperature.

04:05.740 --> 04:10.450
So receive number and then process that number.

04:10.450 --> 04:18.850
So basically you're going to say if number is, for example, lower than 15, you're going to have power

04:18.850 --> 04:23.590
on the LCD and else you are going to power off the LCD.

04:23.860 --> 04:32.590
So in your own kind of void loop on the Raspberry Pi, you first read from Serial, and once you have

04:32.620 --> 04:37.060
gotten some data, you process the data and you write directly to the Arduino.

04:37.210 --> 04:37.750
Okay?

04:37.960 --> 04:44.770
And well, what I'm going to do is I'm going to actually get the code from activity one.

04:44.800 --> 04:46.030
I'm going to get that.

04:46.030 --> 04:49.300
I'm not going to rewrite everything in this case.

04:49.300 --> 04:50.350
Why is that?

04:51.010 --> 04:54.550
Because simply we are going to need to use this.

04:54.580 --> 05:01.240
We are going to need to initialize the communication with the exact same code and then we have the exact

05:01.240 --> 05:03.190
same while true structure.

05:03.190 --> 05:04.900
I'm just going to remove all of that.

05:04.900 --> 05:12.040
Let's put bus here and while this code is going to be the same, okay, for the previous ITV and this

05:12.040 --> 05:12.220
one.

05:12.220 --> 05:20.740
So I'm not going to write it every time because we have basically here 14 lines of code that are identical.

05:20.800 --> 05:21.370
All right.

05:21.370 --> 05:25.180
And now we'll see you in the next lesson for the solution.
