You can send some data from your Arduino to your Raspberry Pi.
And let's start again to see how to send data from the Raspberry Pi to the Arduino.
And so we are not going to start from scratch.
Okay, I'm just going to remove that code here and that code here.
So we keep the same structure on the Arduino.
We're going to initialize Serial and make sure it's up and on the Raspberry Pi, we're the same thing.
We open the serial, we wait a bit, reset input buffer, and then we have this infinite while loop,
which is basically the same as while loop with the accept keyboard interrupt to close the serial communication
when we kill the program.
Okay.
And here we're going to do our stuff here.
So as always, let's start with Arduino here.
What we want to do is we don't want to send data.
We want to receive data and to receive data.
Well, we are going to do basically the same as what we did previously with the Raspberry Pi, which
is to first check if we have received some data.
And if yes, read the next line.
Okay, So what we can do is if serial dot so we have a function which is dot available.
Okay.
You can see it should turn orange.
So parentheses, if this is strictly greater than zero, then we have received some data.
So this function is going to return the number of bytes that the Arduino has received in the buffer,
which is the same thing as the serial in waiting attribute we have in the Raspberry Pi.
Okay, that's the same thing.
So this is the number of bytes.
We don't really care about the number of bytes.
We just care that the number of bytes is greater than zero, which means we have received something.
If this is the case, well, I'm going to just put string.
Let's say message is equal to we can do serial dot read string.
This is going to read the next string.
So it's going to also decode it and put that inside a string message.
So very simple.
Now if you do that and that's where I'm going to talk about the timeout.
So basically there is a reading timeout of one second by default.
So when you use this, the program is going to be stuck for at least one second.
Okay.
So basically, as long as you receive some strings, it's going to wait one second to be sure that it
hasn't received more than what you've already received.
And so if we just run like this and then we send data from the Raspberry Pi to the Arduino, well,
we're going to see some delay in the processing on the Arduino.
So what we can do, there are two options.
The first option is to do serial dot set timeout with.
So the default is 1000 milliseconds.
What we can do is maybe put ten milliseconds.
So that's going to be much faster, but still it's going to be stuck for at least ten milliseconds.
And the lower you go, actually, the higher the risk of missing some data or of just reading so fast
that you don't receive everything and you receive an incomplete message.
So ten milliseconds should work.
But I'm not going to spend too much time on this because we are not going to use set timeout.
Instead, I'm going to use another function which is read string until and I'm going to put the backslash
n character with and this is important single quotes and not double quotes.
Okay, single quotes.
Again, this is super, super important.
So this function is going to read the string until it gets to this character and this function is going
to return as soon as it detects this character.
So the timeout still applies.
So we still have a timeout of one second that's going to block this one second.
But if you receive that character, the timeout doesn't apply anymore and you just get the string.
So basically if we send here from the Raspberry Pi a string with a backslash n character at the end,
so a new line, then we're going to be able to read it immediately with this function.
And so this is basically the function I'm going to use for all the codes read string until which is
quite powerful, quite efficient, and it's going to give us directly a string that we can process afterwards.
Okay.
And then once we have received this message, we can process it and do whatever we want in the Arduino.
All right.
Now I'm going to go back to the Python code on the Raspberry Pi.
So first I'm going to upload that actually to the Arduino.
So now it's running and it's waiting for data on Python.
If you want to send something on the sail, what you can do is do.
So you use the set object we have here, okay?
From cell cell dot write and then you're going to put.
You want to write?
Let's say hello from Raspberry Pi.
And we are going to add a backslash n character at the end.
So that's very important.
Okay.
Because if you don't send that, this is going to wait one second.
So it's still going to receive hello from Raspberry Pi.
But after one second, if you add the backslash n as soon as this function detects the backslash, N
is going to return with this exact string and nothing is going to be stuck.
And then we're coming back here.
As you have seen previously, when we read data from Samuel, we need to decode it.
Well, here when we write data, we need to encode it.
So you're going to do dot Encode with UTF eight, so the same UTF eight as before.
And it's very important here that the dot encode is directly on the string.
Okay, you don't do this, okay, you don't do self dot, right?
And then dot encode No, you do set dot right with the string dot encode and then you end the parenthesis
here.
Okay.
So make sure you have the correct order for the parentheses.
That's super, super important here.
All right, so this is going to send hello from Raspberry Pi to the Arduino.
Maybe we can change the time.sleep here.
Let's put one second.
So we send this every second.
Now the code is running on the Arduino.
I'm going to run the code also on the Raspberry Pi.
Let's see what we get.
Okay, So, yeah.
Okay.
So it's running actually, let's do control C and let's add a print here.
Print send message to Arduino.
All right.
So we can have a better idea of what's going on.
Okay.
Send message to Arduino.
Send message to Arduino Et-cetera.
Now?
Well, what we did before, when we actually sent some data from Arduino to Raspberry Pi is that we
could directly print the data we got on the terminal here or on the shell.
But now we send data from Raspberry Pi to Arduino, and how can we actually check and print and debug
this data we've received?
We could do serial dot println with the message.
Okay.
But the thing is that here on the Arduino we are using println to send message to the Raspberry Pi and
also to debug.
Okay.
So you can't just use these to print the message, for example, on the serial monitor because you're
already using the serial communication with the Raspberry Pi.
So that's one thing that is very important is that on the Arduino side, well, when you receive something
from Serial, don't just print it or you can just print it like this.
If you want to test just with the Arduino and the monitor, but then when you communicate with the Raspberry
Pi, don't just print some logs of some debug.
For example, here, if you print serial communication ready here, if you print received some string,
well, everything that you print actually is going to be sent back to the Raspberry Pi.
Okay.
And that's something you don't want if it's just for debugging.
So, well, we are left with this and well, how to debug that?
That's actually a good question and there are different ways to do this.
So first, of course, as I told you, you could just print what you received.
Run with the serial monitor, check if everything good, you just remove the print.
Another option would be to use an external component.
For example, an LCD screen that you plug to your Arduino.
And that's actually what we are going to do in the following of this course.
So you could receive some stuff from sale and print that on the LCD screen.
And what you can also see as a confirmation that things are working, I mean, at least that you receive
data here is.
So let's run the script again and let's look at the Arduino board.
And you can see the X is blinking every one second and I'm going to kill this.
Okay.
And the X is not blinking anymore, so ah, x is for reception.
So as you can see, if the ah x is on, it means you are receiving data on the Arduino.
All right.
And now that we have seen how to send data only from one side to the other, let's check how to do a
bidirectional communication.