This is the solution of the activity.
Two, where you have to send some random temperature from the Arduino process that in the Raspberry
Pi so that you can choose to power on or power off the Led.
If, for example, you can power on the Led if the temperature is too low.
So let's start with the Arduino side.
Once again.
I'm going to remove that.
So I already have this.
Okay.
Unsigned long last time.
Action.
I'm going to remove this and just rename it last time.
Let's say temperature sent.
Okay.
Which makes more sense.
And then I'm going to do temperature.
Send delay.
And let's keep one second.
Okay.
So now, void loop.
Unsigned long.
We get the time now with Millis and we check if the time now minus the last time temperature sent is
greater than temperature.
Send delay.
And in that case, we don't forget to update the last time temperature sent to the time now.
So basically, this here, this code here is going to be called every one second.
And we don't block the void loop with the delay.
So to get the temperature I'm going to do int temperature is equal to and let's use the random 5 to
25.
Okay.
And itrillionemove that.
And what we're going to do now is we're going to send this to the cell.
So I'm going to do in the void setup Serial.begin.
So same code.
And then while cell like this.
And here I can do serial dot println with temperature.
All right, let's actually test this code.
So I'm going to do a upload activity to.
Don't uploading and how to test this code.
I just open the soil monitor and let's see what we receive.
12, 12, 14, 18, 23, 15, 17, nine.
So you can see it's correctly working.
We receive a random number between 5 and 25 every second.
Okay.
So the first part of the Arduino code is done now in parallel.
So I say in parallel because we're going to do the two actions, one after the other, but very fast.
So it's just like we are doing them in parallel.
I'm going to do if soil dot available.
Is greater than zero.
So if we have received some data, what we're going to do is the same thing we did in the previous activity.
So I'm going to do that very fast.
String command is equal to self dot read string until backslash n with one single quote.
And then if command is equal to n, we are going to power on the else if command is equal to.
Off, we power off the lid and then else we can process the other command and for example, print an
error or something like that.
Okay.
Now for the Led, I will need to initialize Led.
So let's create a define Led pin 13 here.
Let's do so.
I can do the PIN mode before or after serial.
It doesn't really matter here.
PIN mode led pin with output.
And here I do digital write led pin with high because we power on the Led and digital write led pin
low because we power off the Led.
Okay.
So we have this action and this action running separately.
As you can see here, we send data with sale dot print ln and we receive data with sale dot read string
until but the two are not directly linked.
Okay?
We don't just send data when we receive data.
Okay.
We have separated the two actions.
Let's upload this code to the Arduino.
Okay.
And what you can do, for example, is you can just open the monitor.
You're going to see that you receive some numbers.
Okay.
And let's just give on and let's see that the Led is powered on.
Okay, That's working.
And as you may see, the random here is maybe not that random because we have the same data as before.
So you could make it more random, but I'm not going to spend time on this.
Okay.
That's outside of the scope of this course.
If you want, you can search more about this by yourself.
All right.
So the Arduino code is done.
Let's go to the Raspberry Pi side.
Okay.
So we read from Serial and we receive a number.
So if we want to read from Serial, what we are going to do is to do if, say, dot in waiting is greater
than zero.
And here no parenthesis.
Okay, you have parentheses here with dot available, no parentheses here.
So if we have received something, then we read that thing.
Okay, so let's say temperature is equal to C dot read line.
Dot decode with UTF.
Eight and strip.
Okay, So the same thing we did before.
So we read the line, we decode it because it's bytes.
So we decode it into a string and we remove any other special character we have at the end.
And what I'm going to do is I'm also going to cast this into an integer, okay?
Because what we want to process the temperature as an integer.
So I'm going to cast it as an integer.
And to do that I simply do INT and I put everything in the parentheses and now if temperature is lower,
strictly lower than 15, let's say.
Then we are going to power on the Led.
So let's put a comment here.
Power on Led and then else.
So basically, if the temperature is greater or equal than 50, we are going to power off.
So let's actually do a print.
Print.
Temperature lower than 15.
Lead on.
And here.
Print.
Temperature lower or greater actually than 15 of.
And then, of course, we send the command.
So the command is still on and off.
Okay.
So say dot write with on and backslash n dot encode with UTF.
Eight like this.
And then I'm going to copy this here in the else.
And instead of on, we send off.
So as you can see the structure here.
So we continue with our while true.
So the infinite loop, if we have received some data, we read that data.
So we cast it as an int.
So here it's very important, as you see here, that we also have defined a protocol.
The protocol is simply that here we send the number, here we receive a number, okay?
And here we send on and off.
Here we receive on and off.
Okay.
So on both sides, we know what we receive and what we send.
Okay, let's now try this code so it's already uploaded on the Arduino.
I'm going to run that on the Raspberry Pi.
So activity 2.py.
It's going to be added automatically.
And let's see what we get.
Okay.
You can see that when the temperature is lower than 15, we have the Led on and when the temperature
is greater than 15, we have the Led off.
All right.
And that's the end of this activity.