This is the solution of the first activity of this course where you have to power on or power off an
on the Arduino from a user input you get from the Raspberry Pi.
And let's get started.
So I have created a new project file in Python here on Thonny Python and also a new project on Arduino.
Okay, so start from a blank page.
Now, the first thing before we even start to write some code is to define the protocol.
So I've already done that actually for you in the first activity, but I'm going to talk about that
just a few more seconds.
Okay.
So as you can see, it's super important to know in advance what data you are going to send and receive
from both sides.
Okay.
As you can see here, with on and off, we send that and we receive that if we don't agree on both sides,
okay, to send the same data, then we are going to have some problems.
Okay.
Think about what data you need to send and receive and then write this in advance in, for example,
a text document or in a comment.
This is the protocol you're going to use, and for each application you are going to do well.
You are going to define a custom protocol before you write any code.
Okay.
Let's say, for example, here, I wanted to send an Led on an Led off.
Well, if I send this and if I try to read on, well, you can see that if I send led on, I'm not going
to be able to detect on because this is not the same string.
Okay.
So I need to make sure that I send something that is the same on both sides.
All right.
So now let's start with the Arduino code.
Okay.
So once again, we start from Arduino because that's simply easier to debug.
Okay.
We can start with this code, debug it with the monitor, make sure that the communication is correctly
working on that side and then we can create the other side.
So here we have two things to do.
We have to set up the Led and we have to set up the communication.
I'm going to start with the Led.
Okay.
So we first start with the Led and then we use the serial and from the side we can directly make the
Led blink so that we can check that the serial is correctly working.
So let's create a define Led PIN 13 for the Led.
And then in the setup I am going to do pin mode led pin output.
All right, So we have a define.
We have set output mode for the Led.
So now we can do digitalwrite on the Led with high or low.
And so well, when are we going to do digitalwrite with high and low?
We are going to do that when we receive some data from the cell.
So in the while loop, what I'm going to do is I'm going to read from Serial, but before I'm going
to of course, initialize Serial with Begin, let's use this baud rate, okay?
And let's do while on serial like this with exclamation mark.
So not needed on Arduino Uno.
But for example, if you have a Leonardo or a zero board, well, you're going to need that.
So I'm going to put that anyway.
All right.
Cell is initialized.
We have everything in the void setup.
Now in the void loop I'm going to do if cell dot.
With a label like this with parentheses is greater.
Strictly greater than zero means that we have received some data over Serial.
Then we can read this and we are going to do string.
Let's create a cmd variable which means command.
So usually I prefer to write the full name for the variables.
But when you have something like command where CMD is pretty clear and because we are going to use the
CMD variable a lot in the following, so I'm just going to go with string cmd.
Okay.
Serial dot.
Read string until end.
Backslash n with important single quotes here.
All right.
And semicolon.
So basically we use the same function as before, and now we have the comment.
So we receive a comment from here.
What we are going to do is we are going to check that comment.
So if the comment is equal to UN, okay, So if you want to do a comparison with String with the Arduino
string, you can simply use the two equal sign.
So if the comment is on what do we do?
Well, we do digital, right?
With the pin and height and if the command is off.
So let's do an else if actually.
Command is off.
We are going to do digital right led pin with low.
So if we receive.
On we power on the Led.
If we receive off we power off the Led and then I'm going to add an else because well you could receive
any string here you could receive on, you could receive off but you could receive anything.
Okay.
You don't know what's going to happen.
You don't know what the other side is going to send.
Okay.
And as a basic principle, when you write some code like this, you don't trust the other side.
Okay?
Here you are writing the other side, so that's good.
But usually don't trust what the other side sends you.
Okay?
So make sure to always check your receive on your receive off or you receive something else that can
happen.
Okay.
So if you receive something else, well, what we're going to do in that case is just, well, nothing.
So I'm going to keep it like that for now.
But for example, you could print an error so you can send back an error, for example, or you can
print an error on an LCD screen.
Or you could do another behavior.
For example, you could flush the input buffer.
We are going to do that a bit later.
Okay.
But for now, you can see we check if we have some data.
If yes, we read the data, that's going to be our command string.
We check if it's equal to on, then we do an action.
We check if it's equal to off, we do an action and then else everything else we can do something else
that we want, but for now we do nothing.
All right, let's now try this code.
But we don't need to write the python code.
We can just start this.
Let's make sure that we have the Arduino plugged.
Okay, let's.
Oops, I have selected the wrong board.
So tool Arduino Uno and the port is correct.
Now I'm going to upload this, so let's save it as activity one.
Okay.
Enter compiling, uploading.
And now that the code on the Arduino is running, what we can do is we can already try this behavior
with the serial monitor.
So we're going to open.
So instead of having a python program running on the Raspberry Pi, we just have the serial monitor
running on the Raspberry Pi and we can send command from there.
All right.
So make sure that you have the same baud rate here, okay?
Check.
This is correct.
All right.
And then new line, because we are going to send a new line character at the end.
Okay.
So we don't need to write it manually.
And then I'm going to send some comments.
So let's say I send on you can see the Led here is powered on now I send off the Led is powered off,
I send on again and I can power on the Led.
Now if I send let's say led on which is not correct nothing is going to happen led off.
Nothing is going to happen.
Any other string, nothing is going to happen.
You can see we still have the X which is blinking because we are receiving some data, but then the
data is processed in the code and it goes directly to the else which does nothing.
Okay.
So you have to send exactly the on and off command to do something.
And you can see that with our experimentation, the behavior is correct here.
So we can know that the Arduino code is correct.
And now we can write the code on the Raspberry Pi.
So let's start by initializing the communication.
So I'm going to go with.
So this is a bit of Python three for the interpreter.
And then let's import Serial.
Let's also import time because we're going to need that library.
And I am going to do, sir, is equal to cereal dot cereal with the port.
So the port is still the same for me.
Slash dev slash tty ACM zero.
Okay.
The baud rate very important.
Exact same here.
And then the read timeout.
Let's put one second.
Well for this case, we don't really need the timeout here.
The read timeout because we're not going to read anything but I still put it anyway.
So here we open the cell communication and we get the cell in the cell object here.
I'm not going to do the retry code, okay?
Just to keep it simple for this activity, but you can do it if you want to.
So once we have this, I'm going to do a time dot sleep with three seconds.
Okay.
To wait because this is going to connect to the Arduino and here on the Arduino, no, it's going to
restart the program.
So we make sure that we have spent enough time so the Arduino is ready.
And then we're going to do set dot reset input buffer, which is well, not needed as well here because
we don't have anything in the input buffer because the Arduino doesn't send anything.
But still, I'm going to keep it for best practice because we're going to initialize the cell the same
way every time.
All right.
And let's say print cell.
Okay.
Like this.
Now what we can do is we can do our while.
True.
Okay.
So this is kind of the void setup of the Raspberry Pi.
And this is the void loop.
All right, void setup, void loop, void setup and void loop.
So while true, so what I'm going to do is, of course, I'm going to do a try and accept.
So let's just put pass here, try accept keyboard interrupt and when we press Ctrl C, I'm going to
do so.
I'm going to say print close serial communication and set dot close before we exit the program.
Okay.
So again, this is going to be the same every time and in the while true.
What do we do?
So this is the behavior of our void loop.
Well, basically, we want the user input and from that user input we send data to the Arduino.
So I'm going to do, let's say user input is equal to input.
Okay.
I can put some text here to send some instruction.
So for example, select on or off, All right.
With a colon and a space.
So we have this instruction.
The input is going to block the program until we give something when we press enter and we're going
to get that inside the user input variable.
Once we have this, we could directly send the string to the Arduino because we validate the data here
also.
But we can also say, for example, if user input and how to validate this, well we want it to be on
or off.
So what we can do is in.
On off.
So basically, if the user input is in that list, which contains two values, then we're gonna do something.
If not, so else.
Basically, we don't do anything.
So here I'm not going to put an else.
So if the user input is correct, then we do set dot right and well, actually, I'm not just going
to send user input, I'm going to add a backslash N Okay, so let's do for example str to send.
So basically the string to send is equal to user input plus backslash.
N okay.
And we send str.
Who censor.
We write as STR to send dot encode with UTF eight.
Okay.
Make sure to do the encode on the string and not on the write function.
Very important, so you should have open parentheses here.
Open parentheses here and close and close at the end and well, it seems that pretty much it.
Maybe we could add a print here so we can see on the Raspberry Pi side what's happening.
So let's see.
Let's print here.
Print, send comments to Arduino with so plus user input.
So we know that we are sending a command to the Arduino and then we can check if that works on the Arduino.
If we correctly blink the Led, but we already know it works, so let's run that.
I'm going to click on Run here and let's save this as.
Activity one dot python.
Okay, so we have the activity.
One sketch for Arduino and the activity one python file.
Let's press enter and let's see what we got here.
Okay, so.
Okay, select on or off.
You can see the problem is stuck.
Now the Arduino has reboot and now I'm going to send on and you can see we have the Led here which is
powered on.
I send off the Led is powered off.
Let's send anything else, let's say led on nothing happens.
Let's send on again it works.
And now if I press Ctrl C, you can see close serial communication.
All right, so that's the end of the first activity of this course.
Now, if you have a slightly different code than me on both sides and if it still works well, that's
great too.
Okay, this so my solution is just one possible solution.
All right?
It is not the solution.
And if you couldn't find the solution by yourself, well, don't worry, because as I told you, the
activities here in this course are quite challenging.
And what I recommend you to do is to actually come back to this activity maybe in 1 or 2 days or one
week.
Just watch the introduction of the activity again and then do it by yourself and you will see that it's
going to be much easier for you and you're going to get more understanding.
All right.
And now let's go to the next activity.