This is activity two of this course.
And in this new activity, what you are going to do is you are going to send some data from the Arduino
to the Raspberry Pi and then process that data on the Raspberry Pi and send back something to the Arduino.
Okay.
So that's something new because what we've done before is to just send data directly from one side to
the other one or to send some data from the Raspberry Pi and respond from the Arduino.
Here we initiate the communication from the Arduino and we respond from the Raspberry Pi.
But as you will see, that is not more complicated than what we have done before.
Okay.
So if you correctly understood the previous things, that's going to be okay for you.
So what is your challenge?
Your challenge is to send some temperature from the Arduino to the Raspberry Pi.
So the temperature here, we don't have a temperature sensor and I'm not going to use one.
We are just going to fake some temperatures.
Okay.
For this example.
And so you send the temperature from the Arduino and then you're going to check this temperature in
the Raspberry Pi in the Python program.
And if this temperature is lower than a certain threshold, you are going to power on the Led.
So the built in led on the Arduino.
If the temperature is above the threshold, you are going to power off the Led.
So on the Arduino side is to send temperature and then on parallel is to receive data from sale and
power on or off the Led.
So here I have given you some code structure here you can already use.
So basically to send the temperature, let's say we want to send the temperature every one second.
So every 1000 milliseconds, we are not going to use the delay because if we use the delay in the while
loop and then we try to receive data from cell, well, the delay is going to block the program and
we are going to receive data from cell with some delay, actually.
Okay.
So if you block the program for one second, you're going to have a delay of maximum one second to receive
data from the cell.
And you don't want that.
You want to receive data as soon as it is here.
Okay.
So what we're going to do is we're going to do some kind of multitasking.
And as a recap, I have shown you a structure here for multitasking.
So basically that's very simple.
Okay?
You create so for an action you want to do, you create this variable unsigned long with the last time
you have done the action, you initialize it to zero or millis doesn't matter.
And then you have an action delay, which is the delay you want to spend between two actions that you
do.
So here, for example, 1000 millisecond.
Then in the while loop you get the current time with Millis and then you check the difference between
the current time.
So the time now and the last time you have done the action.
This gives you a duration and if this duration is greater than the action delay, then you can do the
action.
And of course you update the last time the action was done to the current time and then you do the action.
So here you can select a random number for the temperature and send it to the cell.
And here on parallel you can do so read cell basically this.
So this is going to be very fast.
This is going to be very fast.
So it's going to go very, very, very fast.
So it's just like if you were doing some kind of multithreading on your Arduino and to get the random
number, I have given you the random function here of the Arduino.
So basically, if you want a random number between, for example, five and 25, you just do random
five comma 25.
Okay, I'm going to use Celsius degrees here, but doesn't really matter.
So if you do this, the random number will be between 5 and 25.
Now on the Raspberry Pi, what you're going to do is read Serial, okay?
And then you're going to receive some temperature.
So receive number and then process that number.
So basically you're going to say if number is, for example, lower than 15, you're going to have power
on the LCD and else you are going to power off the LCD.
So in your own kind of void loop on the Raspberry Pi, you first read from Serial, and once you have
gotten some data, you process the data and you write directly to the Arduino.
Okay?
And well, what I'm going to do is I'm going to actually get the code from activity one.
I'm going to get that.
I'm not going to rewrite everything in this case.
Why is that?
Because simply we are going to need to use this.
We are going to need to initialize the communication with the exact same code and then we have the exact
same while true structure.
I'm just going to remove all of that.
Let's put bus here and while this code is going to be the same, okay, for the previous ITV and this
one.
So I'm not going to write it every time because we have basically here 14 lines of code that are identical.
All right.
And now we'll see you in the next lesson for the solution.